Skip to content

Commit d8c588b

Browse files
committed
fix: repeating-linear-gradient effect with correct cycle
1 parent de5b31a commit d8c588b

4 files changed

+39
-1
lines changed

src/builder/gradient/linear.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function buildLinearGradient(
5656
: points
5757

5858
const stops = normalizeStops(
59-
length,
59+
repeating ? resolveRepeatingCycle(parsed.stops, length) : length,
6060
parsed.stops,
6161
inheritableStyle,
6262
repeating,
@@ -103,6 +103,16 @@ export function buildLinearGradient(
103103
return [patternId, defs]
104104
}
105105

106+
function resolveRepeatingCycle(stops: ColorStop[], length: number) {
107+
const last = stops[stops.length - 1]
108+
const { offset } = last
109+
if (!offset) return length
110+
111+
if (offset.unit === '%') return (Number(offset.value) / 100) * length
112+
113+
return Number(offset.value)
114+
}
115+
106116
function resolveXYFromDirection(dir: string) {
107117
let x1 = 0,
108118
y1 = 0,

test/gradient.test.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -555,5 +555,33 @@ describe('Gradient', () => {
555555
)
556556
expect(toImage(svg, 100)).toMatchImageSnapshot()
557557
})
558+
559+
it('should compute correct cycle', async () => {
560+
const svgs = await Promise.all(
561+
[
562+
`repeating-linear-gradient(45deg, #606dbc, #606dbc 5px, #465298 5px, #465298 10px)`,
563+
`repeating-linear-gradient(45deg, #606dbc, #606dbc 5px, #465298 5px, #465298 10%)`,
564+
].map((backgroundImage) =>
565+
satori(
566+
<div
567+
style={{
568+
backgroundColor: 'white',
569+
backgroundImage,
570+
width: '100px',
571+
height: '100px',
572+
}}
573+
></div>,
574+
{
575+
width: 100,
576+
height: 100,
577+
fonts,
578+
}
579+
)
580+
)
581+
)
582+
svgs.forEach((svg) => {
583+
expect(toImage(svg, 100)).toMatchImageSnapshot()
584+
})
585+
})
558586
})
559587
})

0 commit comments

Comments
 (0)