html svg 背景图片,使用SVG编码作为CSS背景图

编程入门 行业动态 更新时间:2024-10-24 20:20:22

html svg <a href=https://www.elefans.com/category/jswz/34/1769059.html style=背景图片,使用SVG编码作为CSS背景图"/>

html svg 背景图片,使用SVG编码作为CSS背景图

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

console.clear();

Vueponent('file-upload',{

template: `

`,

data: ()=>({

dragging: false,

}),

methods:{

loadFiles(e){

e = e || window.event;

this.dragLeave(e);

var files = Array.from(e.target.files || e.dataTransfer.files || this.$ref.input.files),

len = files.length,

i = 0,

completed = [];

files.forEach((file) => {

var reader = new FileReader();

reader.onloadend = (ev)=>{

this.$emit('load', reader.result, reader, file);

//completed.push(reader.result);

//if ( completed.length >= files.length ) { this.$emit('loaded',completed); }

};

reader.readAsText(file);

//reader.readAsDataURL(file);

});

},

dragOver(e){ e.preventDefault(); /* Essential */ },

dragEnter(){ this.dragging = true },

dragLeave(e){ this.dragging = false; e.preventDefault(); },

}

});

new Vue({

el: '.svg-bg',

data: ()=>({

input: `

`

}),

computed: {

output(){

let url = this.encodeSVG(this.optimized);

document.body.style.backgroundImage = url;

let msg = 'background-image: ' + url + ';' +

'
/* SVG encoded with */';

return msg;

},

optimized(){

return this.input

.replace(/\/g, '')

.replace(/(\)/g, '')

.replace(/([\s\n]+)/g,' ');

}

},

methods: {

loaded(f){

console.log('file loaded!', typeof f, f);

this.input = f;

},

copy(){

var range = document.createRange();

range.selectNode(this.$refs.output);

window.getSelection().addRange(range);

try {

// Now that we've selected the anchor text, execute the copy command

var successful = document.execCommand('copy');

var msg = successful ? 'successful' : 'unsuccessful';

console.log('Copy email command was ' + msg);

} catch(err) {

console.log('Oops, unable to copy');

}

// Remove the selections - NOTE: Should use

// removeRange(range) when it is supported

window.getSelection().removeAllRanges();

},

encodeSVG(svg) {

svg = svg + '';

var b64 = encodeURIComponent(svg.replace(/\|\/g, ''))

.replace(/%20/g," ")

.replace(/%3D/g,"=")

.replace(/%3A/g, ':') // ditto colons

.replace(/%2F/g, '/') // ditto slashes

.replace(/%22/g, "'"); // replace quotes with apostrophes (may break certain SVGs)

return 'url("data:image/svg+xml;charset=utf-8,'+b64+'")';

}

}

});

更多推荐

html svg 背景图片,使用SVG编码作为CSS背景图

本文发布于:2024-02-27 13:04:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1706645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背景图片   背景图   svg   html   SVG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!