UNPKG

2.44 kBJavaScriptView Raw
1if (typeof registerPaint !== 'undefined') {
2 class ScallopedBorder {
3 static get inputProperties() {
4 return ['--extra-scallopRadius', '--extra-scallopWeight', '--extra-scallopColor']
5 }
6
7 paint(ctx, size, properties) {
8 const radius = properties.get('--extra-scallopRadius').value
9 const scallopWeight = properties.get('--extra-scallopWeight')
10 const color = properties.get('--extra-scallopColor')
11 const height = size.height
12 const width = size.width
13
14 ctx.lineWidth = scallopWeight
15 ctx.strokeStyle = color
16
17 const getSteps = (sizeVal) => {
18 return Math.floor(sizeVal / (radius * 2) - 2)
19 }
20
21 const getOwnRadius = (sizeVal, otherRad) => {
22 const steps = getSteps(sizeVal) + 1
23 const totalSpace = sizeVal - (radius * 2)
24 const spaceTaken = steps * (radius * 2)
25 let pixelsRemaining = totalSpace - spaceTaken
26
27 if (otherRad) {
28 const radDif = otherRad - radius
29 pixelsRemaining = totalSpace - spaceTaken - radDif
30 }
31
32 const newRadius = radius + ((pixelsRemaining / 2) / (steps + 1))
33 return (newRadius)
34 }
35
36 const horizRadius = getOwnRadius(width, getOwnRadius(height))
37 const vertRadius = getOwnRadius(height, getOwnRadius(width))
38
39 // top
40 for (let i = 0; i <= getSteps(width); i++) {
41 ctx.beginPath()
42 ctx.arc(horizRadius + horizRadius + (horizRadius * i * 2), horizRadius + (scallopWeight * 1) , horizRadius, 0, Math.PI, true)
43 ctx.stroke()
44 }
45
46 // bottom
47 for (let i = 0; i <= getSteps(width); i++) {
48 ctx.beginPath()
49 ctx.arc(horizRadius + horizRadius + (horizRadius * i * 2), height - horizRadius - (scallopWeight * 1), horizRadius, 0, Math.PI, false)
50 ctx.stroke()
51 }
52
53 // left
54 for (let i = 0; i <= getSteps(height); i++) {
55 ctx.beginPath()
56 ctx.arc(vertRadius + (scallopWeight * 1), vertRadius + vertRadius + (vertRadius * i * 2), vertRadius, Math.PI * 0.5, Math.PI * 1.5, false);
57 ctx.stroke()
58 }
59
60 // right
61 for (let i = 0; i <= getSteps(height); i++) {
62 ctx.beginPath()
63 ctx.arc(width - vertRadius - (scallopWeight * 1), vertRadius + vertRadius + (vertRadius * i * 2), vertRadius, Math.PI * 0.5, Math.PI * 1.5, true);
64 ctx.stroke()
65 }
66 }
67 }
68
69 registerPaint('extra-scallopedBorder', ScallopedBorder)
70}