Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-6014 ISSUE-6077 support percentage gradient on text #6090

Merged
merged 3 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/gradient.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,28 +317,34 @@
/**
* Returns an instance of CanvasGradient
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {fabric.Object} object the fabric.Object for which the gradient is
* @return {CanvasGradient}
*/
toLive: function(ctx) {
var gradient, coords = fabric.util.object.clone(this.coords), i, len;
toLive: function(ctx, object) {
var gradient, coords = fabric.util.object.clone(this.coords), i, len,
x1 = coords.x1, y1 = coords.y1, x2 = coords.x2, y2 = coords.y2,
stops = this.colorStops;

if (!this.type) {
return;
}

if (object instanceof fabric.Text && this.gradientUnits === 'percentage') {
x1 *= object.width;
y1 *= object.height;
x2 *= object.width;
y2 *= object.height;
}
if (this.type === 'linear') {
gradient = ctx.createLinearGradient(
coords.x1, coords.y1, coords.x2, coords.y2);
gradient = ctx.createLinearGradient(x1, y1, x2, y2);
}
else if (this.type === 'radial') {
gradient = ctx.createRadialGradient(
coords.x1, coords.y1, coords.r1, coords.x2, coords.y2, coords.r2);
gradient = ctx.createRadialGradient(x1, y1, coords.r1, x2, y2, coords.r2);
}

for (i = 0, len = this.colorStops.length; i < len; i++) {
var color = this.colorStops[i].color,
opacity = this.colorStops[i].opacity,
offset = this.colorStops[i].offset;
for (i = 0, len = stops.length; i < len; i++) {
var color = stops[i].color,
opacity = stops[i].opacity,
offset = stops[i].offset;

if (typeof opacity !== 'undefined') {
color = new fabric.Color(color).setAlpha(opacity).toRgba();
Expand Down
18 changes: 18 additions & 0 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,24 @@
return -this.height / 2;
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Object} filler fabric.Pattern or fabric.Gradient
* @return {Object} offset.offsetX offset for text rendering
* @return {Object} offset.offsetY offset for text rendering
*/
_applyPatternGradientTransform: function(ctx, filler) {
if (!filler || !filler.toLive) {
return { offsetX: 0, offsetY: 0 };
}
var offsetX = -this.width / 2 + filler.offsetX || 0,
offsetY = -this.height / 2 + filler.offsetY || 0;

ctx.transform(1, 0, 0, 1, offsetX, offsetY);
return { offsetX: offsetX, offsetY: offsetY };
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
Expand Down
Binary file added test/visual/golden/text7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions test/visual/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,41 @@
percentage: 0.06,
});

function text7(canvas, callback) {
var gradient = new fabric.Gradient({
coords: {
x1: 0,
y1: 0,
x2: 1,
y2: 0
},
gradientUnits: 'percentage',
colorStops: [{
offset: 0,
color: 'red',
}, {
offset: 1,
color: 'blue'
}]
});
var text = new fabric.Text('PERCENTAGE GRADIENT\nPERCENTAGE GRADIENT\nPERCENTAGE GRADIENT', {
left: 0,
top: 0,
fontSize: 16,
fill: gradient,
});
canvas.add(text);
canvas.renderAll();
callback(canvas.lowerCanvasEl);
}

tests.push({
test: 'Text percentage gradient',
code: text7,
golden: 'text7.png',
disabled: !fabric.isLikelyNode,
percentage: 0.05,
});

tests.forEach(visualTestLoop(QUnit));
})();