Skip to content

Commit 2f15090

Browse files
committed
Fix applying gradient to horizontal/vertical lines
1 parent 7e025de commit 2f15090

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/helper/style-path.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,31 @@ const createGradientObject = function (color1, color2, gradientType, bounds, rad
7474
if (color2 === null) {
7575
color2 = getColorStringForTransparent(color1);
7676
}
77-
const halfLongestDimension = Math.max(bounds.width, bounds.height) / 2;
78-
const start = gradientType === GradientTypes.RADIAL ? (radialCenter || bounds.center) :
79-
gradientType === GradientTypes.VERTICAL ? bounds.topCenter :
80-
gradientType === GradientTypes.HORIZONTAL ? bounds.leftCenter :
81-
null;
82-
const end = gradientType === GradientTypes.RADIAL ? start.add(new paper.Point(halfLongestDimension, 0)) :
83-
gradientType === GradientTypes.VERTICAL ? bounds.bottomCenter :
84-
gradientType === GradientTypes.HORIZONTAL ? bounds.rightCenter :
85-
null;
77+
78+
// Force gradients to have a minimum length. If the gradient start and end points are the same or very close
79+
// (e.g. applying a vertical gradient to a perfectly horizontal line or vice versa), the gradient will not appear.
80+
const minGradientLength = 1e-2;
81+
82+
let start;
83+
let end;
84+
switch (gradientType) {
85+
case GradientTypes.HORIZONTAL:
86+
start = bounds.leftCenter;
87+
end = bounds.rightCenter;
88+
if (Math.abs(end.x - start.x) < minGradientLength) end.x += minGradientLength;
89+
break;
90+
case GradientTypes.VERTICAL:
91+
start = bounds.topCenter;
92+
end = bounds.bottomCenter;
93+
if (Math.abs(end.y - start.y) < minGradientLength) end.y += minGradientLength;
94+
break;
95+
case GradientTypes.RADIAL: {
96+
const halfLongestDimension = Math.max(bounds.width, bounds.height) / 2;
97+
start = radialCenter || bounds.center;
98+
end = start.add(new paper.Point(halfLongestDimension, 0));
99+
break;
100+
}
101+
}
86102
return {
87103
gradient: {
88104
stops: [color1, color2],

0 commit comments

Comments
 (0)