forked from wopian/smooth-corners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.js
130 lines (130 loc) · 3.57 KB
/
paint.js
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
class SmoothCorners {
static get inputProperties() {
return [
'border-image-source',
'--smooth-corners',
'--smooth-corners-shadow',
'--smooth-corners-bg-color',
'--smooth-corners-padding',
]
}
trimPX(pixel) {
return parseInt(pixel.replace('px', ''), 10)
}
superellipse(a, b, nX, nY) {
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 = 2 / nY
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 + 1 }, (_, i) => points(i * step))
}
paint(ctx, geom, properties) {
const backgroundImage = properties
.get('border-image-source')
const backgroundColor = properties
.get('--smooth-corners-bg-color')
.toString()
const padding = properties
.get('--smooth-corners-padding')
.toString()
const boxShadow = properties
.get('--smooth-corners-shadow')
.toString()
.split(/(?<=[^0-9]),/)
.map(shadow => (
shadow
.split(/(?<=[^,]) /)
.map(value => value.trim())
))
const nX = properties
.get('--smooth-corners')
.toString()
const halfWidth = geom.width / 2
const halfHeight = geom.height / 2
const ratio = (() => {
if (!halfWidth || !halfHeight) {
return 1
} else {
return halfHeight / halfWidth
}
})()
const ratioForTargetNX = ratio >= 1
? ratio
: 1 / ratio
const targetNX = nX * ratioForTargetNX
const targetNY = targetNX * ratio
const smooth = this.superellipse(
halfWidth - this.trimPX(padding),
halfHeight - this.trimPX(padding),
parseFloat(targetNX, 10),
parseFloat(targetNY, 10)
)
ctx.setTransform(1, 0, 0, 1, halfWidth, halfHeight)
ctx.beginPath()
boxShadow.forEach(([
offsetX,
offsetY,
blur,
spread,
color,
]) => {
ctx.shadowColor = null
ctx.shadowOffsetX = 0
ctx.shadowOffsetY = 0
ctx.shadowBlur = 0
const trimedX = this.trimPX(offsetX) * 2
const trimedY = this.trimPX(offsetY) * 2
const trimedBlur = this.trimPX(blur)
const trimedSpread = this.trimPX(spread)
if (trimedBlur === 0) {
ctx.strokeStyle = color
ctx.lineWidth = trimedSpread * 2
} else {
ctx.shadowColor = color
ctx.shadowOffsetX = trimedX
ctx.shadowOffsetY = trimedY
ctx.shadowBlur = trimedBlur * 2
}
smooth.forEach(({ x, y }, index) => {
if (index === 0) {
ctx.moveTo(x, y)
} else {
ctx.lineTo(x, y)
}
})
if (trimedBlur === 0) {
ctx.stroke()
}
})
if (backgroundColor) {
ctx.fillStyle = backgroundColor
ctx.fill()
}
if (backgroundImage) {
smooth.forEach(({ x, y }, index) => {
if (index === 0) {
ctx.moveTo(x, y)
} else {
ctx.lineTo(x, y)
}
})
ctx.closePath()
ctx.clip()
ctx.drawImage(backgroundImage, -halfWidth, -halfHeight, geom.width, geom.height)
}
ctx.closePath()
}
}
registerPaint('smooth-corners', SmoothCorners)