文章
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); //绘制矩形
//lineTo
ctx.beginPath(); //开始绘制路径
ctx.lineWidth = 1; //线条宽度
ctx.strokeStyle = "blue"; //线条填充色
ctx.moveTo(100, 100); //起点坐标
ctx.lineTo(250, 75); //中间点坐标
ctx.lineTo(300, 100); //终点坐标
ctx.stroke(); //绘制线段
//arc
ctx.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">
<rect
width="50"
height="50"
style="fill: red; stroke-width: 0; stroke: rgb(0, 0, 0)"
/>
<line
x1="100"
y1="100"
x2="250"
y2="75"
style="stroke: blue; stroke-width: 1"
/>
<line
x1="250"
y1="75"
x2="300"
y2="100"
style="stroke: blue; stroke-width: 1"
/>
<circle
cx="200"
cy="200"
r="50"
stroke="green"
stroke-width="2"
fill="red"
/>
<line
x1="300"
y1="300"
x2="301"
y2="301"
style="stroke: red; stroke-width: 1"
/>
</svg>
</body>
</html>