Repository navigation
Expand file tree
/
Copy pathpaint.js
More file actions
137 lines (114 loc) · 4 KB
/
Copy pathpaint.js
File metadata and controls
137 lines (114 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class SmoothCornersPainter {
static get inputProperties() {
return ['--smooth-corners']
}
superellipse(a, b, nX = 4, nY) {
if (Number.isNaN(nX)) nX = 4
if (typeof nY === 'undefined' || Number.isNaN(nY)) nY = nX
if (nX > 100) nX = 100
if (nY > 100) nY = 100
if (nX < 0.00000000001) nX = 0.00000000001
if (nY < 0.00000000001) nY = 0.00000000001
const nX2 = 2 / nX
const nY2 = nY ? 2 / nY : nX2
const steps = 360
const step = (2 * Math.PI) / steps
const points = t => {
const cosT = Math.cos(t)
const sinT = Math.sin(t)
return {
x: Math.abs(cosT) ** nX2 * a * Math.sign(cosT),
y: Math.abs(sinT) ** nY2 * b * Math.sign(sinT)
}
}
return Array.from({ length: steps }, (_, i) => points(i * step))
}
paint(ctx, geom, properties) {
const [nX, nY] = properties
.get('--smooth-corners')
.toString()
.replace(/ /g, '')
.split(',')
const width = geom.width / 2
const height = geom.height / 2
const smooth = this.superellipse(
width,
height,
parseFloat(nX),
parseFloat(nY)
)
ctx.fillStyle = '#000'
ctx.setTransform(1, 0, 0, 1, width, height)
ctx.beginPath()
for (let i = 0; i < smooth.length; i++) {
const { x, y } = smooth[i]
if (i === 0) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
}
ctx.closePath()
ctx.fill()
}
}
class SmoothCornerPainterV2 {
static get inputProperties() {
return ['--smooth-corners', '--smooth-corners-radius', '--smooth-corners-steps']
}
superellipse(a, b, nX = 4, nY, steps = 360) {
if (Number.isNaN(nX)) nX = 4
if (typeof nY === 'undefined' || Number.isNaN(nY)) nY = nX
if (nX > 100) nX = 100
if (nY > 100) nY = 100
if (nX < 0.00000000001) nX = 0.00000000001
if (nY < 0.00000000001) nY = 0.00000000001
const nX2 = 2 / nX
const nY2 = nY ? 2 / nY : nX2
const step = (2 * Math.PI) / steps
const points = t => {
const cosT = Math.cos(t)
const sinT = Math.sin(t)
return {
x: Math.abs(cosT) ** nX2 * a * Math.sign(cosT),
y: Math.abs(sinT) ** nY2 * b * Math.sign(sinT)
}
}
return Array.from({ length: steps }, (_, i) => points(i * step))
}
paint(ctx, size, props) {
const [nX, nY] = props
.get('--smooth-corners')
.toString()
.replace(/ /g, '')
.split(',')
.map(parseFloat)
const halfWidth = size.width / 2
const halfHeight = size.height / 2
const cornerRadius = Math.min(parseFloat(props.get('--smooth-corner-radius')) || size.width / 2, size.height / 2, size.width / 2)
const steps = parseFloat(props.get('--smooth-corner-steps')) || 360
const smoothCorners = this.superellipse(
cornerRadius,
cornerRadius,
nX,
nY,
steps
)
const xOffset = halfWidth - cornerRadius
const yOffset = halfHeight - cornerRadius
const leftBottomCorners = smoothCorners.slice(0, steps / 4).map(({ x, y }) => ({ x: x + xOffset, y: y + yOffset }))
const rightBottomCorners = smoothCorners.slice(steps / 4, steps / 2).map(({ x, y }) => ({ x: x - xOffset, y: y + yOffset }))
const rightTopCorners = smoothCorners.slice(steps / 2, steps * 3 / 4).map(({ x, y }) => ({ x: x - xOffset, y: y - yOffset }))
const leftTopCorners = smoothCorners.slice(steps * 3 / 4, steps).map(({ x, y }) => ({ x: x + xOffset, y: y - yOffset }))
const points = [...leftBottomCorners, ...rightBottomCorners, ...rightTopCorners, ...leftTopCorners]
ctx.fillStyle = '#000'
ctx.setTransform(1, 0, 0, 1, halfWidth, halfHeight)
ctx.beginPath()
points.forEach(({ x, y }, i) => {
if (i === 0) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
})
ctx.closePath()
ctx.fill()
}
}
// eslint-disable-next-line no-undef
registerPaint('smooth-corners', SmoothCornersPainter)
registerPaint('smooth-corners-v2', SmoothCornerPainterV2)