forked from trpomoais2018/fg01-snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (40 loc) · 1.08 KB
/
Copy pathscript.js
File metadata and controls
42 lines (40 loc) · 1.08 KB
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
var canvas = document.createElement('canvas');
document.body.insertBefore(canvas, document.body.firstChild);
canvas.style.position = 'fixed';
canvas.style.top = '0px';
canvas.style.left = '0px';
canvas.width = document.body.offsetWidth;
canvas.height = window.innerHeight;
var sky = canvas.getContext('2d');
var radiansPerDegrees = Math.PI / 180;
function edge(n, length) {
sky.save();
if (n === 0) {
sky.lineTo(length, 0);
} else {
sky.scale(1 / 3, 1 / 3);
edge(n - 1, length);
sky.rotate(60 * radiansPerDegrees);
edge(n - 1, length);
sky.rotate(-120 * radiansPerDegrees);
edge(n - 1, length);
sky.rotate(60 * radiansPerDegrees);
edge(n - 1, length);
}
sky.restore();
sky.translate(length, 0);
}
function drawFlake(x, y, length, n, stroke) {
sky.save();
sky.strokeStyle = stroke;
sky.translate(x, y);
sky.moveTo(0, 0);
edge(n, length);
sky.rotate(-120 * radiansPerDegrees);
edge(n, length);
sky.rotate(-120 * radiansPerDegrees);
edge(n, length);
sky.stroke();
sky.restore();
}
drawFlake(400, 400, 200, 4, "#FF0000");