Skip to content

Commit ca2b22b

Browse files
committed
fix: add rotation direction to fix jump visible when rendering asymetrical shapes
1 parent ddf5259 commit ca2b22b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Particle.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export enum ParticleShape {
77
Strip,
88
}
99

10+
enum RotationDirection {
11+
Positive,
12+
Negative,
13+
}
14+
1015
export default class Particle {
1116
constructor(context: CanvasRenderingContext2D, getOptions: () => IConfettiOptions, x: number, y: number) {
1217
this.getOptions = getOptions
@@ -24,7 +29,7 @@ export default class Particle {
2429
this.angularSpin = randomRange(-0.2, 0.2)
2530
this.color = colors[Math.floor(Math.random() * colors.length)]
2631
this.rotateY = randomRange(0, 1)
27-
this.rotateDirection = 1
32+
this.rotationDirection = RotationDirection.Positive
2833
}
2934
context: CanvasRenderingContext2D
3035
radius: number
@@ -39,6 +44,7 @@ export default class Particle {
3944
angularSpin: number
4045
color: string
4146
rotateY: number
47+
rotationDirection: RotationDirection
4248
getOptions: () => IConfettiOptions
4349

4450
update() {
@@ -55,13 +61,13 @@ export default class Particle {
5561
this.vx += wind
5662
this.vx *= friction
5763
this.vy *= friction
58-
if(this.rotateY >= 1 && this.rotateDirection === 1) {
59-
this.rotateDirection = -1
60-
} else if(this.rotateY <= -1 && this.rotateDirection === -1) {
61-
this.rotateDirection = 1
64+
if(this.rotateY >= 1 && this.rotationDirection === RotationDirection.Positive) {
65+
this.rotationDirection = RotationDirection.Negative
66+
} else if(this.rotateY <= -1 && this.rotationDirection === RotationDirection.Negative) {
67+
this.rotationDirection = RotationDirection.Positive
6268
}
6369

64-
const rotateDelta = 0.1 * this.rotateDirection
70+
const rotateDelta = 0.1 * this.rotationDirection
6571

6672
this.rotateY += rotateDelta
6773
this.angle += this.angularSpin

0 commit comments

Comments
 (0)