Skip to content

Commit 8cfcc50

Browse files
committed
fix: improved cannon options loading
1 parent 5d55e8f commit 8cfcc50

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

interactions/external/cannon/src/Cannoner.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ interface CannonGesture {
8989
export class Cannoner extends ExternalInteractorBase<CannonContainer> {
9090
readonly maxDistance = 0;
9191

92+
private _data?: CannonData;
9293
private _gesture: CannonGesture = {
9394
origin: Vector.origin,
9495
current: Vector.origin,
@@ -108,7 +109,18 @@ export class Cannoner extends ExternalInteractorBase<CannonContainer> {
108109
}
109110

110111
init(): void {
111-
// nothing to initialize
112+
const options = this.container.actualOptions.interactivity?.modes.cannon ?? new Cannon();
113+
114+
this._data = {
115+
spread: degToRad(options.spread),
116+
maxDragDistance: options.maxDragDistance,
117+
velocityFactor: options.velocityFactor,
118+
particleFactor: options.particleFactor,
119+
minParticles: options.minParticles,
120+
maxParticles: options.maxParticles,
121+
drawVector: options.drawVector,
122+
vectorColor: options.vectorColor,
123+
};
112124
}
113125

114126
interact(interactivityData: IInteractivityData, _delta: IDelta): void {
@@ -178,37 +190,23 @@ export class Cannoner extends ExternalInteractorBase<CannonContainer> {
178190
}
179191

180192
// ── Private helpers ────────────────────────────────────────────────────────
181-
/**
182-
* Resolves cannon options with defaults.
183-
* @returns -
184-
*/
185-
private _cannonOptions(): CannonData {
186-
const raw = (this.container.actualOptions.interactivity?.modes as CannonMode | undefined)?.cannon ?? new Cannon();
187-
188-
return {
189-
spread: degToRad(raw.spread),
190-
maxDragDistance: raw.maxDragDistance * this.container.retina.pixelRatio,
191-
velocityFactor: raw.velocityFactor,
192-
particleFactor: raw.particleFactor,
193-
minParticles: raw.minParticles,
194-
maxParticles: raw.maxParticles,
195-
drawVector: raw.drawVector,
196-
vectorColor: raw.vectorColor,
197-
};
198-
}
199-
200193
/**
201194
* Draws the aiming line and power circle on the canvas.
202195
* Rendered directly on the container canvas context.
203196
*/
204197
private _drawVector(): void {
205198
this.container.canvas.render.draw(ctx => {
206-
const opts = this._cannonOptions(),
207-
{ origin, current } = this._gesture,
199+
const opts = this._data;
200+
201+
if (!opts) {
202+
return;
203+
}
204+
205+
const { origin, current } = this._gesture,
208206
pxRatio = this.container.retina.pixelRatio,
209207
dragDist = getDistance(origin, current),
210208
// Clamp to maxDragDistance so visual feedback matches actual force
211-
clampedDist = Math.min(dragDist, opts.maxDragDistance),
209+
clampedDist = Math.min(dragDist, opts.maxDragDistance * pxRatio),
212210
clampRatio = dragDist > minDistance ? clampedDist / dragDist : minDistance,
213211
clampedX = origin.x + (current.x - origin.x) * clampRatio,
214212
clampedY = origin.y + (current.y - origin.y) * clampRatio;
@@ -237,17 +235,22 @@ export class Cannoner extends ExternalInteractorBase<CannonContainer> {
237235
* Fires a burst of particles based on the completed drag gesture.
238236
*/
239237
private _fire(): void {
240-
const opts = this._cannonOptions(),
241-
{ origin, current } = this._gesture,
242-
dragLength = Math.min(getDistance(origin, current), opts.maxDragDistance);
238+
const opts = this._data;
239+
240+
if (!opts) {
241+
return;
242+
}
243+
244+
const { origin, current } = this._gesture,
245+
pxRatio = this.container.retina.pixelRatio,
246+
dragLength = Math.min(getDistance(origin, current), opts.maxDragDistance * pxRatio);
243247

244248
if (dragLength < minTapsLength) {
245249
// Ignore accidental taps
246250
return;
247251
}
248252

249-
const pxRatio = this.container.retina.pixelRatio,
250-
pxRatioFactor = identity / pxRatio,
253+
const pxRatioFactor = identity / pxRatio,
251254
// Reverse the drag vector to get launch angle (already in radians)
252255
launchAngle = angleRad(current.x, current.y, origin.x, origin.y),
253256
velocity = dragLength * pxRatioFactor * opts.velocityFactor,

0 commit comments

Comments
 (0)