canvas绘图 | svg绘图 | |
类型 | 2D位图 | 2D矢量图 |
如何绘图 | 使用JS代码绘图 | 使用标签绘图 |
事件绑定 | 每个图形不是元素,无法直接绑定事件 | 每个图形都是元素,可以直接绑定事件监听 |
应用场合 | 游戏;特性 | 图标;地图 |
1.直接在HTML文档,使用SVG标准
<svg></svg> 本身一个 300 * 150 inline-block
矩形<rect>
圆形<circle>
椭圆<ecllipse>
直线<line>
折现<polyline>
多边形<polygon>
1.SVG绘图--矩形
<rect width="" height="" x="" fill="" fill-opacity="" stroke="" stroke-width="" stroke-opacity=""></rect>
<h1>svg事件监听绑定</h1>
<svg id="s2" width="500" height="400">
<rect id="r2" width="50" height="1" x="235" y="350" fill="#faa" fill-opacity="0.3" stroke="#800" stroke-opacity="0.3"></rect>
</svg>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>svg创建元素</h1>
<svg id="s2" width="500" height="400"></svg>
<script>
var data=[
{"label":"部门1","value":300},
{"label":"部门2","value":310},
{"label":"部门3","value":200},
{"label":"部门4","value":190},
];
for (var i=0;i<data.length;i++){
var dept = data[i];
var rect = document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute("width",50);
rect.setAttribute("height",dept.value);
rect.setAttribute("y",400-dept.value);
rect.setAttribute("x",(i*100)+50);
rect.setAttribute("fill",rc());
s2.appendChild(rect)
}
function rc() {
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
return `rgb(${r},${g},${b})`;
}
</script>
</body>
</html>
2.SVG绘图--圆形
<circle cx="" cy="" r="" fill="" fill-opacity="" stroke="" stroke-opacity=""></circle>
3.SVG绘图--椭圆
<ellipse cx="" cy="" rx="" ry=""></ellipse>
rx:水平半径
ry:垂直半径
4.SVG绘图--直线
<line x1="" y1="" x2="" y2="" stroke="" stroke-width="" stroke-linecap="round;square;butt"></line>
5.SVG绘图--折线
<polyline points="50,50 100,55 ..." stroke="#000" fill="transparent"></polyline>
6.SVG绘图--多边形
<polygon points="50,50 100,55 ..." stroke="#000" fill="transparent"></polygon>
7.SVG绘图--文本
<text font-size="" fill="" stroke="" x="" y="" alignment-baseline="before-edge">文本内容</text>
8.SVG绘图--图像
<image xlink:href="x.png" width="" height="" x="" y=""></image>
9.SVG绘图--渐变对象
<defs> 定义特效对象:渐变对象定义标签内
<linearGradient id="g3" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="red"></stop>
<stop offset="100%" stop-color="purple"></stop>
</linearGradient>
</defs>
<ANY fill="url(#g3)" stroke="url(#g3)"></ANY>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
text-align: center;
}
svg{
background: #ddd;
}
</style>
</head>07_h5_svg.html
<body>
<h1>svg创建元素</h1>
<svg id="s2" width="500" height="400">
//渐变对象属于一种特效对象
<defs>
<linearGradient id="g3" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="red"></stop>
<stop offset="17%" stop-color="orange"></stop>
<stop offset="34%" stop-color="yellow"></stop>
<stop offset="51%" stop-color="green"></stop>
<stop offset="68%" stop-color="cyan"></stop>
<stop offset="85%" stop-color="blue"></stop>
<stop offset="100%" stop-color="purple"></stop>
</linearGradient>
</defs>
<rect width="400" height="100" x="50" y="150" fill="url(#g3)"></rect>
<text font-size="60" y="300" fill="url(#g3)">一段文本</text>
</svg>
</body>
</html>
10.SVG绘图--滤镜
<defs>
<filter id="f2">
<feGaussianBlur stdDeviation="3"/>
</filter>
<ANF filter="url(#f2)">
</defs>