程序运行截图如下:
代码如下:
- <!DOCTYPE html>
- <html>
- <head>
- </head>
-
- <body>
- <svg width="https://cdn.jxasp.com:9143/image/200" height="https://cdn.jxasp.com:9143/image/200">
- <rect x="0" y="0" width="100" height="100" fill="red"></rect>
- <line x1="100" y1="0" x2="https://cdn.jxasp.com:9143/image/200" y2="100" stroke="green" stroke-width="1"></line>
- <polygon points="0,100 0,200 100,200" fill="red"></polygon>
- <circle cx="150" cy="150" r="50" stroke-width="1" stroke="green" fill="red"></circle>
- </svg>
- </body>
-
- </html>
svg是矢量绘图,和canvas一样是一个标签。
浏览器中只有Dom元素才能被选中。
svg是通过svg标签来绘图。
canvas通过JavaScript API来绘图。
canvas绘制的图是不能被选中的。
svg是可以被选中的,每一个元素都是个dom。
Svg不管放多大,都可以保持在可升缩的范围内。而canvas放大就锯齿了(可以对其进行抗锯齿)。
Canvas适合做动画。
Svg适合做展示相关的。