Skip to content

Commit f6212cd

Browse files
committed
Properly render gradients when strokeScaling=false
1 parent 4980971 commit f6212cd

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

src/item/Item.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4279,16 +4279,16 @@ new function() { // Injection scope for hit-test functions shared with project
42794279
* Not defined in Path as it is required by other classes too,
42804280
* e.g. PointText.
42814281
*/
4282-
_setStyles: function(ctx, param, viewMatrix) {
4282+
_setStyles: function(ctx, param, viewMatrix, strokeMatrix) {
42834283
// We can access internal properties since we're only using this on
42844284
// items without children, where styles would be merged.
42854285
var style = this._style,
42864286
matrix = this._matrix;
42874287
if (style.hasFill()) {
4288-
ctx.fillStyle = style.getFillColor().toCanvasStyle(ctx, matrix);
4288+
ctx.fillStyle = style.getFillColor().toCanvasStyle(ctx, matrix, strokeMatrix);
42894289
}
42904290
if (style.hasStroke()) {
4291-
ctx.strokeStyle = style.getStrokeColor().toCanvasStyle(ctx, matrix);
4291+
ctx.strokeStyle = style.getStrokeColor().toCanvasStyle(ctx, matrix, strokeMatrix);
42924292
ctx.lineWidth = style.getStrokeWidth();
42934293
var strokeJoin = style.getStrokeJoin(),
42944294
strokeCap = style.getStrokeCap(),

src/item/Shape.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ var Shape = Item.extend(/** @lends Shape# */{
261261
ctx.closePath();
262262
}
263263
if (!dontPaint && (hasFill || hasStroke)) {
264-
this._setStyles(ctx, param, viewMatrix);
264+
this._setStyles(ctx, param, viewMatrix, strokeMatrix);
265265
if (hasFill) {
266266
ctx.fill(style.getFillRule());
267267
ctx.shadowColor = 'rgba(0,0,0,0)';

src/path/CompoundPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
311311
children[i].draw(ctx, param, strokeMatrix);
312312

313313
if (!param.clip) {
314-
this._setStyles(ctx, param, viewMatrix);
314+
this._setStyles(ctx, param, viewMatrix, strokeMatrix);
315315
var style = this._style;
316316
if (style.hasFill()) {
317317
ctx.fill(style.getFillRule());

src/path/Path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,7 @@ new function() { // Scope for drawing
23212321
if (!dontPaint && (hasFill || hasStroke)) {
23222322
// If the path is part of a compound path or doesn't have a fill
23232323
// or stroke, there is no need to continue.
2324-
this._setStyles(ctx, param, viewMatrix);
2324+
this._setStyles(ctx, param, viewMatrix, strokeMatrix);
23252325
if (hasFill) {
23262326
ctx.fill(style.getFillRule());
23272327
// If shadowColor is defined, clear it after fill, so it

src/style/Color.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,11 @@ var Color = Base.extend(new function() {
899899
+ components.join(',') + ')';
900900
},
901901

902-
toCanvasStyle: function(ctx, matrix) {
903-
if (this._canvasStyle)
902+
toCanvasStyle: function(ctx, matrix, strokeMatrix) {
903+
// strokeMatrix can change without triggering _changed here, so we
904+
// can't use a cached gradient here.
905+
var strokeMayChange = this._type === 'gradient' && strokeMatrix;
906+
if (this._canvasStyle && !strokeMayChange)
904907
return this._canvasStyle;
905908
// Normal colors are simply represented by their CSS string.
906909
if (this._type !== 'gradient')
@@ -923,6 +926,12 @@ var Color = Base.extend(new function() {
923926
if (highlight)
924927
highlight = inverse._transformPoint(highlight);
925928
}
929+
if (strokeMatrix) {
930+
origin = strokeMatrix._transformPoint(origin);
931+
destination = strokeMatrix._transformPoint(destination);
932+
if (highlight)
933+
highlight = strokeMatrix._transformPoint(highlight);
934+
}
926935
if (gradient._radial) {
927936
var radius = destination.getDistance(origin);
928937
if (highlight) {
@@ -948,7 +957,12 @@ var Color = Base.extend(new function() {
948957
offset == null ? i / (l - 1) : offset,
949958
stop._color.toCanvasStyle());
950959
}
951-
return this._canvasStyle = canvasGradient;
960+
// Don't store gradients that may change in the cache.
961+
// If we cached a gradient that was transformed by strokeMatrix
962+
// then set strokeScaling to true, then the transformed gradient
963+
// could get "stuck" in the cache.
964+
if (!strokeMayChange) this._canvasStyle = canvasGradient;
965+
return canvasGradient;
952966
},
953967

954968
/**

0 commit comments

Comments
 (0)