-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnowflake.js
More file actions
226 lines (194 loc) · 7.6 KB
/
snowflake.js
File metadata and controls
226 lines (194 loc) · 7.6 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import * as mt from 'lib/mersenne-twister.js';
import * as util from 'utils.js';
export class Snowflake {
constructor() {
this.version = 0.3;
this.snowFlakeWidth = 128;
this.bgFreezeLevel = 0.5;
this.fgFreezeSpeed = 0.0005;
this.diffusionSpeed = 0.01;
this.diffusionAsymmetry = 1.0;
this.rndBgNoise = 0.25;
this.rndSeed = 12345;
this.transparentBackground = true;
this.fgColor = '#D7EBFF';
this.bgColor = '#343F4D';
this.threshold = 0.65;
this.animateSteps = 50;
this.iterationsPerStep = 25;
}
randomizeValues() {
const randGen = new MersenneTwister();
this.bgFreezeLevel = 0.3 + 0.3 * randGen.random();
this.fgFreezeSpeed = 0.001 + 2.0 * randGen.random();
this.diffusionSpeed = -0.25 + 0.5 * randGen.random();
this.diffusionAsymmetry = 0.5 + randGen.random();
this.rndBgNoise = randGen.random() * 0.5;
this.rndSeed = randGen.random() * 1000000 | 0;
}
}
export const createNeighbours = (cellCount, snowFlakeWidth) => {
const neighbours = Array(6);
for(let i = 0; i < neighbours.length; i++) {
neighbours[i] = Array(cellCount);
}
for(let n = 0; n < cellCount; n++) {
neighbours[0][n] = n - snowFlakeWidth - ( snowFlakeWidth - 1 );
neighbours[1][n] = n - ( snowFlakeWidth - 1 );
neighbours[2][n] = n + snowFlakeWidth;
neighbours[3][n] = n + snowFlakeWidth + ( snowFlakeWidth - 1 );
neighbours[4][n] = n + ( snowFlakeWidth - 1 );
neighbours[5][n] = n - snowFlakeWidth;
}
return neighbours;
}
export const createSnowFlakeWithBgValue = (cellCount, bgValue, rndNoise, rndSeed) => {
const randGen = new MersenneTwister(rndSeed);
const snowflake = Array(cellCount);
for(let i = 0; i < cellCount; i++) {
snowflake[i] = bgValue + (rndNoise - 2.0 * rndNoise * randGen.random());
}
return snowflake;
}
export const initializeStartingPoint = (snowflake, cellCount) => {
snowflake[ Math.floor( cellCount / 2 ) ] = 1.0;
}
export const getSnowFlakeAsHtml = (data, width, height) => {
let snowflakeText = '';
for(let i=0;i<height;i++) {
for(let j=0;j<width;j++) {
const idx = ( width + width - 1 ) * i + j;
snowflakeText += data[idx].toFixed(2) + (j<width-1 ? ' | ' : '');
}
if(i<height -1) {
snowflakeText += "\n ";
for(let j=0;j<width-1;j++) {
const idx = ( width + width - 1 ) * i + j + width;
snowflakeText += data[idx].toFixed(2) + (j<width-2 ? ' | ' : '');
}
}
snowflakeText += "\n";
}
return snowflakeText;
}
export const isIceCell = (cellValue) => {
return cellValue < 1.0 ? false : true;
}
export const splitIntoNonReceptiveAndReceptivePart = (data, neighbours, nonReceptivePart, receptivePart, nonReceptiveTmpBuffer, cellCount, fgFreezeSpeed, diffusionSpeed, diffusionAsymmetry) => {
const tFreezeSpeed = fgFreezeSpeed / 1000.0;
const tDiffusionSpeed = diffusionSpeed / 1000.0 + 1;
for(let i = 0; i < cellCount; i++) {
nonReceptiveTmpBuffer[i] = data[i];
receptivePart[i] = 0.0;
}
for(let i = 0; i < cellCount; i++) {
let centreIsIce = isIceCell(data[i]);
if(centreIsIce) {
nonReceptivePart[i] = 0.0;
receptivePart[i] = data[i];
for(let j = 0; j < neighbours.length; j++) {
let nb = neighbours[j][i];
if( nb >= 0 && nb < cellCount) {
nonReceptiveTmpBuffer[nb] = 0.0;
receptivePart[nb] = data[nb] > 0 ? data[nb] + tFreezeSpeed: data[nb];
}
}
}
}
const cWeight = 0.5 * diffusionAsymmetry / tDiffusionSpeed;
const nbWeight = (1.0 * tDiffusionSpeed - cWeight) / 6.0;
for(let i = 0; i < cellCount; i++) {
nonReceptivePart[i] = nonReceptiveTmpBuffer[i] * cWeight;
for(let j = 0; j < neighbours.length; j++) {
let nb = neighbours[j][i];
if(nb>=0 && nb<cellCount) {
nonReceptivePart[i] += nonReceptiveTmpBuffer[nb] * nbWeight;
}
}
}
}
export const iterate = (snowflake, neighbours, nonReceptivePart, receptivePart, nonReceptiveTmpBuffer, cellCount, fgFreezeSpeed, diffusionSpeed, diffusionAsymmetry) => {
splitIntoNonReceptiveAndReceptivePart(snowflake, neighbours, nonReceptivePart, receptivePart, nonReceptiveTmpBuffer, cellCount, fgFreezeSpeed, diffusionSpeed, diffusionAsymmetry);
for(let i = 0; i < cellCount; i++) {
snowflake[i] = nonReceptivePart[i] + receptivePart[i];
}
}
let bgColorR = 52, bgColorG = 63, bgColorB = 77;
let fgColorR = 215, fgColorG = 235, fgColorB = 255;
export const setFgColor = (hexValue) => {
[fgColorR, fgColorG, fgColorB] = util.hex2rgb(hexValue);
}
export const setBgColor = (hexValue) => {
[bgColorR, bgColorG, bgColorB] = util.hex2rgb(hexValue);
}
const renderVal = (imageData, val, x, y, transparentBackground, threshold) => {
let r, g, b, a;
if(val>threshold) {
const rawIntensity = Math.min(1.0, Math.log((val - threshold + 1.0) * 2.0));
const intensity = rawIntensity * rawIntensity * rawIntensity;
const invIntensity = 1.0 - intensity;
r = intensity * fgColorR + invIntensity * bgColorR | 0;
g = intensity * fgColorG + invIntensity * bgColorG | 0;
b = intensity * fgColorB + invIntensity * bgColorB | 0;
a = transparentBackground ? (intensity * 256 | 0) : 255;
}
else {
r = bgColorR;
g = bgColorG;
b = bgColorB;
a = transparentBackground ? 0 : 255;
}
let index = (x + y * imageData.width) * 4;
imageData.data[index+0] = r;
imageData.data[index+1] = g;
imageData.data[index+2] = b;
imageData.data[index+3] = a;
}
const setVal = (imgfilterBuffer, val, x, y, width) => {
const idx = (x + y * width);
imgfilterBuffer.buffer[idx] = val;
}
const drawFilteredImage = (imgfilterBuffer, imageData, canvasWidth, canvasHeight, transparentBackground, threshold) => {
const buffer = imgfilterBuffer.buffer;
for(let y = 0; y < canvasHeight; y++) {
for(let x = 0; x < canvasWidth; x++) {
const idx = (x + y * canvasWidth);
renderVal(imageData, buffer[idx], x, y, transparentBackground, threshold);
}
}
}
export const renderSnowflake = (canvas, snowflake, snowFlakeWidth, snowFlakeHeight, cellCount, imgfilterBuffer, transparentBackground, threshold) => {
let stretchFactor = 1.5 / 1.7321;
let ctx = canvas.getContext("2d");
if(canvas.width < Math.floor(2 * snowFlakeWidth * stretchFactor)) {
canvas.width = Math.floor(2 * snowFlakeWidth * stretchFactor);
}
if(canvas.height<snowFlakeHeight) {
canvas.height = snowFlakeHeight;
}
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
imgfilterBuffer.setBufferSize(canvasWidth * canvasHeight);
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
let imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
// c0 c1 c2 | c0 (0+c3)/2 c1 (0+c4)/2 c2
// c3 c4 | c5 (c3+c8)/2 c6 (c4+c9)/2 c7
// c5 c6 c7 | c10 (c8+0)/2 c11 (c9+0)/2 c12
// c8 c9 |
// c10 c11 c12 |
for(let i = 0; i < snowFlakeHeight; i++) {
for(let j = 0; j < snowFlakeWidth; j++) {
const baseIdx = ( snowFlakeWidth + snowFlakeWidth - 1 ) * i + j;
let x = Math.trunc((2*j) * stretchFactor);
//renderVal(imageData, snowflake[baseIdx], x, i);
setVal(imgfilterBuffer, snowflake[baseIdx], x, i, canvasWidth);
x = Math.trunc((2*j+1) * stretchFactor);
const idx1 = baseIdx - snowFlakeWidth + 1;
const idx2 = baseIdx + snowFlakeWidth;
//renderVal(imageData, (snowflake[idx1] + snowflake[idx2]) * 0.5, x, i);
setVal(imgfilterBuffer, (snowflake[idx1] + snowflake[idx2]) * 0.5, x, i, canvasWidth);
}
}
drawFilteredImage(imgfilterBuffer, imageData, canvasWidth, canvasHeight, transparentBackground, threshold);
ctx.putImageData(imageData, 0, 0);
}