-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
clip-path
basic usage (#459)
- Loading branch information
1 parent
c1469ee
commit a3e26c1
Showing
14 changed files
with
408 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { buildXMLString } from '../utils.js' | ||
import { createShapeParser } from '../parser/shape.js' | ||
|
||
export function genClipPathId(id: string) { | ||
return `satori_cp-${id}` | ||
} | ||
export function genClipPath(id: string) { | ||
return `url(#${genClipPathId(id)})` | ||
} | ||
|
||
export function buildClipPath( | ||
v: { | ||
left: number | ||
top: number | ||
width: number | ||
height: number | ||
path: string | ||
matrix: string | undefined | ||
id: string | ||
currentClipPath: string | string | ||
src?: string | ||
}, | ||
style: Record<string, string | number>, | ||
inheritedStyle: Record<string, string | number> | ||
) { | ||
if (style.clipPath === 'none') return '' | ||
|
||
const parser = createShapeParser(v, style, inheritedStyle) | ||
const clipPath = style.clipPath as string | ||
|
||
let tmp: { type: string; [p: string]: string | number } = { type: '' } | ||
|
||
for (const k of Object.keys(parser)) { | ||
tmp = parser[k](clipPath) | ||
if (tmp) break | ||
} | ||
|
||
if (tmp) { | ||
const { type, ...rest } = tmp | ||
return buildXMLString( | ||
'clipPath', | ||
{ | ||
id: genClipPathId(v.id), | ||
'clip-path': v.currentClipPath, | ||
}, | ||
buildXMLString(type, rest) | ||
) | ||
} | ||
return '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
import { lengthToNumber } from '../utils.js' | ||
import { default as buildBorderRadius } from '../builder/border-radius.js' | ||
import { getStylesForProperty } from 'css-to-react-native' | ||
|
||
const regexMap = { | ||
circle: /circle\((.+)\)/, | ||
ellipse: /ellipse\((.+)\)/, | ||
path: /path\((.+)\)/, | ||
polygon: /polygon\((.+)\)/, | ||
inset: /inset\((.+)\)/, | ||
} | ||
|
||
export function createShapeParser( | ||
{ | ||
width, | ||
height, | ||
}: { | ||
width: number | ||
height: number | ||
}, | ||
style: Record<string, string | number>, | ||
inheritedStyle: Record<string, string | number> | ||
) { | ||
function parseCircle(str: string) { | ||
const res = str.match(regexMap['circle']) | ||
|
||
if (!res) return null | ||
|
||
const [, value] = res | ||
const [radius, pos = ''] = value.split('at').map((v) => v.trim()) | ||
const { x, y } = resolvePosition(pos, width, height) | ||
|
||
return { | ||
type: 'circle', | ||
r: lengthToNumber( | ||
radius, | ||
inheritedStyle.fontSize as number, | ||
Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / Math.sqrt(2), | ||
inheritedStyle, | ||
true | ||
), | ||
cx: lengthToNumber( | ||
x, | ||
inheritedStyle.fontSize as number, | ||
width, | ||
inheritedStyle, | ||
true | ||
), | ||
cy: lengthToNumber( | ||
y, | ||
inheritedStyle.fontSize as number, | ||
height, | ||
inheritedStyle, | ||
true | ||
), | ||
} | ||
} | ||
function parseEllipse(str: string) { | ||
const res = str.match(regexMap['ellipse']) | ||
|
||
if (!res) return null | ||
|
||
const [, value] = res | ||
const [radius, pos = ''] = value.split('at').map((v) => v.trim()) | ||
const [rx, ry] = radius.split(' ') | ||
const { x, y } = resolvePosition(pos, width, height) | ||
|
||
return { | ||
type: 'ellipse', | ||
rx: lengthToNumber( | ||
rx || '50%', | ||
inheritedStyle.fontSize as number, | ||
width, | ||
inheritedStyle, | ||
true | ||
), | ||
ry: lengthToNumber( | ||
ry || '50%', | ||
inheritedStyle.fontSize as number, | ||
height, | ||
inheritedStyle, | ||
true | ||
), | ||
cx: lengthToNumber( | ||
x, | ||
inheritedStyle.fontSize as number, | ||
width, | ||
inheritedStyle, | ||
true | ||
), | ||
cy: lengthToNumber( | ||
y, | ||
inheritedStyle.fontSize as number, | ||
height, | ||
inheritedStyle, | ||
true | ||
), | ||
} | ||
} | ||
function parsePath(str: string) { | ||
const res = str.match(regexMap['path']) | ||
|
||
if (!res) return null | ||
|
||
const [fillRule, d] = resolveFillRule(res[1]) | ||
|
||
return { | ||
type: 'path', | ||
d, | ||
'fill-rule': fillRule, | ||
} | ||
} | ||
function parsePolygon(str: string) { | ||
const res = str.match(regexMap['polygon']) | ||
|
||
if (!res) return null | ||
|
||
const [fillRule, points] = resolveFillRule(res[1]) | ||
|
||
return { | ||
type: 'polygon', | ||
'fill-rule': fillRule, | ||
points: points | ||
.split(',') | ||
.map((v) => | ||
v | ||
.split(' ') | ||
.map((k, i) => | ||
lengthToNumber( | ||
k, | ||
inheritedStyle.fontSize as number, | ||
i === 0 ? width : height, | ||
inheritedStyle, | ||
true | ||
) | ||
) | ||
.join(' ') | ||
) | ||
.join(','), | ||
} | ||
} | ||
function parseInset(str: string) { | ||
const res = str.match(regexMap['inset']) | ||
|
||
if (!res) return null | ||
|
||
const [inset, radius] = ( | ||
res[1].includes('round') ? res[1] : `${res[1].trim()} round 0` | ||
).split('round') | ||
const radiusMap = getStylesForProperty('borderRadius', radius, true) | ||
const r = Object.values(radiusMap) | ||
.map((s) => String(s)) | ||
.map( | ||
(s, i) => | ||
lengthToNumber( | ||
s, | ||
inheritedStyle.fontSize as number, | ||
i === 0 || i === 2 ? height : width, | ||
inheritedStyle, | ||
true | ||
) || 0 | ||
) | ||
const offsets = Object.values(getStylesForProperty('margin', inset, true)) | ||
.map((s) => String(s)) | ||
.map( | ||
(s, i) => | ||
lengthToNumber( | ||
s, | ||
inheritedStyle.fontSize as number, | ||
i === 0 || i === 2 ? height : width, | ||
inheritedStyle, | ||
true | ||
) || 0 | ||
) | ||
const x = offsets[3] | ||
const y = offsets[0] | ||
const w = width - (offsets[1] + offsets[3]) | ||
const h = height - (offsets[0] + offsets[2]) | ||
|
||
if (r.some((v) => v > 0)) { | ||
const d = buildBorderRadius( | ||
{ left: x, top: y, width: w, height: h }, | ||
{ ...style, ...radiusMap } | ||
) | ||
|
||
return { type: 'path', d } | ||
} | ||
|
||
return { | ||
type: 'rect', | ||
x, | ||
y, | ||
width: w, | ||
height: h, | ||
} | ||
} | ||
|
||
return { | ||
parseCircle, | ||
parseEllipse, | ||
parsePath, | ||
parsePolygon, | ||
parseInset, | ||
} | ||
} | ||
|
||
function resolveFillRule(str: string) { | ||
const [, fillRule = 'nonzero', d] = | ||
str.replace(/('|")/g, '').match(/^(nonzero|evenodd)?,?(.+)/) || [] | ||
|
||
return [fillRule, d] | ||
} | ||
|
||
function resolvePosition(position: string, xDelta: number, yDelta: number) { | ||
const pos = position.split(' ') | ||
const res: { x: number | string; y: number | string } = { | ||
x: pos[0] || '50%', | ||
y: pos[1] || '50%', | ||
} | ||
|
||
pos.forEach((v) => { | ||
if (v === 'top') { | ||
res.y = 0 | ||
} else if (v === 'bottom') { | ||
res.y = yDelta | ||
} else if (v === 'left') { | ||
res.x = 0 | ||
} else if (v === 'right') { | ||
res.x = xDelta | ||
} else { | ||
res.x = xDelta / 2 | ||
res.y = yDelta / 2 | ||
} | ||
}) | ||
|
||
return res | ||
} |
Binary file added
BIN
+1.45 KB
...-path-test-tsx-border-should-make-clip-path-compatible-with-overflow-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.36 KB
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+941 Bytes
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-2-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1017 Bytes
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-3-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.19 KB
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-4-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.71 KB
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-5-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.72 KB
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-6-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.33 KB
...path-test-tsx-test-clip-path-test-tsx-border-should-render-clip-path-7-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
a3e26c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
satori-playground – ./
og-playground.vercel.app
satori-playground.vercel.app
og-playground.vercel.sh
satori-playground.vercel.sh
satori-playground-git-main.vercel.sh