用SVG做background image

Posted 动漫引擎

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用SVG做background image相关的知识,希望对你有一定的参考价值。

 

 

1 用utf8格式, 需要 双引号“”替换为单引号,而且采用url encode编码,例如# 替换为 %23,

body { background-image:
url("data:image/svg+xml;utf8,<svg xmlns=‘http://www.w3.org/2000/svg‘ xmlns:xlink=‘http://www.w3.org/1999/xlink‘ width=‘1060px‘ height=‘580px‘ viewBox=‘0 0 1060 580‘ preserveAspectRatio=‘xMidYMid meet‘ ><rect id=‘svgEditorBackground‘ x=‘0‘ y=‘0‘ width=‘1060‘ height=‘580‘ style=‘fill: none; stroke: none;‘/><rect x=‘115‘ y=‘111‘ stroke=‘black‘ id=‘e1_rectangle‘ style=‘stroke-width: 1px;‘ width=‘144‘ height=‘120‘ fill=‘khaki‘/><polyline stroke=‘black‘ stroke-width=‘1‘ id=‘e2_polyline‘ style=‘fill: none;‘ points=‘115 109 187 69 257 109‘ /><text fill=‘black‘ x=‘257‘ y=‘33‘ id=‘e4_texte‘ style=‘font-family: Arial; font-size: 20px;‘></text><rect x=‘179‘ y=‘19‘ stroke=‘black‘ id=‘e5_rectangle‘ style=‘stroke-width: 1px;‘ width=‘16‘ height=‘54‘ fill=‘khaki‘ /></svg>");
}

 


2 用base64格式, 把<svg>... </svg> 用btoa() 转换为base64编码

 

body { background-image:
url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB3aWR0aD0iMTA2MHB4IiBoZWlnaHQ9IjU4MHB4IiB2aWV3Qm94PSIwIDAgIDEwNjAgNTgwIiBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSJ4TWlkWU1pZCBtZWV0IiA+PHJlY3QgaWQ9InN2Z0VkaXRvckJhY2tncm91bmQiIHg9IjAiIHk9IjAiIHdpZHRoPSIxMDYwIiBoZWlnaHQ9IjU4MCIgc3R5bGU9ImZpbGw6IG5vbmU7IHN0cm9rZTogbm9uZTsiLz48cmVjdCB4PSIxMTUiIHk9IjExMSIgc3Ryb2tlPSJibGFjayIgaWQ9ImUxX3JlY3RhbmdsZSIgc3R5bGU9InN0cm9rZS13aWR0aDogMXB4OyIgd2lkdGg9IjE0NCIgaGVpZ2h0PSIxMjAiIGZpbGw9ImtoYWtpIi8+PHBvbHlsaW5lIHN0cm9rZT0iYmxhY2siIHN0cm9rZS13aWR0aD0iMSIgaWQ9ImUyX3BvbHlsaW5lIiBzdHlsZT0iZmlsbDogbm9uZTsiIHBvaW50cz0iMTE1IDEwOSAxODcgNjkgMjU3IDEwOSIgLz48dGV4dCBmaWxsPSJibGFjayIgeD0iMjU3IiB5PSIzMyIgaWQ9ImU0X3RleHRlIiBzdHlsZT0iZm9udC1mYW1pbHk6IEFyaWFsOyBmb250LXNpemU6IDIwcHg7Ij48L3RleHQ+PHJlY3QgeD0iMTc5IiB5PSIxOSIgc3Ryb2tlPSJibGFjayIgaWQ9ImU1X3JlY3RhbmdsZSIgc3R5bGU9InN0cm9rZS13aWR0aDogMXB4OyIgd2lkdGg9IjE2IiBoZWlnaHQ9IjU0IiBmaWxsPSJraGFraSIgLz48L3N2Zz4=");

 

用绘制SVG 

http://www.drawsvg.org/drawsvg.html

 

以上是关于用SVG做background image的主要内容,如果未能解决你的问题,请参考以下文章

android webview加载嵌入了svg的html,svg作为background-img,但是不同设备为啥显示不同,怎么解决好

RaphaelJS 条带化成 SVG

CSS:在 IE 中背景图像的 URL 参数中使用原始 svg

为啥 background-size:100% 100%;不适用于此 SVG?

background-image使用svg如何改变颜色

[SVG] Add an SVG as a Background Image