From 41e7ec339723df5a307ee3ec6bbbec4c1a132284 Mon Sep 17 00:00:00 2001 From: "K. Ventouras" Date: Thu, 29 Aug 2024 17:04:48 +0300 Subject: [PATCH] example emitters follow mouse while held --- docs/examples/js/ParticleExample.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/examples/js/ParticleExample.js b/docs/examples/js/ParticleExample.js index 9bea0972..52131c08 100644 --- a/docs/examples/js/ParticleExample.js +++ b/docs/examples/js/ParticleExample.js @@ -159,6 +159,30 @@ // Center on the stage this.emitter.updateOwnerPos(window.innerWidth / 2, window.innerHeight / 2); + let isMouseDown = false; + canvas.addEventListener('mousedown', (e) => + { + if (!this.emitter) return; + + // Left click + if (e.button === 0) + { + isMouseDown = true; + this.emitter.emit = true; + this.emitter.resetPositionTracking(); + this.emitter.updateOwnerPos(e.offsetX || e.layerX, e.offsetY || e.layerY); + } + }); + + // Position the emitter to the mouse while left click is down + canvas.addEventListener('mousemove', (e) => + { + if (isMouseDown && this.emitter) + { + this.emitter.updateOwnerPos(e.offsetX || e.layerX, e.offsetY || e.layerY); + } + }); + // Click on the canvas to trigger canvas.addEventListener('mouseup', (e) => { @@ -186,9 +210,7 @@ } else { - this.emitter.emit = true; - this.emitter.resetPositionTracking(); - this.emitter.updateOwnerPos(e.offsetX || e.layerX, e.offsetY || e.layerY); + isMouseDown = false; } });