UNPKG

1.68 kBJavaScriptView Raw
1// See files in superUnderline/*
2
3CSS.registerProperty({
4 name: '--extra-underlineNumber',
5 syntax: '<number>',
6 inherits: false,
7 initialValue: 3
8});
9
10CSS.registerProperty({
11 name: '--extra-underlineColor',
12 syntax: '<color>',
13 inherits: true,
14 initialValue: 'black'
15});
16
17CSS.registerProperty({
18 name: '--extra-underlineSpread',
19 syntax: '<number>',
20 inherits: false,
21 initialValue: 20
22});
23
24CSS.registerProperty({
25 name: '--extra-underlineWidth',
26 syntax: '<number>',
27 inherits: false,
28 initialValue: 2
29});
30
31const worklet = `
32const getRandom = (min, max) => {
33 return Math.floor(Math.random() * (max - min + 1)) + min
34}
35
36if (typeof registerPaint !== 'undefined') {
37 class SuperUnderline {
38 static get inputProperties() {
39 return ['--extra-underlineNumber', '--extra-underlineColor', '--extra-underlineSpread', '--extra-underlineWidth']
40 }
41
42 paint(ctx, size, properties) {
43 const numUnderlines = properties.get('--extra-underlineNumber')
44 const lineWidth = properties.get('--extra-underlineWidth')
45 const color = properties.get('--extra-underlineColor')
46 const spread = properties.get('--extra-underlineSpread')
47
48 ctx.lineWidth = lineWidth
49 ctx.strokeStyle = color
50
51 for (let i = 0; i < numUnderlines; i++) {
52 ctx.beginPath()
53 ctx.moveTo(0, getRandom(0, spread) + size.height/1.4)
54 ctx.lineTo(size.width, getRandom(0, spread) + size.height/1.4)
55 ctx.stroke()
56 }
57 }
58 }
59
60 registerPaint('extra-superUnderline', SuperUnderline)
61}`
62
63const workletBlob = URL.createObjectURL(new Blob([worklet], { type: 'application/javascript' }))
64
65window.CSS.paintWorklet.addModule(workletBlob)
\No newline at end of file