-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheffect.ts
More file actions
76 lines (69 loc) · 2.06 KB
/
effect.ts
File metadata and controls
76 lines (69 loc) · 2.06 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
import { optimizeHex } from '../../utils/optimize-hex'
import { rgbaToHex } from '../../utils/rgba-to-hex'
import { addPx } from '../utils/add-px'
export function getEffectProps(
node: SceneNode,
): Record<string, string> | undefined {
if ('effects' in node && node.effects.length > 0) {
// TEXT nodes use textShadow for DROP_SHADOW (handled by text-shadow.ts)
const effects =
node.type === 'TEXT'
? node.effects.filter((e) => e.type !== 'DROP_SHADOW')
: node.effects
if (effects.length === 0) return
return effects.reduce(
(acc, effect) => {
const props = _getEffectPropsFromEffect(effect)
for (const [key, value] of Object.entries(props)) {
if (value) {
if (acc[key]) {
acc[key] = `${acc[key]}, ${value}`
} else {
acc[key] = value
}
}
}
return acc
},
{} as Record<string, string>,
)
}
}
function _getEffectPropsFromEffect(effect: Effect): Record<string, string> {
switch (effect.type) {
case 'DROP_SHADOW': {
const { offset, radius, spread, color } = effect
const { x, y } = offset
return {
boxShadow: `${addPx(x, '0')} ${addPx(y, '0')} ${addPx(radius, '0')} ${addPx(spread, '0')} ${optimizeHex(rgbaToHex(color))}`,
}
}
case 'INNER_SHADOW': {
const { offset, radius, spread, color } = effect
const { x, y } = offset
return {
boxShadow: `inset ${addPx(x, '0')} ${addPx(y, '0')} ${addPx(radius, '0')} ${addPx(spread, '0')} ${optimizeHex(rgbaToHex(color))}`,
}
}
case 'LAYER_BLUR':
return {
filter: `blur(${effect.radius}px)`,
}
case 'BACKGROUND_BLUR':
return {
backdropFilter: `blur(${effect.radius}px)`,
}
case 'NOISE':
return {
filter: `contrast(100%) brightness(100%)`,
}
case 'TEXTURE':
return {
filter: `contrast(100%) brightness(100%)`,
}
case 'GLASS':
return {
backdropFilter: `blur(${effect.radius}px)`,
}
}
}