UNPKG

2.74 kBJavaScriptView Raw
1// See files in sparkles/*
2
3CSS.registerProperty({
4 name: '--extra-sparkleNumber',
5 syntax: '<number>',
6 inherits: false,
7 initialValue: 30,
8});
9
10CSS.registerProperty({
11 name: '--extra-sparkleHue',
12 syntax: '<number>',
13 inherits: false,
14 initialValue: 60,
15});
16
17CSS.registerProperty({
18 name: '--extra-sparkleHeightVariance',
19 syntax: '<number>',
20 inherits: false,
21 initialValue: 9,
22});
23
24CSS.registerProperty({
25 name: '--extra-sparkleWidthVariance',
26 syntax: '<number>',
27 inherits: false,
28 initialValue: 12,
29});
30
31CSS.registerProperty({
32 name: '--extra-sparkleWeightVariance',
33 syntax: '<number>',
34 inherits: false,
35 initialValue: 2,
36});
37
38(() => { const worklet = `
39const getRandom = (min, max) => {
40 return Math.floor(Math.random() * (max - min + 1)) + min
41}
42
43if (typeof registerPaint !== 'undefined') {
44 class ExtraSparkles {
45 static get inputProperties() {
46 return ['--extra-sparkleNumber', '--extra-sparkleHue', '--extra-sparkleHeightVariance', '--extra-sparkleWidthVariance', '--extra-sparkleWeightVariance']
47 }
48
49 paint(ctx, size, properties) {
50 const sparkles = properties.get('--extra-sparkleNumber')
51 const minHeight = 3
52 const maxHeight = minHeight + parseInt(properties.get('--extra-sparkleHeightVariance'))
53 const minWidth = 3
54 const maxWidth = minWidth + parseInt(properties.get('--extra-sparkleWidthVariance'))
55 const minWeight = 1
56 const maxWeight = minWeight + parseInt(properties.get('--extra-sparkleWeightVariance'))
57
58 for (let i = 0; i < sparkles; i++) {
59 const x = Math.random() * size.width
60 const y = Math.random() * (size.height - maxHeight)
61 const sparkleHeight = getRandom(minHeight, maxHeight)
62 const sparkleWidth = getRandom(minWidth, maxWidth)
63 const strokeWidth = getRandom(minWeight, maxWeight)
64
65 // Set Color
66 const hueVal = parseInt(properties.get('--extra-sparkleHue'))
67 const hue = getRandom(hueVal, hueVal + 20)
68 const sat = getRandom(90,100)
69 const light = getRandom(50,100)
70 const color = 'hsl(' + hue + 'deg,' + sat + '%,' + light + '%)'
71
72 // Set Stroke Info
73 ctx.lineWidth = strokeWidth
74 ctx.strokeStyle = color
75
76 // Paint
77 ctx.beginPath()
78 ctx.moveTo((x - sparkleWidth / 2), sparkleHeight/2 + y)
79 ctx.lineTo((x + sparkleWidth / 2), sparkleHeight/2 + y)
80 ctx.moveTo(x, 0 + y)
81 ctx.lineTo(x, sparkleHeight + y)
82 ctx.stroke()
83 }
84 }
85 }
86
87 registerPaint('extra-sparkles', ExtraSparkles)
88}`
89
90const workletBlob = URL.createObjectURL(new Blob([worklet], { type: 'application/javascript' }))
91
92window.CSS.paintWorklet.addModule(workletBlob)
93})()
\No newline at end of file