@@ -24,6 +24,8 @@ function run_wasm(obj) {
24
24
w = obj . instance ;
25
25
memory = new Uint8Array ( w . exports . memory . buffer ) ;
26
26
27
+ let m = new Mandel ( w ) ;
28
+
27
29
test1 ( w ) ;
28
30
test2 ( w ) ;
29
31
test3 ( w ) ;
@@ -125,3 +127,110 @@ function test4(w) {
125
127
console . log ( "test4" , reverseResult , typeof ( reverseResult ) ) ;
126
128
element . innerHTML = result ;
127
129
}
130
+
131
+ class Mandel
132
+ {
133
+ constructor ( w ) {
134
+ this . buttonStartC = document . getElementById ( "btnStartC" ) ;
135
+ this . buttonStopC = document . getElementById ( "btnStopC" ) ;
136
+
137
+ this . canvasC = document . getElementById ( "canvasC" ) ;
138
+ this . w = w ;
139
+
140
+ this . ctxC = this . canvasC . getContext ( "2d" ) ;
141
+
142
+ this . canvasWidth = this . canvasC . width ;
143
+ this . canvasHeight = this . canvasC . height ;
144
+
145
+ this . boundHandleClickStartC = this . startC . bind ( this ) ;
146
+ this . boundHandleClickStopC = this . stopC . bind ( this ) ;
147
+
148
+ this . boundLoopC = this . loopC . bind ( this ) ;
149
+
150
+ this . buttonStartC . addEventListener ( "click" , this . boundHandleClickStartC ) ;
151
+ this . buttonStopC . addEventListener ( "click" , this . boundHandleClickStopC ) ;
152
+
153
+ this . buttonStopC . style . display = "none" ;
154
+
155
+ this . runC = false ;
156
+
157
+ this . startC ( ) ;
158
+ }
159
+
160
+ startC ( )
161
+ {
162
+ this . buttonStartC . style . display = "none" ;
163
+ this . buttonStopC . style . display = "block" ;
164
+ this . initMandel ( ) ;
165
+
166
+ this . ctxC . fillStyle = "rgba(10,10,10,255)" ;
167
+ this . ctxC . fillRect ( 0 , 0 , this . canvasWidth , this . canvasHeight ) ;
168
+
169
+ this . runC = true ;
170
+ requestAnimationFrame ( this . boundLoopC ) ;
171
+ }
172
+
173
+ stopC ( )
174
+ {
175
+ this . buttonStartC . style . display = "block" ;
176
+ this . buttonStopC . style . display = "none" ;
177
+ this . runC = false ;
178
+ }
179
+
180
+ initMandel ( )
181
+ {
182
+ this . minR = - 1.016104875794718741911 ;
183
+ this . maxR = - 1.016069839835083325286 ;
184
+ this . minI = 0.277341371563625000140 ;
185
+ this . maxI = 0.277367718605270833442 ;
186
+
187
+ // this.minR = -1.016097461503191826601;
188
+ // this.maxR = -1.016097461213187951076;
189
+ // this.minI = 0.277356844127615701971;
190
+ // this.maxI = 0.277356844344781394806;
191
+
192
+ this . minR = - 2 ;
193
+ this . maxR = 1 ;
194
+ this . minI = - 1 ;
195
+ this . maxI = 1 ;
196
+
197
+ this . minR = - 1.293019999999999985104 ;
198
+ this . maxR = - 1.193859999999999987472 ;
199
+ this . minI = 0.12808 ;
200
+ this . maxI = 0.20245 ;
201
+
202
+ this . minR = - 0.868819999999999995828 ;
203
+ this . maxR = - 0.858714999999999996086 ;
204
+ this . minI = 0.239415 ;
205
+ this . maxI = 0.246935 ;
206
+
207
+ this . maxIterations = 5000 ;
208
+ this . x = 0 ;
209
+ this . y = 0 ;
210
+
211
+ this . colors = new Array ( 16 ) . fill ( 0 ) . map ( ( _ , i ) => i === 0 ? '#000' : `#${ ( ( 1 << 24 ) * Math . random ( ) | 0 ) . toString ( 16 ) } ` ) ;
212
+
213
+ this . w . exports . initMandel ( this . minR , this . maxR , this . minI , this . maxI , this . maxIterations , this . canvasWidth , this . canvasHeight ) ;
214
+ }
215
+
216
+ loopC ( )
217
+ {
218
+ for ( let x = 0 ; x < this . canvasWidth ; ++ x )
219
+ {
220
+ const iterations = this . w . exports . calcPixel ( x , this . y ) ;
221
+ this . ctxC . fillStyle = this . colors [ iterations >= this . maxIterations ? 0 : ( iterations % this . colors . length - 1 ) + 1 ] ;
222
+ this . ctxC . fillRect ( x , this . y , 1 , 1 ) ;
223
+ }
224
+
225
+ this . y += 1 ;
226
+ if ( this . y >= this . canvasHeight )
227
+ {
228
+ this . stopC ( ) ;
229
+ }
230
+
231
+ if ( this . runC )
232
+ {
233
+ requestAnimationFrame ( this . boundLoopC ) ;
234
+ }
235
+ }
236
+ }
0 commit comments