UNPKG

3 kBJavaScriptView Raw
1// See files in scallopedBorder/*
2
3CSS.registerProperty({
4 name: '--extra-scallopRadius',
5 syntax: '<number>',
6 inherits: false,
7 initialValue: 10
8});
9
10CSS.registerProperty({
11 name: '--extra-scallopColor',
12 syntax: '<color>',
13 inherits: true,
14 initialValue: 'black'
15});
16
17CSS.registerProperty({
18 name: '--extra-scallopWeight',
19 syntax: '<number>',
20 inherits: false,
21 initialValue: 2
22});
23
24const worklet = `
25if (typeof registerPaint !== 'undefined') {
26 class ScallopedBorder {
27 static get inputProperties() {
28 return ['--extra-scallopRadius', '--extra-scallopWeight', '--extra-scallopColor']
29 }
30
31 paint(ctx, size, properties) {
32 const radius = properties.get('--extra-scallopRadius').value
33 const scallopWeight = properties.get('--extra-scallopWeight')
34 const color = properties.get('--extra-scallopColor')
35 const height = size.height
36 const width = size.width
37
38 ctx.lineWidth = scallopWeight
39 ctx.strokeStyle = color
40
41 const getSteps = (sizeVal) => {
42 return Math.floor(sizeVal / (radius * 2) - 2)
43 }
44
45 const getOwnRadius = (sizeVal, otherRad) => {
46 const steps = getSteps(sizeVal) + 1
47 const totalSpace = sizeVal - (radius * 2)
48 const spaceTaken = steps * (radius * 2)
49 let pixelsRemaining = totalSpace - spaceTaken
50
51 if (otherRad) {
52 const radDif = otherRad - radius
53 pixelsRemaining = totalSpace - spaceTaken - radDif
54 }
55
56 const newRadius = radius + ((pixelsRemaining / 2) / (steps + 1))
57 return (newRadius)
58 }
59
60 const horizRadius = getOwnRadius(width, getOwnRadius(height))
61 const vertRadius = getOwnRadius(height, getOwnRadius(width))
62
63 // top
64 for (let i = 0; i <= getSteps(width); i++) {
65 ctx.beginPath()
66 ctx.arc(horizRadius + horizRadius + (horizRadius * i * 2), horizRadius + (scallopWeight * 1) , horizRadius, 0, Math.PI, true)
67 ctx.stroke()
68 }
69
70 // bottom
71 for (let i = 0; i <= getSteps(width); i++) {
72 ctx.beginPath()
73 ctx.arc(horizRadius + horizRadius + (horizRadius * i * 2), height - horizRadius - (scallopWeight * 1), horizRadius, 0, Math.PI, false)
74 ctx.stroke()
75 }
76
77 // left
78 for (let i = 0; i <= getSteps(height); i++) {
79 ctx.beginPath()
80 ctx.arc(vertRadius + (scallopWeight * 1), vertRadius + vertRadius + (vertRadius * i * 2), vertRadius, Math.PI * 0.5, Math.PI * 1.5, false);
81 ctx.stroke()
82 }
83
84 // right
85 for (let i = 0; i <= getSteps(height); i++) {
86 ctx.beginPath()
87 ctx.arc(width - vertRadius - (scallopWeight * 1), vertRadius + vertRadius + (vertRadius * i * 2), vertRadius, Math.PI * 0.5, Math.PI * 1.5, true);
88 ctx.stroke()
89 }
90 }
91 }
92
93 registerPaint('extra-scallopedBorder', ScallopedBorder)
94}`
95
96const workletBlob = URL.createObjectURL(new Blob([worklet], { type: 'application/javascript' }))
97
98window.CSS.paintWorklet.addModule(workletBlob)
\No newline at end of file