微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

canvas画矩形和svg画矩形的两种方法代码

本篇文章给大家带来的内容是关于canvas画矩形和svg画矩形的两种方法代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1.canvas画矩形

<!doctype html>
<html>
<head>
<!--canvas画矩形-->
<style>
#huaban{
border:1px solid;
}
</style>
<script>
</script>
<Meta charset="UTF-8">
</head>
<body>
<canvas width="200" height="200" id="huaban">
</canvas>
<script>
var can=document.getElementById("huaban");//获得画板数据
var con=can.getContext('2d');//获得笔刷
   con.fillStyle="red";//设置填充颜色
   con.strokeStyle="blue";//设置线条颜色
   con.fillRect(10,10,100,100);//填充画矩形
   con.strokeRect(100,100,200,200);//线条画矩形

</script>
</body>
</html>

2.svg画矩形

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
        <rect width="300" height="100"></rect>    
        </svg>

rect 代表矩形;

rect 元素的 width 和 height 属性可定义矩形的高度和宽度;

圆角矩形:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
        <rect width="300" height="100" style="fill:deepskyblue;stroke-width:2;stroke:rgb(0,0,0);fill-opacity:0.5;stroke-opacity:0.9;opacity:0.5;" x="0" y="20" rx="20" ry="20"></rect>    
        </svg>

CSS 的 fill-opacity 属性定义填充颜色透明度(合法的范围是:0 - 1);

CSS 的 stroke-opacity 属性定义边框颜色的透明度(合法的范围是:0 - 1);

CSS 的 opacity 属性定义整个元素的不透明度(合法的范围是:0 - 1);

svg中旋转的话可以用transform='rotate(45)

相关文章推荐:

jpg图片转换成svg文字路径动画的实例(附代码)

SVG画图功能:svg实现画出一朵花(附代码)

以上就是canvas画矩形和svg画矩形的两种方法代码的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。