简单用法
1 |
|
详细用法
https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial
注意事项
-
即用 Chrome 打开带canvas的页面,切到其他 tab 上,再切换到另一个tab,确保超过3个其他tab; 最后切回带canvas的tab,会发现 canvas 内容变白屏了;
注: 虽然看起来是白屏,但通过getImageData从canvas中读取出来的数据其实是对的(非白屏);
应用
1. 视频播放
ps: 通过该方法播放视频也可以增加各式各样的字幕
1 |
|
2. 时钟
1 |
|
PS:上面是有刻度版本,记录一个无刻度版本,较为简洁
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
<html>
<head>
<meta charset="UTF-8" />
<title>Canvas Clock</title>
<style>
#clockCanvas {
display: block;
margin: 0 auto;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<canvas id="clockCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('clockCanvas');
const ctx = canvas.getContext('2d');
const radius = canvas.height / 2;
ctx.translate(radius, radius);
const clockRadius = radius * 0.9;
function drawClock() {
drawFace(ctx, clockRadius);
drawNumbers(ctx, clockRadius);
drawTime(ctx, clockRadius);
}
function drawFace(ctx, radius) {
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
ctx.strokeStyle = '#333';
ctx.lineWidth = radius * 0.1;
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
}
function drawNumbers(ctx, radius) {
let ang;
let num;
ctx.font = radius * 0.15 + 'px arial';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
for (num = 1; num <= 12; num++) {
ang = (num * Math.PI) / 6;
ctx.rotate(ang);
ctx.translate(0, -radius * 0.85);
ctx.rotate(-ang);
ctx.fillText(num.toString(), 0, 0);
ctx.rotate(ang);
ctx.translate(0, radius * 0.85);
ctx.rotate(-ang);
}
}
function drawTime(ctx, radius) {
const now = new Date();
const hour = now.getHours();
const minute = now.getMinutes();
const second = now.getSeconds();
// Hour
const hourPos = ((hour % 12) * Math.PI) / 6 + (minute * Math.PI) / 360;
drawHand(ctx, hourPos, radius * 0.5, radius * 0.07);
// Minute
const minutePos = (minute * Math.PI) / 30 + (second * Math.PI) / 1800;
drawHand(ctx, minutePos, radius * 0.8, radius * 0.07);
// Second
const secondPos = (second * Math.PI) / 30;
drawHand(ctx, secondPos, radius * 0.9, radius * 0.02);
}
function drawHand(ctx, pos, length, width) {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = 'round';
ctx.moveTo(0, 0);
ctx.rotate(pos);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-pos);
}
function updateClock() {
ctx.clearRect(-radius, -radius, canvas.width, canvas.height);
drawClock();
}
setInterval(updateClock, 1000);
drawClock();
</script>
</body>
</html>
3. 刮刮卡
1 |
|
4. 画板
以前的关于这个的文章:https://blog.csdn.net/ion_L/article/details/83020855
这里对原来的一些问题做下优化
1 |
|
5.随机验证码
1 |
|
6. 图片压缩
这里只是一个demo,实际生产环境中不建议使用canvas这种全损压缩方案,其存在压缩可能不减反增情况。生产环境最好还是通过买 tinyPNG 的服务进行后端压缩。(可见:https://segmentfault.com/a/1190000023486410)
1 | <html> |
图片压缩也存在第三方库 compressorjs,更多图片处理推荐看看阿宝哥的 图片处理不用愁,给你十个小帮手
7. canvas生成自定义图片
通常用于自动生成用户头像,如钉钉注册后默认的头像
1 |
|
8. 粒子碰撞
9. canvas特效或游戏开发
下面的代码来自TechbrooD.com, 领先的沉浸式互联网内容门户,一站式学习、创作和展示。
HTML5 Canvas五彩纸屑粒子动画特效
1 |
|
HTML5 Canvas可交互的粒子碰撞实验
1 |
|
HTML5 Canvas可在线定制的9类粒子爆发动画
1 |
|
HTML5 Canvas炫彩粒子特效生成器
1 |
|
HTML5 Canvas图像粒子化渐显动画特效
1 |
|
HTML5 Canvas粉尘态粒子引力效应动画特效
1 |
|
nanogl逼真的3D冰凌雪花模型和漫天飞雪特效
1 |
|
本文链接: http://www.ionluo.cn/blog/posts/78bd14cd.html
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!