HTML中使用SVG与SVG预定义形状元素介绍

2022-10-20,,,,

svg 文件可通过以下标签嵌入 html 文档:<embed>、<object>或者<iframe>。

复制代码代码如下:
<embed src="rect.svg" width="300"height="100"
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/"/>

pluginspage 属性指向下载插件的 url。

复制代码代码如下:
<object data="rect.svg"width="300" height="100"
type="image/svg+xml"
codebase="http://www.adobe.com/svg/viewer/install/"/>
<iframe src="rect.svg" width="300"height="100">
</iframe>

在这三个中<iframe>是比较早期的标签,现在用的比较的少了。用的较多的还是<embed>标签。
同时我们也可以将svg直接写入html文件中:
这样的话需要先引入svg的dtd文件:

复制代码代码如下:
<?xml version="1.0" standalone="no"?>
<!doctype svg public "-//w3c//dtd svg 1.1//en"
"http://www.w3.org/graphics/svg/1.1/dtd/svg11.dtd">

再在<svg>标签中填入需要的代码:

复制代码代码如下:
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg" onclick="ccc();">
<ellipse cx="240" cy="100" rx="220" ry="30"
style="fill:yellow"/>
<ellipse cx="220" cy="100" rx="190" ry="20" id="w1"
style="fill:white"/>
</svg>

如果svg代码在html中,我们就更容易写javascript来控制图形的变换:

复制代码代码如下:
<script type="text/javascript">
function ccc(){
var a = document.getelementbyid("w1");
a.style.fill="red";
a.setattribute("cx", "150"); //设置值
a.setattribute("ry", "50"); //设置值
}
</script>

下面介绍一些svg的预定义的形状元素
矩形 <rect>
圆形 <circle>
椭圆 <ellipse>
线 <line>
折线 <polyline>
多边形 <polygon>
路径 <path>

《HTML中使用SVG与SVG预定义形状元素介绍.doc》

下载本文的Word格式文档,以方便收藏与打印。