UNPKG

1.66 kBJavaScriptView Raw
1const getRandom = (min, max) => {
2 return Math.floor(Math.random() * (max - min + 1)) + min
3}
4
5if (typeof registerPaint !== 'undefined') {
6 class ExtraConfetti {
7 static get inputProperties() {
8 return ['--extra-confettiNumber','--extra-confettiLengthVariance', '--extra-confettiWeightVariance']
9 }
10
11 paint(ctx, size, properties) {
12 const confettiNum = properties.get('--extra-confettiNumber')
13 const minLength = 3
14 const maxLength = minLength + parseInt(properties.get('--extra-confettiLengthVariance'))
15 const minWeight = 1
16 const maxWeight = minWeight + parseInt(properties.get('--extra-confettiWeightVariance'))
17
18 for (var i = 0; i < confettiNum; i++) {
19 const x = Math.random() * size.width
20 const y = Math.random() * (size.height - maxLength)
21 const confettiLength = getRandom(minLength, maxLength)
22 const confettiWeight = getRandom(minWeight, maxWeight)
23
24 // Set Color
25 const hue = getRandom(0,360)
26 const sat = getRandom(90,100)
27 const light = getRandom(40,90)
28 const color = `hsl(${hue}deg, ${sat}%, ${light}%)`
29
30 // Set Paint Info
31 ctx.lineWidth = confettiWeight
32 ctx.strokeStyle = color
33
34 // Calculate New Position
35 const angle = getRandom(0,89)
36 const hypotenuse = confettiLength
37 const newX = x + Math.cos(angle) * hypotenuse
38 const newY = y + Math.sin(angle) * hypotenuse
39
40 // Paint
41 ctx.beginPath();
42 ctx.moveTo(x,y)
43 ctx.lineTo(newX, newY)
44 ctx.stroke()
45 }
46 }
47 }
48
49 registerPaint('extra-confetti', ExtraConfetti)
50}
\No newline at end of file