-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (133 loc) · 3.9 KB
/
script.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
let dataStructure = [];
const sliderInput = document.getElementById("fire-slider");
let fireSource = parseInt(sliderInput.value);
sliderInput.onmouseup = e => updateFireSource(e.target.value);
sliderInput.ontouchend = e => updateFireSource(e.target.value);
const decayRange = 3;
const windRange = 2;
const canvas = document.getElementById("fire-canvas");
const isMobileRes = () => window.screen.width < 960;
const fireWidth = isMobileRes() ? 100 : 180;
const fireHeight = 80;
const pixelSize = 3;
canvas.width = fireWidth * pixelSize;
canvas.height = fireHeight * pixelSize;
const widthPixels = fireWidth * pixelSize;
const heightPixels = fireHeight * pixelSize;
const stopButton = document.getElementById("fire-stop");
stopButton.onclick = () => updateFireSource(0);
const startButton = document.getElementById("fire-start");
startButton.onclick = () => updateFireSource(36);
const decreaseButton = document.getElementById("fire-decrease");
decreaseButton.onclick = () => updateFireSource(parseInt(fireSource) - 1);
const increaseButton = document.getElementById("fire-increase");
increaseButton.onclick = () => updateFireSource(parseInt(fireSource) + 1);
const fireColorsPalette = [
"rgb(7,7,7)",
"rgb(31,7,7)",
"rgb(47,15,7)",
"rgb(71,15,7)",
"rgb(87,23,7)",
"rgb(103,31,7)",
"rgb(119,31,7)",
"rgb(143,39,7)",
"rgb(159,47,7)",
"rgb(175,63,7)",
"rgb(191,71,7)",
"rgb(199,71,7)",
"rgb(223,79,7)",
"rgb(223,87,7)",
"rgb(223,87,7)",
"rgb(215,95,7)",
"rgb(215,95,7)",
"rgb(215,103,15)",
"rgb(207,111,15)",
"rgb(207,119,15)",
"rgb(207,127,15)",
"rgb(207,135,23)",
"rgb(199,135,23)",
"rgb(199,143,23)",
"rgb(199,151,31)",
"rgb(191,159,31)",
"rgb(191,159,31)",
"rgb(191,167,39)",
"rgb(191,167,39)",
"rgb(191,175,47)",
"rgb(183,175,47)",
"rgb(183,183,47)",
"rgb(183,183,55)",
"rgb(207,207,111)",
"rgb(223,223,159)",
"rgb(239,239,199)",
"rgb(255,255,255)"
];
const start = () => {
createFireDataStructure();
createFireSource();
calculateFirePropagation();
renderFire();
setInterval(updateFire, 33);
};
const createFireDataStructure = () => {
const aH = [...Array(fireHeight)];
const rows = aH.map((row, index) => (row = { index }));
const aW = [...Array(fireWidth)];
const columns = aW.map((column, index) => (column = { index }));
dataStructure = [
...rows.map((row, rowIndex) =>
columns.map(
(cell, columnIndex) =>
(cell = { index: columnIndex + rowIndex * fireWidth, intensity: 0 })
)
)
];
};
const createFireSource = () => {
const firstRow = dataStructure[0];
const setFire = firstRow.map(
cell => (cell = { ...cell, intensity: fireSource })
);
dataStructure[0] = setFire;
};
const updateFireSource = intensity => {
console.log(intensity);
if (intensity < 0 || intensity > 36) return null;
fireSource = intensity;
sliderInput.value = fireSource;
createFireSource();
};
const calculateFirePropagation = () => {
dataStructure.map((row, rowIndex) => {
row.map((cell, columnIndex) => {
const decay = Math.floor(Math.random() * decayRange);
if (rowIndex === 0 || columnIndex < 0) return null;
const newIntensity =
dataStructure[rowIndex - 1][columnIndex].intensity - decay;
newIntensity >= 0 &&
columnIndex - Math.floor(Math.random() * windRange) > 0
? (dataStructure[rowIndex][
columnIndex - Math.floor(Math.random() * windRange)
].intensity = newIntensity)
: null;
});
});
};
const renderFire = () => {
dataStructure.map((nested, rowIndex) => {
nested.map((_, columnIndex) => {
ctx = canvas.getContext("2d");
ctx.fillStyle = fireColorsPalette[_.intensity];
ctx.fillRect(
widthPixels - columnIndex * pixelSize,
heightPixels - rowIndex * pixelSize,
pixelSize,
pixelSize
);
});
});
};
const updateFire = () => {
calculateFirePropagation();
renderFire();
};
start();