2011-01-09 06:38:54 +00:00
|
|
|
(function() {
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _FROM_SVG_START_ */
|
2013-02-23 16:02:52 +00:00
|
|
|
function getColorStop(el) {
|
|
|
|
|
var style = el.getAttribute('style'),
|
|
|
|
|
offset = el.getAttribute('offset'),
|
|
|
|
|
color, opacity;
|
|
|
|
|
|
|
|
|
|
// convert percents to absolute values
|
|
|
|
|
offset = parseFloat(offset) / (/%$/.test(offset) ? 100 : 1);
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-20 06:42:00 +00:00
|
|
|
if (style) {
|
|
|
|
|
var keyValuePairs = style.split(/\s*;\s*/);
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2014-02-16 21:36:03 +00:00
|
|
|
if (keyValuePairs[keyValuePairs.length - 1] === '') {
|
2012-06-07 10:11:23 +00:00
|
|
|
keyValuePairs.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-20 06:42:00 +00:00
|
|
|
for (var i = keyValuePairs.length; i--; ) {
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-20 06:42:00 +00:00
|
|
|
var split = keyValuePairs[i].split(/\s*:\s*/),
|
|
|
|
|
key = split[0].trim(),
|
|
|
|
|
value = split[1].trim();
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-20 06:42:00 +00:00
|
|
|
if (key === 'stop-color') {
|
2013-02-23 16:02:52 +00:00
|
|
|
color = value;
|
|
|
|
|
}
|
|
|
|
|
else if (key === 'stop-opacity') {
|
|
|
|
|
opacity = value;
|
2011-01-20 06:42:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-23 16:02:52 +00:00
|
|
|
|
|
|
|
|
if (!color) {
|
2013-07-02 19:09:50 +00:00
|
|
|
color = el.getAttribute('stop-color') || 'rgb(0,0,0)';
|
2013-02-23 16:02:52 +00:00
|
|
|
}
|
|
|
|
|
if (!opacity) {
|
|
|
|
|
opacity = el.getAttribute('stop-opacity');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert rgba color to rgb color - alpha value has no affect in svg
|
|
|
|
|
color = new fabric.Color(color).toRgb();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
offset: offset,
|
|
|
|
|
color: color,
|
2013-07-02 19:09:50 +00:00
|
|
|
opacity: isNaN(parseFloat(opacity)) ? 1 : parseFloat(opacity)
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
};
|
2011-01-20 06:42:00 +00:00
|
|
|
}
|
2013-11-10 15:51:08 +00:00
|
|
|
|
|
|
|
|
function getLinearCoords(el) {
|
|
|
|
|
return {
|
|
|
|
|
x1: el.getAttribute('x1') || 0,
|
|
|
|
|
y1: el.getAttribute('y1') || 0,
|
|
|
|
|
x2: el.getAttribute('x2') || '100%',
|
|
|
|
|
y2: el.getAttribute('y2') || 0
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRadialCoords(el) {
|
|
|
|
|
return {
|
|
|
|
|
x1: el.getAttribute('fx') || el.getAttribute('cx') || '50%',
|
|
|
|
|
y1: el.getAttribute('fy') || el.getAttribute('cy') || '50%',
|
|
|
|
|
r1: 0,
|
|
|
|
|
x2: el.getAttribute('cx') || '50%',
|
|
|
|
|
y2: el.getAttribute('cy') || '50%',
|
|
|
|
|
r2: el.getAttribute('r') || '50%'
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _FROM_SVG_END_ */
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2012-08-20 16:18:30 +00:00
|
|
|
/**
|
2012-12-15 16:05:23 +00:00
|
|
|
* Gradient class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Gradient
|
2013-09-28 10:31:32 +00:00
|
|
|
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2/#gradients}
|
2013-10-05 18:21:28 +00:00
|
|
|
* @see {@link fabric.Gradient#initialize} for constructor definition
|
2012-08-20 16:18:30 +00:00
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Gradient = fabric.util.createClass(/** @lends fabric.Gradient.prototype */ {
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2012-11-23 12:38:13 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Constructor
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
* @param {Object} [options] Options object with type, coords, gradientUnits and colorStops
|
2012-12-15 16:05:23 +00:00
|
|
|
* @return {fabric.Gradient} thisArg
|
2012-11-23 12:38:13 +00:00
|
|
|
*/
|
2012-08-20 16:18:30 +00:00
|
|
|
initialize: function(options) {
|
2011-01-09 06:38:54 +00:00
|
|
|
options || (options = { });
|
|
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
var coords = { };
|
|
|
|
|
|
|
|
|
|
this.id = fabric.Object.__uid++;
|
|
|
|
|
this.type = options.type || 'linear';
|
2012-08-20 16:18:30 +00:00
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
coords = {
|
|
|
|
|
x1: options.coords.x1 || 0,
|
|
|
|
|
y1: options.coords.y1 || 0,
|
|
|
|
|
x2: options.coords.x2 || 0,
|
|
|
|
|
y2: options.coords.y2 || 0
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
};
|
2013-02-23 16:02:52 +00:00
|
|
|
|
|
|
|
|
if (this.type === 'radial') {
|
|
|
|
|
coords.r1 = options.coords.r1 || 0;
|
|
|
|
|
coords.r2 = options.coords.r2 || 0;
|
|
|
|
|
}
|
2012-08-20 16:18:30 +00:00
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
this.coords = coords;
|
|
|
|
|
this.gradientUnits = options.gradientUnits || 'objectBoundingBox';
|
2013-03-18 12:11:31 +00:00
|
|
|
this.colorStops = options.colorStops.slice();
|
2013-02-23 16:02:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds another colorStop
|
2013-02-25 17:26:20 +00:00
|
|
|
* @param {Object} colorStop Object with offset and color
|
2013-02-23 16:02:52 +00:00
|
|
|
* @return {fabric.Gradient} thisArg
|
|
|
|
|
*/
|
|
|
|
|
addColorStop: function(colorStop) {
|
2013-02-25 17:26:20 +00:00
|
|
|
for (var position in colorStop) {
|
|
|
|
|
var color = new fabric.Color(colorStop[position]);
|
2014-02-16 21:36:03 +00:00
|
|
|
this.colorStops.push({
|
|
|
|
|
offset: position,
|
|
|
|
|
color: color.toRgb(),
|
|
|
|
|
opacity: color.getAlpha()
|
|
|
|
|
});
|
2013-02-25 17:26:20 +00:00
|
|
|
}
|
2013-02-23 16:02:52 +00:00
|
|
|
return this;
|
2012-08-20 16:18:30 +00:00
|
|
|
},
|
|
|
|
|
|
2012-11-23 12:38:13 +00:00
|
|
|
/**
|
|
|
|
|
* Returns object representation of a gradient
|
2012-12-15 16:05:23 +00:00
|
|
|
* @return {Object}
|
2012-11-23 12:38:13 +00:00
|
|
|
*/
|
2012-08-20 16:18:30 +00:00
|
|
|
toObject: function() {
|
|
|
|
|
return {
|
2013-02-23 16:02:52 +00:00
|
|
|
type: this.type,
|
|
|
|
|
coords: this.coords,
|
|
|
|
|
gradientUnits: this.gradientUnits,
|
2012-08-20 16:18:30 +00:00
|
|
|
colorStops: this.colorStops
|
|
|
|
|
};
|
|
|
|
|
},
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_START_ */
|
2013-02-23 16:02:52 +00:00
|
|
|
/**
|
|
|
|
|
* Returns SVG representation of an gradient
|
|
|
|
|
* @param {Object} object Object to create a gradient for
|
|
|
|
|
* @param {Boolean} normalize Whether coords should be normalized
|
|
|
|
|
* @return {String} SVG representation of an gradient (linear/radial)
|
|
|
|
|
*/
|
|
|
|
|
toSVG: function(object, normalize) {
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
var coords = fabric.util.object.clone(this.coords),
|
|
|
|
|
markup;
|
2013-02-23 16:02:52 +00:00
|
|
|
|
|
|
|
|
// colorStops must be sorted ascending
|
|
|
|
|
this.colorStops.sort(function(a, b) {
|
|
|
|
|
return a.offset - b.offset;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (normalize && this.gradientUnits === 'userSpaceOnUse') {
|
|
|
|
|
coords.x1 += object.width / 2;
|
|
|
|
|
coords.y1 += object.height / 2;
|
|
|
|
|
coords.x2 += object.width / 2;
|
|
|
|
|
coords.y2 += object.height / 2;
|
|
|
|
|
}
|
|
|
|
|
else if (this.gradientUnits === 'objectBoundingBox') {
|
|
|
|
|
_convertValuesToPercentUnits(object, coords);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.type === 'linear') {
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
markup = [
|
2013-02-23 16:02:52 +00:00
|
|
|
'<linearGradient ',
|
|
|
|
|
'id="SVGID_', this.id,
|
|
|
|
|
'" gradientUnits="', this.gradientUnits,
|
|
|
|
|
'" x1="', coords.x1,
|
|
|
|
|
'" y1="', coords.y1,
|
|
|
|
|
'" x2="', coords.x2,
|
|
|
|
|
'" y2="', coords.y2,
|
|
|
|
|
'">'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
else if (this.type === 'radial') {
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
markup = [
|
2013-02-23 16:02:52 +00:00
|
|
|
'<radialGradient ',
|
|
|
|
|
'id="SVGID_', this.id,
|
|
|
|
|
'" gradientUnits="', this.gradientUnits,
|
|
|
|
|
'" cx="', coords.x2,
|
|
|
|
|
'" cy="', coords.y2,
|
|
|
|
|
'" r="', coords.r2,
|
|
|
|
|
'" fx="', coords.x1,
|
|
|
|
|
'" fy="', coords.y1,
|
|
|
|
|
'">'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < this.colorStops.length; i++) {
|
|
|
|
|
markup.push(
|
|
|
|
|
'<stop ',
|
|
|
|
|
'offset="', (this.colorStops[i].offset * 100) + '%',
|
|
|
|
|
'" style="stop-color:', this.colorStops[i].color,
|
|
|
|
|
(this.colorStops[i].opacity ? ';stop-opacity: ' + this.colorStops[i].opacity : ';'),
|
|
|
|
|
'"/>'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markup.push((this.type === 'linear' ? '</linearGradient>' : '</radialGradient>'));
|
|
|
|
|
|
|
|
|
|
return markup.join('');
|
2013-05-09 18:19:06 +00:00
|
|
|
},
|
|
|
|
|
/* _TO_SVG_END_ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an instance of CanvasGradient
|
2013-09-17 20:42:58 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
2013-05-09 18:19:06 +00:00
|
|
|
* @return {CanvasGradient}
|
|
|
|
|
*/
|
|
|
|
|
toLive: function(ctx) {
|
|
|
|
|
var gradient;
|
|
|
|
|
|
|
|
|
|
if (!this.type) return;
|
|
|
|
|
|
|
|
|
|
if (this.type === 'linear') {
|
|
|
|
|
gradient = ctx.createLinearGradient(
|
2013-05-31 11:30:44 +00:00
|
|
|
this.coords.x1, this.coords.y1, this.coords.x2, this.coords.y2);
|
2013-05-09 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
else if (this.type === 'radial') {
|
|
|
|
|
gradient = ctx.createRadialGradient(
|
|
|
|
|
this.coords.x1, this.coords.y1, this.coords.r1, this.coords.x2, this.coords.y2, this.coords.r2);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-06 10:31:32 +00:00
|
|
|
for (var i = 0, len = this.colorStops.length; i < len; i++) {
|
2013-05-09 18:19:06 +00:00
|
|
|
var color = this.colorStops[i].color,
|
|
|
|
|
opacity = this.colorStops[i].opacity,
|
|
|
|
|
offset = this.colorStops[i].offset;
|
|
|
|
|
|
2013-06-18 07:00:55 +00:00
|
|
|
if (typeof opacity !== 'undefined') {
|
2013-05-09 18:19:06 +00:00
|
|
|
color = new fabric.Color(color).setAlpha(opacity).toRgba();
|
|
|
|
|
}
|
|
|
|
|
gradient.addColorStop(parseFloat(offset), color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gradient;
|
2012-08-20 16:18:30 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fabric.util.object.extend(fabric.Gradient, {
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _FROM_SVG_START_ */
|
2011-01-09 06:38:54 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Returns {@link fabric.Gradient} instance from an SVG element
|
2011-01-09 06:38:54 +00:00
|
|
|
* @static
|
2012-11-23 12:38:13 +00:00
|
|
|
* @memberof fabric.Gradient
|
2013-08-08 16:31:26 +00:00
|
|
|
* @param {SVGGradientElement} el SVG gradient element
|
2013-09-17 20:42:58 +00:00
|
|
|
* @param {fabric.Object} instance
|
|
|
|
|
* @return {fabric.Gradient} Gradient instance
|
2011-01-09 06:38:54 +00:00
|
|
|
* @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement
|
2013-02-23 16:02:52 +00:00
|
|
|
* @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement
|
2011-01-09 06:38:54 +00:00
|
|
|
*/
|
2012-08-20 16:18:30 +00:00
|
|
|
fromElement: function(el, instance) {
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-20 06:42:00 +00:00
|
|
|
/**
|
2011-01-09 06:38:54 +00:00
|
|
|
* @example:
|
|
|
|
|
*
|
2013-02-23 16:02:52 +00:00
|
|
|
* <linearGradient id="linearGrad1">
|
2011-01-20 06:42:00 +00:00
|
|
|
* <stop offset="0%" stop-color="white"/>
|
|
|
|
|
* <stop offset="100%" stop-color="black"/>
|
|
|
|
|
* </linearGradient>
|
|
|
|
|
*
|
|
|
|
|
* OR
|
|
|
|
|
*
|
2013-02-23 16:02:52 +00:00
|
|
|
* <linearGradient id="linearGrad2">
|
|
|
|
|
* <stop offset="0" style="stop-color:rgb(255,255,255)"/>
|
|
|
|
|
* <stop offset="1" style="stop-color:rgb(0,0,0)"/>
|
2011-01-09 06:38:54 +00:00
|
|
|
* </linearGradient>
|
|
|
|
|
*
|
2013-02-23 16:02:52 +00:00
|
|
|
* OR
|
|
|
|
|
*
|
|
|
|
|
* <radialGradient id="radialGrad1">
|
|
|
|
|
* <stop offset="0%" stop-color="white" stop-opacity="1" />
|
|
|
|
|
* <stop offset="50%" stop-color="black" stop-opacity="0.5" />
|
|
|
|
|
* <stop offset="100%" stop-color="white" stop-opacity="1" />
|
|
|
|
|
* </radialGradient>
|
|
|
|
|
*
|
|
|
|
|
* OR
|
|
|
|
|
*
|
|
|
|
|
* <radialGradient id="radialGrad2">
|
|
|
|
|
* <stop offset="0" stop-color="rgb(255,255,255)" />
|
|
|
|
|
* <stop offset="0.5" stop-color="rgb(0,0,0)" />
|
|
|
|
|
* <stop offset="1" stop-color="rgb(255,255,255)" />
|
|
|
|
|
* </radialGradient>
|
|
|
|
|
*
|
2011-01-09 06:38:54 +00:00
|
|
|
*/
|
2011-01-20 06:42:00 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
var colorStopEls = el.getElementsByTagName('stop'),
|
2013-02-23 16:02:52 +00:00
|
|
|
type = (el.nodeName === 'linearGradient' ? 'linear' : 'radial'),
|
|
|
|
|
gradientUnits = el.getAttribute('gradientUnits') || 'objectBoundingBox',
|
|
|
|
|
colorStops = [],
|
|
|
|
|
coords = { };
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
if (type === 'linear') {
|
2013-11-10 15:51:08 +00:00
|
|
|
coords = getLinearCoords(el);
|
2013-02-23 16:02:52 +00:00
|
|
|
}
|
|
|
|
|
else if (type === 'radial') {
|
2013-11-10 15:51:08 +00:00
|
|
|
coords = getRadialCoords(el);
|
2013-02-23 16:02:52 +00:00
|
|
|
}
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
for (var i = colorStopEls.length; i--; ) {
|
|
|
|
|
colorStops.push(getColorStop(colorStopEls[i]));
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-09 07:09:28 +00:00
|
|
|
_convertPercentUnitsToValues(instance, coords);
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2012-08-20 16:18:30 +00:00
|
|
|
return new fabric.Gradient({
|
2013-02-23 16:02:52 +00:00
|
|
|
type: type,
|
|
|
|
|
coords: coords,
|
|
|
|
|
gradientUnits: gradientUnits,
|
2011-01-09 06:38:54 +00:00
|
|
|
colorStops: colorStops
|
|
|
|
|
});
|
|
|
|
|
},
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _FROM_SVG_END_ */
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Returns {@link fabric.Gradient} instance from its object representation
|
2011-01-09 06:38:54 +00:00
|
|
|
* @static
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberof fabric.Gradient
|
2013-02-23 16:02:52 +00:00
|
|
|
* @param {Object} obj
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
* @param {Object} [options] Options object
|
2011-01-09 06:38:54 +00:00
|
|
|
*/
|
2012-08-20 16:18:30 +00:00
|
|
|
forObject: function(obj, options) {
|
2011-01-09 06:38:54 +00:00
|
|
|
options || (options = { });
|
|
|
|
|
_convertPercentUnitsToValues(obj, options);
|
2012-08-20 16:18:30 +00:00
|
|
|
return new fabric.Gradient(options);
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
2012-08-20 16:18:30 +00:00
|
|
|
});
|
2012-06-07 10:11:23 +00:00
|
|
|
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2011-01-09 06:38:54 +00:00
|
|
|
function _convertPercentUnitsToValues(object, options) {
|
|
|
|
|
for (var prop in options) {
|
|
|
|
|
if (typeof options[prop] === 'string' && /^\d+%$/.test(options[prop])) {
|
|
|
|
|
var percents = parseFloat(options[prop], 10);
|
2013-02-23 16:02:52 +00:00
|
|
|
if (prop === 'x1' || prop === 'x2' || prop === 'r2') {
|
2012-12-15 16:09:21 +00:00
|
|
|
options[prop] = fabric.util.toFixed(object.width * percents / 100, 2);
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
else if (prop === 'y1' || prop === 'y2') {
|
2012-12-15 16:09:21 +00:00
|
|
|
options[prop] = fabric.util.toFixed(object.height * percents / 100, 2);
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-25 18:27:02 +00:00
|
|
|
normalize(options, prop, object);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// normalize rendering point (should be from top/left corner rather than center of the shape)
|
|
|
|
|
function normalize(options, prop, object) {
|
|
|
|
|
if (prop === 'x1' || prop === 'x2') {
|
|
|
|
|
options[prop] -= fabric.util.toFixed(object.width / 2, 2);
|
|
|
|
|
}
|
|
|
|
|
else if (prop === 'y1' || prop === 'y2') {
|
|
|
|
|
options[prop] -= fabric.util.toFixed(object.height / 2, 2);
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _TO_SVG_START_ */
|
[BACK_INCOMPAT] Implement fabric.Gradient#toSVG() and radialGradient
- Implement radial gradient and expand linear gradient (stop-opacity should now take into account)
- Gradients should now be included in the SVG output for the following fabric objects: circle, ellipse, line, path, polygon, polyline, rect and triangle (text is not yet implemented)
- Gradients (linear / radial) can be applied to stroke or fill property => change setGradientFill(options) to setGradient(type, options)
- Change toObject() that linear and radial gradients can be serialized
- Expand fabric.Color by 16 basic colors fabric.Color.colorNameMap => gradients with e.g. stop-color="blue" and stop-opacity="0.5 can be converted to RGBA color
- RGBA colors in svg has no affect (convert to RGB color), only stop-opacity has affect to color opacity
- Attached some test svg files http://kienzle.geschaeft.s3.amazonaws.com/projects/fabricjs/gradients/gradients.rarUpdate setGradient and addColorStop interface
Update setGradient and addColorStop interface
- setGradient('fill', {x1: 50, y2: 10, colorStops: {'0.7', 'rgba(0,153,153,0.5)'}});
- addColorStop({'0.4', 'blue'});
2013-03-09 20:00:54 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2013-02-23 16:02:52 +00:00
|
|
|
function _convertValuesToPercentUnits(object, options) {
|
|
|
|
|
for (var prop in options) {
|
2013-10-25 18:27:02 +00:00
|
|
|
|
|
|
|
|
normalize(options, prop, object);
|
|
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
// convert to percent units
|
|
|
|
|
if (prop === 'x1' || prop === 'x2' || prop === 'r2') {
|
|
|
|
|
options[prop] = fabric.util.toFixed(options[prop] / object.width * 100, 2) + '%';
|
|
|
|
|
}
|
|
|
|
|
else if (prop === 'y1' || prop === 'y2') {
|
|
|
|
|
options[prop] = fabric.util.toFixed(options[prop] / object.height * 100, 2) + '%';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-30 19:55:26 +00:00
|
|
|
/* _TO_SVG_END_ */
|
2012-06-07 10:11:23 +00:00
|
|
|
|
2013-04-24 16:58:04 +00:00
|
|
|
})();
|