@@ -17,6 +17,7 @@ const U_ANGLE_RANGE = "aR";
1717const U_SPEED_RANGE = "sR" ;
1818const U_LIFE_RANGE = "lR" ;
1919const U_PARTICLE_COLOR = "c" ;
20+ const U_SCALE = "s" ;
2021
2122// inputs
2223const IN_POSITION = "p" ;
@@ -81,7 +82,7 @@ const randomRGData = (): Uint8Array => {
8182
8283/** Particles simulator */
8384const simulate = (
84- _canvas : HTMLCanvasElement ,
85+ canvas : HTMLCanvasElement ,
8586 gl : WebGL2RenderingContext ,
8687 options : ParticlesOptions ,
8788) => {
@@ -205,6 +206,8 @@ const simulate = (
205206
206207 gl . clearColor ( 0 , 0 , 0 , 0 ) ;
207208
209+ gl . useProgram ( updateProgram ) ;
210+
208211 const rgNoiseTexture = gl . createTexture ( ) ;
209212 gl . bindTexture ( gl . TEXTURE_2D , rgNoiseTexture ) ;
210213 gl . activeTexture ( gl . TEXTURE0 ) ;
@@ -233,6 +236,15 @@ const simulate = (
233236 const location = gl . getUniformLocation ( updateProgram , name ) ;
234237 y ? gl . uniform2f ( location , x , y ) : gl . uniform1f ( location , x ) ;
235238 } ;
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+
236248 let prevT = 0 ;
237249 let bornParticles = 0 ;
238250 let readIndex = 0 ;
@@ -257,15 +269,7 @@ const simulate = (
257269
258270 setUpdateUniform ( U_DT , dt / 1000 ) ;
259271 setUpdateUniform ( U_EXTRA_RANDOM , random ( ) ) ;
260- // skipcq: JS-0339 -- forcefield is always set by the default options
261- setUpdateUniform ( U_FORCE_FIELD , ...options . forceField ! ) ;
262272 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 ] ) ;
269273
270274 gl . bindVertexArray ( vertexArrayObjects [ readIndex ] ) ;
271275 gl . bindBufferBase ( gl . TRANSFORM_FEEDBACK_BUFFER , 0 , buffers [ writeIndex ] ) ;
@@ -280,6 +284,12 @@ const simulate = (
280284 gl . useProgram ( renderProgram ) ;
281285 // skipcq: JS-0339 -- set in default options
282286 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+ ) ;
283293 gl . drawArrays ( gl . POINTS , 0 , bornParticles ) ;
284294 [ readIndex , writeIndex ] = [ writeIndex , readIndex ] ;
285295
@@ -327,7 +337,13 @@ export const renderParticles = (canvas: HTMLCanvasElement, options?: ParticlesOp
327337 const target = options ?. overlay ? window : canvas ;
328338 /** update mouse position */
329339 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+ ) ;
331347 } ;
332348
333349 // @ts -expect-error -- strange type-error
0 commit comments