MCPcopy
hub / github.com/vercel/satori / buildDropShadow

Function buildDropShadow

src/builder/shadow.ts:25–133  ·  view source on GitHub ↗
(
  { id, width, height }: { id: string; width: number; height: number },
  style: {
    shadowColor: string[]
    shadowOffset: {
      width: number
      height: number
    }[]
    shadowRadius: number[]
  },
  transparentText = false
)

Source from the content-addressed store, hash-verified

23const SCALE = 1.1
24
25export function buildDropShadow(
26 { id, width, height }: { id: string; width: number; height: number },
27 style: {
28 shadowColor: string[]
29 shadowOffset: {
30 width: number
31 height: number
32 }[]
33 shadowRadius: number[]
34 },
35 transparentText = false
36) {
37 if (
38 !style.shadowColor ||
39 !style.shadowOffset ||
40 typeof style.shadowRadius === 'undefined'
41 ) {
42 return ''
43 }
44
45 const shadowCount = style.shadowColor.length
46 let effects = ''
47 let merge = ''
48
49 // There could be multiple shadows, we need to get the maximum bounding box
50 // and use `feMerge` to merge them together.
51 let left = 0
52 let right = width
53 let top = 0
54 let bottom = height
55 for (let i = 0; i < shadowCount; i++) {
56 // Expand the area for the filter to prevent it from cutting off.
57 const grow = (style.shadowRadius[i] * style.shadowRadius[i]) / 4
58 left = Math.min(style.shadowOffset[i].width - grow, left)
59 right = Math.max(style.shadowOffset[i].width + grow + width, right)
60 top = Math.min(style.shadowOffset[i].height - grow, top)
61 bottom = Math.max(style.shadowOffset[i].height + grow + height, bottom)
62
63 if (transparentText) {
64 // For transparent text, use primitive filters instead of feDropShadow
65 // because feDropShadow automatically includes source in output and
66 // source has unwanted text color
67 const resultId = `satori_s-${id}-result-${i}`
68 effects +=
69 buildXMLString('feGaussianBlur', {
70 in: 'SourceAlpha',
71 stdDeviation: style.shadowRadius[i] / 2,
72 result: `${resultId}-blur`,
73 }) +
74 buildXMLString('feOffset', {
75 in: `${resultId}-blur`,
76 dx: style.shadowOffset[i].width,
77 dy: style.shadowOffset[i].height,
78 result: `${resultId}-offset`,
79 }) +
80 buildXMLString('feFlood', {
81 'flood-color': style.shadowColor[i],
82 'flood-opacity': 1,

Callers 1

buildTextNodesFunction · 0.85

Calls 1

buildXMLStringFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…