Skip to content

Commit 023c891

Browse files
author
Li Xinyang
committed
add canvas content
1 parent 105f561 commit 023c891

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- [样式操作](chapter3/04_style_manipulation.md)
4343
- [事件](chapter3/05_events.md)
4444
- [动画](chapter3/06_animation.md)
45+
- [Canvas](chapter3/07_canvas.md)
4546
- [页面架构]()
4647
- [产品前端架构]()
4748
- [附录 A:书单](Booklist.md)

chapter3/07_canvas.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Canvas
2+
3+
**Mozilla** 官方 `<canvas>` 教程在[这里](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial)
4+
5+
画布 `<canvas>` 的默认宽高为 300 与 150 ,但是同样可以使用 CSS 设定宽高。但因为 CSS 与 JavaScript 在渲染工程中有速度的差异,所以**不建议使用** CSS 对 `<canvas>` 的宽高做设定。
6+
7+
```html
8+
<canvas id="canvasId" width="300" height="150">
9+
</canvas>
10+
```
11+
12+
### 渲染上下文对象
13+
14+
下面的 `ctx` 即为渲染上下文对象。`globalCompositeOperation` 为对于 `canvas` 绘画时的渲染属性设置,具体表现如下图所示。
15+
16+
```Javascript
17+
var canvas = document.getElementById('canvasId');
18+
var ctx = canvas.getContext('2d');
19+
20+
21+
// 绘画 canvas 的属性
22+
ctx.globalCompositeOperation = 'destination-over';
23+
```
24+
25+
![](../img/C/canvas-global-composite.png)
26+
27+
### 绘图步骤
28+
29+
![](../img/C/canvas-drawing-steps.png)
30+
31+
一个周期就是完整的一帧的绘画过程。
32+
33+
![](../img/C/canvas-animation.gif)
34+
35+
```javascript
36+
var sun = new Image();
37+
var moon = new Image();
38+
var earth = new Image();
39+
40+
function init() {
41+
sun.src = 'Canvas_sun.png';
42+
moon.src = 'Canvas_moon.png';
43+
earth.src = 'Canvas_earth.png';
44+
window.requestAnimationFrame(draw);
45+
}
46+
47+
function draw(){
48+
var ctx = document.getElementById('canvas').getContext('2d');
49+
50+
ctx.globalCompositeOperation = 'destination-over';
51+
// 清空画布内容
52+
ctx.clearRect(0, 0, 300, 300);
53+
54+
ctx.fillStyle = 'rgba(0, 0, 0, 0.4)';
55+
ctx.strokeStyle = 'rgba(0, 153, 255, 0.4)';
56+
57+
// 保存画布状态
58+
ctx.save();
59+
ctx.translate(150, 150);
60+
61+
// 开始绘制图形
62+
63+
// 地球
64+
var time = new Date();
65+
ctx.rotate(((2*Math.PI)/60)* time.getSeconds() + ((2*Math.PI)/60000)*time.getMilliseconds());
66+
ctx.translate(105, 0);
67+
// 阴影
68+
ctx.fillRect(0, -12, 50, 24);
69+
ctx.drawImage(earth, -12, -12);
70+
71+
// 月亮
72+
ctx.rotate(((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds());
73+
ctx.translate(0, 28.5);
74+
ctx.drawInmage(moon, -3.5, -3.5);
75+
76+
// 恢复画布状态
77+
ctx.restore();
78+
79+
ctx.beginPath();
80+
ctx.arc(150, 150, 105, 0, Math.PI*2, false);
81+
82+
ctx.stroke();
83+
84+
ctx.drawImage(sun, 0, 0, 300, 300);
85+
86+
window.requestAnimationFrame(draw);
87+
}
88+
89+
init();
90+
```

img/C/canvas-animation.gif

228 KB
Loading

img/C/canvas-drawing-steps.png

64.5 KB
Loading

img/C/canvas-global-composite.png

91.4 KB
Loading

0 commit comments

Comments
 (0)