1 | const getRandom = (min, max) => {
|
2 | return Math.floor(Math.random() * (max - min + 1)) + min
|
3 | }
|
4 |
|
5 | if (typeof registerPaint !== 'undefined') {
|
6 | class ExtraSparkles {
|
7 | static get inputProperties() {
|
8 | return ['--extra-sparkleNumber', '--extra-sparkleHue', '--extra-sparkleHeightVariance', '--extra-sparkleWidthVariance', '--extra-sparkleWeightVariance']
|
9 | }
|
10 |
|
11 | paint(ctx, size, properties) {
|
12 | const sparkles = properties.get('--extra-sparkleNumber')
|
13 | const minHeight = 3
|
14 | const maxHeight = minHeight + parseInt(properties.get('--extra-sparkleHeightVariance'))
|
15 | const minWidth = 3
|
16 | const maxWidth = minWidth + parseInt(properties.get('--extra-sparkleWidthVariance'))
|
17 | const minWeight = 1
|
18 | const maxWeight = minWeight + parseInt(properties.get('--extra-sparkleWeightVariance'))
|
19 |
|
20 | for (let i = 0; i < sparkles; i++) {
|
21 | const x = Math.random() * size.width
|
22 | const y = Math.random() * (size.height - maxHeight)
|
23 | const sparkleHeight = getRandom(minHeight, maxHeight)
|
24 | const sparkleWidth = getRandom(minWidth, maxWidth)
|
25 | const strokeWidth = getRandom(minWeight, maxWeight)
|
26 |
|
27 |
|
28 | const hueVal = parseInt(properties.get('--extra-sparkleHue'))
|
29 | const hue = getRandom(hueVal, hueVal + 20)
|
30 | const sat = getRandom(90,100)
|
31 | const light = getRandom(50,100)
|
32 | const color = `hsl(${hue}deg, ${sat}%, ${light}%)`
|
33 |
|
34 |
|
35 | ctx.lineWidth = strokeWidth
|
36 | ctx.strokeStyle = color
|
37 |
|
38 |
|
39 | ctx.beginPath()
|
40 | ctx.moveTo((x - sparkleWidth / 2), sparkleHeight/2 + y)
|
41 | ctx.lineTo((x + sparkleWidth / 2), sparkleHeight/2 + y)
|
42 | ctx.moveTo(x, 0 + y)
|
43 | ctx.lineTo(x, sparkleHeight + y)
|
44 | ctx.stroke()
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | registerPaint('extra-sparkles', ExtraSparkles)
|
50 | } |
\ | No newline at end of file |