0%

Canvas 学习(时钟 绘图板)

canvas画圆

  • 为什么需要用canvas,如果用onmousemove,每一次移动都会进行DOM重绘,非常消耗性能,这时我们需要看一下 canvas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div id="canvas"></div>
<script>
canvas.onmousemove = (e) => {
let div = document.createElement("div");
div.style.position = "absolute";
div.style.left = e.clientX + "px";
div.style.top = e.clientY + "px";
div.style.width = "6px";
div.style.height = "6px";
div.style.marginLeft = "-3px";
div.style.marginRight = "-3px";
div.style.borderRadius = "50%";
div.style.backgroundColor = "black";
canvas.appendChild(div);
}
</script>
  • 我们现在已经给 canvas 设置一屏宽高,但还是出现滚动条,原因是 canvas 是 inline 元素,设置宽高不起作用,所以需要把它转换成块级元素(不要这样做,这样做会拉伸元素
1
2
3
4
5
6
7
8
9
<canvas id="canvas"></canvas>
<style>
#canvas {
height: 100vh;
width: 100vw;
border: 1px solid red;
/* display: block; */
}
</style>
  • <canvas> 标签只有两个属性—— widthheight,可以使用 JS 设置canvas 宽高,这里不能直接给 canvas 设置 height: 100vh; width: 100vw;
1
2
3
let canvas = document.getElementById("canvas");
canvas.width = document.documentElement.clientWidth;
canvas.height = document.documentElement.clientHeight;
  • canvas画圆
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let painting = false;
let ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.strokeStyle = "none";

canvas.onmousedown = (e) => {
painting = true;
}
canvas.onmousemove = (e) => {
if (painting === true) {
ctx.beginPath();
ctx.arc(e.clientX, e.clientY, 10, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill()
}
}
canvas.onmouseup = () => {
painting = false;
}

Canvas

颜色、样式和阴影

属性 描述
fillStyle 设置或返回用于填充绘画的颜色、渐变或模式
strokeStyle 设置或返回用于笔触的颜色、渐变或模式
shadowColor 设置或返回用于阴影的颜色
shadowBlur 设置或返回用于阴影的模糊级别
shadowOffsetX 设置或返回阴影距形状的水平距离
shadowOffsetY 设置或返回阴影距形状的垂直距离

线条样式

属性 描述
lineCap 设置或返回线条的结束端点样式
lineJoin 设置或返回两条线相交时,所创建的拐角类型
lineWidth 设置或返回当前的线条宽度
miterLimit 设置或返回最大斜接长度

矩形

方法 描述
rect() 创建矩形
fillRect() 绘制“被填充”的矩形
strokeRect() 绘制矩形(无填充)
clearRect() 在给定的矩形内清除指定的像素

路径

方法 描述
fill() 填充当前绘图(路径)
stroke() 绘制已定义的路径
beginPath() 起始一条路径,或重置当前路径
moveTo() 把路径移动到画布中的指定点,不创建线条
closePath() 创建从当前点回到起始点的路径
lineTo() 添加一个新点,然后在画布中创建从该点到最后指定点的线条
clip() 从原始画布剪切任意形状和尺寸的区域
quadraticCurveTo() 创建二次贝塞尔曲线
bezierCurveTo() 创建三次方贝塞尔曲线
arc() 创建弧/曲线(用于创建圆形或部分圆)
arcTo() 创建两切线之间的弧/曲线
isPointInPath() 如果指定的点位于当前路径中,则返回 true,否则返回 false

转换

方法 描述
scale() 缩放当前绘图至更大或更小
rotate() 旋转当前绘图
translate() 重新映射画布上的 (0,0) 位置
transform() 替换绘图的当前转换矩阵
setTransform() 将当前转换重置为单位矩阵。然后运行 transform()

文本

属性 描述
font 设置或返回文本内容的当前字体属性
textAlign 设置或返回文本内容的当前对齐方式
textBaseline 设置或返回在绘制文本时使用的当前文本基线

图像绘制

方法 描述
drawImage() 向画布上绘制图像、画布或视频

像素操作

属性 描述
width 返回 ImageData 对象的宽度
height 返回 ImageData 对象的高度
data 返回一个对象,其包含指定的 ImageData 对象的图像数据

Canvas练习

绘制圆形和文本

  • 绘制线段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ctx.beginPath();
// 设置绘制的起始点
ctx.moveTo(50, 50);
// 设置经过某个位置
ctx.lineTo(50, 300);
ctx.lineTo(300, 100);
// 设置绘制的结束点
ctx.closePath();

// 起始路径的线段边缘设置为圆角
ctx.lineCap = "round";
// 转折处的线段设置为圆角
ctx.lineJoin = "round"
// 设置颜色样式
ctx.strokeStyle = "aqua";
ctx.lineWidth = 20;
ctx.stroke()
  • 绘制圆
1
2
3
4
5
6
// 默认为false,顺时针,true逆时针
// ctx,arc(x, y, radius, startAngle, endAngle, anticlockwise)
ctx.arc(300, 300, 100, 0, 2 * Math.PI, true);
ctx.fillStyle = "bisque";
ctx.fill();
ctx.stroke();
  • 弹幕字体
1
2
3
4
5
6
7
8
9
10
11
ctx.font = "50px sans-serif"
var x = 1200;
setInterval(function () {
ctx.clearRect(0, 0, 1200, 1200);
x -= 10;
if (x <= 0) {
x = 1200;
}
ctx.fillText("hello", x, 100);
ctx.strokeText("world", x, 200);
}, 40)

绘制图形

  • 绘制图像(同一图片)
1
2
3
4
5
6
7
8
9
10
var img = new Image();
img.src = "./imgs/2.jpg";
img.onload = function () {
ctx.drawImage(img, 50, 100, 480, 270);
}
var img2 = new Image();
img2.src = "./imgs/3.jpg";
img2.onload = function () {
ctx.drawImage(img2, 400, 400, 480, 270, 100, 400, 480, 270);
}
  • 绘制图像(视频抽帧)
1
2
3
4
5
6
7
8
9
10
var video = document.querySelector("video");
var interval = null;
video.onplay = function () {
interval = setInterval(function () {
ctx.drawImage(video, 0, 800, 800, 400)
}, 32)
}
video.onpause = function () {
clearInterval(interval);
}

绘制时钟

ES6版本参考这个(附带效果):https://github.com/llwodexue/clock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let canvas1 = document.querySelector("#canvas1");
let ctx = canvas1.getContext("2d");
ctx.clearRect(0, 0, 800, 600);
ctx.save();
ctx.translate(400, 300);
ctx.rotate(-Math.PI / 2);
// 绘制表盘
ctx.beginPath();
ctx.arc(0, 0, 200, 0, 2 * Math.PI);
ctx.strokeStyle = "darkgrey";
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();

function renderClock() {
ctx.clearRect(0, 0, 800, 600);
ctx.save();
ctx.translate(400, 300);
ctx.rotate(-Math.PI / 2);
// 绘制表盘
ctx.beginPath();
ctx.arc(0, 0, 200, 0, 2 * Math.PI);
ctx.strokeStyle = "darkgrey";
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();

function drawLine({ count, start, end, width, color }) {
let i = 0;
for (; i < count; i++) {
ctx.beginPath();
ctx.rotate((2 * Math.PI) / count);
ctx.moveTo(start, 0);
ctx.lineTo(end, 0);
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.stroke();
ctx.closePath();
}
}
// 绘制分针刻度线
drawLine({
count: 60,
start: 180,
end: 190,
width: 2,
color: "orangered",
});
// 绘制时针刻度线
drawLine({
count: 12,
start: 180,
end: 200,
width: 10,
color: "darkgrey",
});

let time = new Date();
let min = time.getMinutes();
let sec = time.getSeconds();
let hour = time.getHours();
hour = hour > 12 ? hour - 12 : hour;
// 绘制秒针
ctx.save();
ctx.beginPath();
ctx.rotate(((2 * Math.PI) / 60) * sec);
ctx.moveTo(-30, 0);
ctx.lineTo(170, 0);
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
ctx.stroke();
ctx.closePath();
ctx.restore();
// 绘制分针
ctx.save();
ctx.beginPath();
ctx.rotate(((2 * Math.PI) / 60) * min + ((2 * Math.PI) / 3600) * sec);
ctx.moveTo(-20, 0);
ctx.lineTo(150, 0);
ctx.lineWidth = 4;
ctx.strokeStyle = "darkblue";
ctx.stroke();
ctx.closePath();
ctx.restore();
// 绘制时针
ctx.save();
ctx.beginPath();
ctx.rotate(
((2 * Math.PI) / 12) * hour +
((2 * Math.PI) / 12 / 60) * min +
((2 * Math.PI) / 12 / 60 / 60) * sec
);
ctx.moveTo(-10, 0);
ctx.lineTo(130, 0);
ctx.lineWidth = 6;
ctx.strokeStyle = "darkgrey";
ctx.stroke();
ctx.closePath();
ctx.restore();
// 绘制表圈
ctx.beginPath();
ctx.arc(0, 0, 10, 0, 2 * Math.PI);
ctx.fillStyle = "deepskyblue";
ctx.fill();
ctx.closePath();
ctx.restore();
}
setInterval(function () {
renderClock();
}, 1000);

Canvas 绘图板

绘图板参考(附带效果): https://github.com/llwodexue/canvas_painting

参考

Fira Code | 为写程序而生的字体

Canvas教程

Detecting touch screen devices with Javascript

HTML 5 Canvas 参考手册