-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimatedThreeGrowth.js
More file actions
63 lines (49 loc) · 1.32 KB
/
animatedThreeGrowth.js
File metadata and controls
63 lines (49 loc) · 1.32 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let t = 1;
let level = 0;
let treeStructure = {};
let treeStructureParams = {};
let threeTheme = {};
let branchPartitionFactor = 20;
let ctx = null;
onmessage = function(evt) {
resetGlobalParameters();
treeStructure = evt.data.treeStructure;
treeStructureParams = evt.data.treeStructureParams;
treeTheme = evt.data.treeTheme;
const canvas = evt.data.canvas;
ctx = canvas.getContext('2d');
ctx.strokeStyle = treeTheme.branchColor;
requestAnimationFrame(animateTreeGrowth);
}
function animateTreeGrowth() {
if (t == branchPartitionFactor + 1) {
t = 1;
level++;
}
if (level == treeStructureParams.deepth) {
postMessage({ isAnimationFinished: true });
return;
};
ctx.lineWidth = treeStructure[level].lineWidth;
const branches = treeStructure[level].branches;
for (let i = 0; i < branches.length; i++) {
const branchPoints = treeStructure[level].branches[i];
ctx.shadowBlur = 10;
ctx.beginPath();
ctx.moveTo(branchPoints[t - 1].x, branchPoints[t - 1].y);
ctx.lineTo(branchPoints[t].x, branchPoints[t].y);
ctx.closePath();
ctx.stroke();
}
t++;
requestAnimationFrame(animateTreeGrowth);
}
function resetGlobalParameters() {
t = 1;
level = 0;
treeStructure = {};
treeStructureParams = {};
threeTheme = {};
branchPartitionFactor = 20;
ctx = null;
}