forked from alampros/react-confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.ts
128 lines (107 loc) · 3.37 KB
/
Particle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { randomRange, randomInt, degreesToRads } from './utils'
import { IConfettiOptions } from './Confetti'
export enum ParticleShape {
Circle = 0,
Square,
Strip,
}
enum RotationDirection {
Positive = 1,
Negative = -1,
}
export default class Particle {
constructor(context: CanvasRenderingContext2D, getOptions: () => IConfettiOptions, x: number, y: number) {
this.getOptions = getOptions
const {
colors,
initialVelocityX,
initialVelocityY,
} = this.getOptions()
this.context = context
this.x = x
this.y = y
this.w = randomRange(5, 20)
this.h = randomRange(5, 20)
this.radius = randomRange(5, 10)
this.vx = typeof initialVelocityX === 'number' ? randomRange(-initialVelocityX, initialVelocityX) : randomRange(initialVelocityX.min, initialVelocityX.max)
this.vy = typeof initialVelocityY === 'number' ? randomRange(-initialVelocityY, 0) : randomRange(initialVelocityY.min, initialVelocityY.max)
this.shape = randomInt(0, 2)
this.angle = degreesToRads(randomRange(0, 360))
this.angularSpin = randomRange(-0.2, 0.2)
this.color = colors[Math.floor(Math.random() * colors.length)]
this.rotateY = randomRange(0, 1)
this.rotationDirection = randomRange(0, 1) ? RotationDirection.Positive : RotationDirection.Negative
}
context: CanvasRenderingContext2D
radius: number
x: number
y: number
w: number
h: number
vx: number
vy: number
shape: ParticleShape
angle: number
angularSpin: number
color: string
// Actually used as scaleY to simulate rotation cheaply
rotateY: number
rotationDirection: RotationDirection
getOptions: () => IConfettiOptions
update() {
const {
gravity,
wind,
friction,
opacity,
drawShape,
} = this.getOptions()
this.x += this.vx
this.y += this.vy
this.vy += gravity
this.vx += wind
this.vx *= friction
this.vy *= friction
if(this.rotateY >= 1 && this.rotationDirection === RotationDirection.Positive) {
this.rotationDirection = RotationDirection.Negative
} else if(this.rotateY <= -1 && this.rotationDirection === RotationDirection.Negative) {
this.rotationDirection = RotationDirection.Positive
}
const rotateDelta = 0.1 * this.rotationDirection
this.rotateY += rotateDelta
this.angle += this.angularSpin
this.context.save()
this.context.translate(this.x, this.y)
this.context.rotate(this.angle)
this.context.scale(1, this.rotateY)
this.context.rotate(this.angle)
this.context.beginPath()
this.context.fillStyle = this.color
this.context.strokeStyle = this.color
this.context.globalAlpha = opacity
this.context.lineCap = 'round'
this.context.lineWidth = 2
if(drawShape && typeof drawShape === 'function') {
drawShape.call(this, this.context)
} else {
switch(this.shape) {
case ParticleShape.Circle: {
this.context.beginPath()
this.context.arc(0, 0, this.radius, 0, 2 * Math.PI)
this.context.fill()
break
}
case ParticleShape.Square: {
this.context.fillRect(-this.w / 2, -this.h / 2, this.w, this.h)
break
}
case ParticleShape.Strip: {
this.context.fillRect(-this.w / 6, -this.h / 2, this.w / 3, this.h)
break
}
}
}
this.context.closePath()
this.context.restore()
}
}