2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
"use strict";
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
2011-09-19 21:48:16 +00:00
|
|
|
extend = fabric.util.object.extend,
|
2013-05-05 00:29:59 +00:00
|
|
|
coordProps = { 'x1': 1, 'x2': 1, 'y1': 1, 'y2': 1 },
|
|
|
|
|
supportsLineDash = fabric.StaticCanvas.supports('setLineDash');
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
if (fabric.Line) {
|
2010-10-14 21:42:39 +00:00
|
|
|
fabric.warn('fabric.Line is already defined');
|
2010-06-10 17:57:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Line class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Line
|
2010-10-14 21:42:39 +00:00
|
|
|
* @extends fabric.Object
|
|
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Line = fabric.util.createClass(fabric.Object, /** @lends fabric.Line.prototype */ {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Type of an object
|
2010-10-15 02:16:24 +00:00
|
|
|
* @type String
|
2013-05-18 11:01:34 +00:00
|
|
|
* @default
|
2010-10-15 02:16:24 +00:00
|
|
|
*/
|
2010-06-09 22:34:55 +00:00
|
|
|
type: 'line',
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Constructor
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Array} [points] Array of points
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Object} [options] Options object
|
|
|
|
|
* @return {fabric.Line} thisArg
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
|
|
|
|
initialize: function(points, options) {
|
2012-10-16 23:17:51 +00:00
|
|
|
options = options || { };
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
if (!points) {
|
|
|
|
|
points = [0, 0, 0, 0];
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
this.callSuper('initialize', options);
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
this.set('x1', points[0]);
|
|
|
|
|
this.set('y1', points[1]);
|
|
|
|
|
this.set('x2', points[2]);
|
|
|
|
|
this.set('y2', points[3]);
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-06-28 18:36:55 +00:00
|
|
|
this._setWidthHeight(options);
|
2011-09-19 21:48:16 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-08-09 10:24:22 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options
|
2012-08-09 10:24:22 +00:00
|
|
|
*/
|
2012-06-28 18:36:55 +00:00
|
|
|
_setWidthHeight: function(options) {
|
|
|
|
|
options || (options = { });
|
|
|
|
|
|
2012-06-28 19:14:19 +00:00
|
|
|
this.set('width', (this.x2 - this.x1) || 1);
|
|
|
|
|
this.set('height', (this.y2 - this.y1) || 1);
|
|
|
|
|
|
2012-06-28 18:36:55 +00:00
|
|
|
this.set('left', 'left' in options ? options.left : (this.x1 + this.width / 2));
|
|
|
|
|
this.set('top', 'top' in options ? options.top : (this.y1 + this.height / 2));
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-08-09 10:24:22 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param {String} key
|
|
|
|
|
* @param {Any} value
|
|
|
|
|
*/
|
|
|
|
|
_set: function(key, value) {
|
|
|
|
|
this[key] = value;
|
|
|
|
|
if (key in coordProps) {
|
2011-09-19 21:48:16 +00:00
|
|
|
this._setWidthHeight();
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
|
|
|
|
_render: function(ctx) {
|
|
|
|
|
ctx.beginPath();
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-04-04 10:56:22 +00:00
|
|
|
var isInPathGroup = this.group && this.group.type !== 'group';
|
2013-04-19 01:42:27 +00:00
|
|
|
if (isInPathGroup && !this.transformMatrix) {
|
2012-09-06 20:41:31 +00:00
|
|
|
ctx.translate(-this.group.width/2 + this.left, -this.group.height / 2 + this.top);
|
|
|
|
|
}
|
2013-04-19 01:42:27 +00:00
|
|
|
else {
|
|
|
|
|
ctx.translate(this.left, this.top);
|
|
|
|
|
}
|
2012-09-06 20:41:31 +00:00
|
|
|
|
2013-05-05 00:29:59 +00:00
|
|
|
if (!this.strokeDashArray || this.strokeDashArray && supportsLineDash) {
|
2013-05-04 16:25:57 +00:00
|
|
|
// move from center (of virtual box) to its left/top corner
|
|
|
|
|
ctx.moveTo(this.width === 1 ? 0 : (-this.width / 2), this.height === 1 ? 0 : (-this.height / 2));
|
|
|
|
|
ctx.lineTo(this.width === 1 ? 0 : (this.width / 2), this.height === 1 ? 0 : (this.height / 2));
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2011-05-31 18:23:30 +00:00
|
|
|
ctx.lineWidth = this.strokeWidth;
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-24 03:42:06 +00:00
|
|
|
// TODO: test this
|
|
|
|
|
// make sure setting "fill" changes color of a line
|
|
|
|
|
// (by copying fillStyle to strokeStyle, since line is stroked, not filled)
|
|
|
|
|
var origStrokeStyle = ctx.strokeStyle;
|
2013-04-19 01:42:27 +00:00
|
|
|
ctx.strokeStyle = this.stroke || ctx.fillStyle;
|
2013-05-04 16:25:57 +00:00
|
|
|
this._renderStroke(ctx);
|
2010-07-24 03:42:06 +00:00
|
|
|
ctx.strokeStyle = origStrokeStyle;
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-05-04 16:25:57 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
|
|
|
|
*/
|
|
|
|
|
_renderDashedStroke: function(ctx) {
|
|
|
|
|
var x = this.width === 1 ? 0 : -this.width / 2,
|
|
|
|
|
y = this.height === 1 ? 0 : -this.height / 2;
|
|
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
fabric.util.drawDashedLine(ctx, x, y, -x, -y, this.strokeDashArray);
|
|
|
|
|
ctx.closePath();
|
|
|
|
|
},
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns object representation of an instance
|
2010-06-09 22:34:55 +00:00
|
|
|
* @methd toObject
|
2012-12-01 12:57:27 +00:00
|
|
|
* @param {Array} propertiesToInclude
|
|
|
|
|
* @return {Object} object representation of an instance
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2012-11-30 22:46:09 +00:00
|
|
|
toObject: function(propertiesToInclude) {
|
|
|
|
|
return extend(this.callSuper('toObject', propertiesToInclude), {
|
2010-06-09 22:34:55 +00:00
|
|
|
x1: this.get('x1'),
|
|
|
|
|
y1: this.get('y1'),
|
|
|
|
|
x2: this.get('x2'),
|
|
|
|
|
y2: this.get('y2')
|
|
|
|
|
});
|
2012-01-02 21:14:20 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_START_ */
|
2012-01-02 21:14:20 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Returns SVG representation of an instance
|
|
|
|
|
* @return {String} svg representation of an instance
|
2012-01-02 21:14:20 +00:00
|
|
|
*/
|
|
|
|
|
toSVG: function() {
|
2013-02-23 16:02:52 +00:00
|
|
|
var markup = [];
|
|
|
|
|
|
|
|
|
|
if (this.stroke && this.stroke.toLive) {
|
|
|
|
|
markup.push(this.stroke.toSVG(this, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markup.push(
|
2012-01-02 21:14:20 +00:00
|
|
|
'<line ',
|
2013-02-23 16:02:52 +00:00
|
|
|
'x1="', this.get('x1'),
|
|
|
|
|
'" y1="', this.get('y1'),
|
|
|
|
|
'" x2="', this.get('x2'),
|
|
|
|
|
'" y2="', this.get('y2'),
|
|
|
|
|
'" style="', this.getSvgStyles(),
|
|
|
|
|
'"/>'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return markup.join('');
|
2013-05-09 18:19:06 +00:00
|
|
|
},
|
|
|
|
|
/* _TO_SVG_END_ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns complexity of an instance
|
|
|
|
|
* @return {Number} complexity
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return 1;
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
});
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-11-23 12:38:13 +00:00
|
|
|
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Line.fromElement})
|
2010-10-19 20:27:24 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @see http://www.w3.org/TR/SVG/shapes.html#LineElement
|
|
|
|
|
*/
|
2013-05-18 11:01:34 +00:00
|
|
|
fabric.Line.ATTRIBUTE_NAMES = (
|
|
|
|
|
'x1 y1 x2 y2 stroke stroke-width stroke-dasharray ' +
|
|
|
|
|
'stroke-linejoin stroke-linecap stroke-miterlimit transform'
|
|
|
|
|
).split(' ');
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Returns fabric.Line instance from an SVG element
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {SVGElement} element Element to parse
|
|
|
|
|
* @param {Object} [options] Options object
|
|
|
|
|
* @return {fabric.Line} instance of fabric.Line
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Line.fromElement = function(element, options) {
|
2010-10-19 20:27:24 +00:00
|
|
|
var parsedAttributes = fabric.parseAttributes(element, fabric.Line.ATTRIBUTE_NAMES);
|
2010-06-09 22:34:55 +00:00
|
|
|
var points = [
|
|
|
|
|
parsedAttributes.x1 || 0,
|
|
|
|
|
parsedAttributes.y1 || 0,
|
|
|
|
|
parsedAttributes.x2 || 0,
|
|
|
|
|
parsedAttributes.y2 || 0
|
|
|
|
|
];
|
2010-07-26 23:20:19 +00:00
|
|
|
return new fabric.Line(points, extend(parsedAttributes, options));
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Returns fabric.Line instance from an object representation
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Object} object Object to create an instance from
|
|
|
|
|
* @return {fabric.Line} instance of fabric.Line
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Line.fromObject = function(object) {
|
2010-06-09 22:34:55 +00:00
|
|
|
var points = [object.x1, object.y1, object.x2, object.y2];
|
2010-07-09 23:43:50 +00:00
|
|
|
return new fabric.Line(points, object);
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2011-08-05 23:00:26 +00:00
|
|
|
|
2013-04-04 10:56:22 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|