UNPKG

2.26 kBJavaScriptView Raw
1// See files in confetti/*
2
3CSS.registerProperty({
4 name: '--extra-confettiNumber',
5 syntax: '<number>',
6 inherits: false,
7 initialValue: 30,
8});
9
10CSS.registerProperty({
11 name: '--extra-confettiLengthVariance',
12 syntax: '<number>',
13 inherits: false,
14 initialValue: 15,
15});
16
17CSS.registerProperty({
18 name: '--extra-confettiWeightVariance',
19 syntax: '<number>',
20 inherits: false,
21 initialValue: 4,
22});
23
24(() => {
25 const worklet = `
26const getRandom = (min, max) => {
27 return Math.floor(Math.random() * (max - min + 1)) + min
28}
29
30if (typeof registerPaint !== 'undefined') {
31 class ExtraConfetti {
32 static get inputProperties() {
33 return ['--extra-confettiNumber','--extra-confettiLengthVariance', '--extra-confettiWeightVariance']
34 }
35
36 paint(ctx, size, properties) {
37 const confettiNum = properties.get('--extra-confettiNumber')
38 const minLength = 3
39 const maxLength = minLength + parseInt(properties.get('--extra-confettiLengthVariance'))
40 const minWeight = 1
41 const maxWeight = minWeight + parseInt(properties.get('--extra-confettiWeightVariance'))
42
43 for (var i = 0; i < confettiNum; i++) {
44 const x = Math.random() * size.width
45 const y = Math.random() * (size.height - maxLength)
46 const confettiLength = getRandom(minLength, maxLength)
47 const confettiWeight = getRandom(minWeight, maxWeight)
48
49 // Set Color
50 const hue = getRandom(0,360)
51 const sat = getRandom(90,100)
52 const light = getRandom(40,90)
53 const color = 'hsl(' + hue + 'deg,' + sat + '%,' + light + '%)'
54
55 // Set Paint Info
56 ctx.lineWidth = confettiWeight
57 ctx.strokeStyle = color
58
59 // Calculate New Position
60 const angle = getRandom(0,89)
61 const hypotenuse = confettiLength
62 const newX = x + Math.cos(angle) * hypotenuse
63 const newY = y + Math.sin(angle) * hypotenuse
64
65 // Paint
66 ctx.beginPath();
67 ctx.moveTo(x,y)
68 ctx.lineTo(newX, newY)
69 ctx.stroke()
70 }
71 }
72 }
73
74 registerPaint('extra-confetti', ExtraConfetti)
75}`
76
77const workletBlob = URL.createObjectURL(new Blob([worklet], { type: 'application/javascript' }))
78
79window.CSS.paintWorklet.addModule(workletBlob)
80})()
\No newline at end of file