Giter Site home page Giter Site logo

encodesvg's Introduction

##encodeSVG 通过将SVG图形编码,在css:background-image: url("data:image/svg+xml,encodeddSVG")或者border-image中使用SVG背景图片 线上工具地址【传送门】:http://www.xiyoulive.com/encodeSVG

用的方便还请给个star

<template>
	<div>
		<h1>SVG图形编码工具</h1>
		作者:[email protected]
		<p>通过将SVG图形编码,在css:background-image: url("data:image/svg+xml,encode")或者border-image中使用SVG背景图片</p>
		<div class="cont">
		<h4>粘贴SVG代码,格式&lt;svg&gt;...code...&lt;/svg&gt;</h4>
			<textarea v-model="insert" class="con" placeholder="此处粘贴<svg>...code...</svg>"></textarea>
		</div>
		<div class="cont">
		<h4>编码后的代码</h4>
			<textarea v-model="encoded" class="con" placeholder="输出编码"></textarea>
		</div>
		<div class="cont">
		<h4>输出css</h4>
			<textarea v-model="css" class="con" placeholder="输出css"></textarea>
		</div>
		<div class="cont">
			<h4>预览</h4>
			<div class="preview con" id="pre"></div>
		</div>

	</div>
</template>

<script>
	export default {
    data() {
            return {
            	//这里数据绑定
            	insert:'',
            	encoded:'',
            	css:'',
            	}
        },
    watch:{
    	insert:function(val,oldVal){
    		//逻辑
    		let namespaced = this.addNameSpace( val );
    		let escaped = this.encodeSVG( namespaced );
    		this.encoded = escaped;
    		let resCss = 'background-image: url("data:image/svg+xml,' + escaped + '");';
    		this.css = resCss;
    		document.getElementById("pre").setAttribute( "style", resCss );
    	}

    },
    methods:{
    	encodeSVG:function(data){
    		//编码
    		let symbols = /[\r\n"%#()<>?\[\\\]^`{|}]/g;
    		
		    // 单引号防止重编码
		    if ( data.indexOf( '"' ) >= 0 ) {
		        data = data.replace( /"/g, "'" );
		    }
		    data = data.replace( />\s{1,}</g, "><" );
		    data = data.replace( /\s{2,}/g, " " );
		    //这里推荐使用encodeURL
		    return data.replace( symbols, encodeURI );
    	},
    	addNameSpace:function(data){
    		//xmlns='http://www.w3.org/2000/svg必须有
		    if ( data.indexOf( "http://www.w3.org/2000/svg" ) < 0 ) {
		        data = data.replace( /<svg/g, "<svg xmlns='http://www.w3.org/2000/svg'" );
		    }
		    return data;
    	}
    }
}
</script>

<style scoped>
.cont{
	display: block;
	width: 90%;
	margin-left: 5%;
	margin-top: 30px;

}
.con{
	width: 100%;
	height: 60px;
	background-repeat:no-repeat;
}
.preview{
	width: 100%;
	height: 200px;
	border:1px solid rgb(150,150,150); 
}
</style>

##作者 [email protected]

encodesvg's People

Contributors

tfzh2017 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.