@@ -17,6 +17,7 @@ const U_ANGLE_RANGE = "aR";
17
17
const U_SPEED_RANGE = "sR" ;
18
18
const U_LIFE_RANGE = "lR" ;
19
19
const U_PARTICLE_COLOR = "c" ;
20
+ const U_SCALE = "s" ;
20
21
21
22
// inputs
22
23
const IN_POSITION = "p" ;
@@ -81,7 +82,7 @@ const randomRGData = (): Uint8Array => {
81
82
82
83
/** Particles simulator */
83
84
const simulate = (
84
- _canvas : HTMLCanvasElement ,
85
+ canvas : HTMLCanvasElement ,
85
86
gl : WebGL2RenderingContext ,
86
87
options : ParticlesOptions ,
87
88
) => {
@@ -205,6 +206,8 @@ const simulate = (
205
206
206
207
gl . clearColor ( 0 , 0 , 0 , 0 ) ;
207
208
209
+ gl . useProgram ( updateProgram ) ;
210
+
208
211
const rgNoiseTexture = gl . createTexture ( ) ;
209
212
gl . bindTexture ( gl . TEXTURE_2D , rgNoiseTexture ) ;
210
213
gl . activeTexture ( gl . TEXTURE0 ) ;
@@ -233,6 +236,15 @@ const simulate = (
233
236
const location = gl . getUniformLocation ( updateProgram , name ) ;
234
237
y ? gl . uniform2f ( location , x , y ) : gl . uniform1f ( location , x ) ;
235
238
} ;
239
+
240
+ setUpdateUniform ( U_ANGLE_RANGE , ...angleRange ) ;
241
+ // skipcq: JS-0339 -- set in default options
242
+ setUpdateUniform ( U_LIFE_RANGE , ...options . ageRange ! ) ;
243
+ // skipcq: JS-0339 -- set in default options
244
+ setUpdateUniform ( U_SPEED_RANGE , ...options . speedRange ! ) ;
245
+ // skipcq: JS-0339 -- forcefield is always set by the default options
246
+ setUpdateUniform ( U_FORCE_FIELD , ...options . forceField ! ) ;
247
+
236
248
let prevT = 0 ;
237
249
let bornParticles = 0 ;
238
250
let readIndex = 0 ;
@@ -257,15 +269,7 @@ const simulate = (
257
269
258
270
setUpdateUniform ( U_DT , dt / 1000 ) ;
259
271
setUpdateUniform ( U_EXTRA_RANDOM , random ( ) ) ;
260
- // skipcq: JS-0339 -- forcefield is always set by the default options
261
- setUpdateUniform ( U_FORCE_FIELD , ...options . forceField ! ) ;
262
272
setUpdateUniform ( U_ORIGIN , mouseX , mouseY ) ;
263
- setUpdateUniform ( U_ANGLE_RANGE , ...angleRange ) ;
264
- // skipcq: JS-0339 -- set in default options
265
- setUpdateUniform ( U_LIFE_RANGE , ...options . ageRange ! ) ;
266
- // skipcq: JS-0339 -- set in default options
267
- const speedRange = options . speedRange ! ;
268
- setUpdateUniform ( U_SPEED_RANGE , speedRange [ 0 ] , speedRange [ 1 ] ) ;
269
273
270
274
gl . bindVertexArray ( vertexArrayObjects [ readIndex ] ) ;
271
275
gl . bindBufferBase ( gl . TRANSFORM_FEEDBACK_BUFFER , 0 , buffers [ writeIndex ] ) ;
@@ -280,6 +284,12 @@ const simulate = (
280
284
gl . useProgram ( renderProgram ) ;
281
285
// skipcq: JS-0339 -- set in default options
282
286
gl . uniform4f ( gl . getUniformLocation ( renderProgram , U_PARTICLE_COLOR ) , ...options . rgba ! ) ;
287
+ const height = canvas . height ;
288
+ const width = canvas . width ;
289
+ gl . uniform2f (
290
+ gl . getUniformLocation ( renderProgram , U_SCALE ) ,
291
+ ...( ( height > width ? [ 1 , width / height ] : [ height / width , 1 ] ) as [ number , number ] ) ,
292
+ ) ;
283
293
gl . drawArrays ( gl . POINTS , 0 , bornParticles ) ;
284
294
[ readIndex , writeIndex ] = [ writeIndex , readIndex ] ;
285
295
@@ -327,7 +337,13 @@ export const renderParticles = (canvas: HTMLCanvasElement, options?: ParticlesOp
327
337
const target = options ?. overlay ? window : canvas ;
328
338
/** update mouse position */
329
339
const onMouseMove = ( e : MouseEvent ) => {
330
- setOrigin ( ( e . clientX / canvas . width ) * 2 - 1 , 1 - ( e . clientY / canvas . height ) * 2 ) ;
340
+ const height = canvas . height ;
341
+ const width = canvas . width ;
342
+ const scale = height > width ? [ 1 , height / width ] : [ width / height , 1 ] ;
343
+ setOrigin (
344
+ ( ( e . clientX / canvas . width ) * 2 - 1 ) * scale [ 0 ] ,
345
+ ( 1 - ( e . clientY / canvas . height ) * 2 ) * scale [ 1 ] ,
346
+ ) ;
331
347
} ;
332
348
333
349
// @ts -expect-error -- strange type-error
0 commit comments