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

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" οnclick="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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值