文章
http://www.youbaobao.xyz/datav-docs/
https://blog.csdn.net/u012468376/article/details/73350998
canvas
渲染上下文getContext() 方法是用来渲染上下文和它的绘画功能。
var canvas = document.getElementById('tutorial');var ctx = canvas.getContext('2d');
基本案例:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><canvas id="canvas" width="800" height="800"></canvas><script>const canvas = document.getElementById("canvas");const ctx = canvas.getContext("2d"); //获取canvas对象ctx.fillStyle = "red"; //填充为红色ctx.fillRect(0, 0, 50, 50); //绘制矩形//lineToctx.beginPath(); //开始绘制路径ctx.lineWidth = 1; //线条宽度ctx.strokeStyle = "blue"; //线条填充色ctx.moveTo(100, 100); //起点坐标ctx.lineTo(250, 75); //中间点坐标ctx.lineTo(300, 100); //终点坐标ctx.stroke(); //绘制线段//arcctx.beginPath();ctx.lineWidth = 2;ctx.strokeStyle = "green"; // 圆形边框色ctx.fillStyle = "red"; // 圆形填充色ctx.arc(200, 200, 50, 0, 2 * Math.PI); // 绘制圆形ctx.stroke(); // 绘制圆形的边框ctx.fill(); // 绘制圆形的填充色//绘制点ctx.beginPath();ctx.lineWidth = 1;ctx.strokeStyle = 'red';ctx.moveTo(300, 300);ctx.lineTo(301, 301); // 绘制一个点ctx.stroke();</script></body></html>
应用:图片压缩
svg
可缩放的矢量图形。
与 canvas 的区别在何处?
canvas 放大会模糊,svg 则不会。
基本案例:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><svg width="800" height="800"><rectwidth="50"height="50"style="fill: red; stroke-width: 0; stroke: rgb(0, 0, 0)"/><linex1="100"y1="100"x2="250"y2="75"style="stroke: blue; stroke-width: 1"/><linex1="250"y1="75"x2="300"y2="100"style="stroke: blue; stroke-width: 1"/><circlecx="200"cy="200"r="50"stroke="green"stroke-width="2"fill="red"/><linex1="300"y1="300"x2="301"y2="301"style="stroke: red; stroke-width: 1"/></svg></body></html>
