fabric.js/src/line.class.js

208 lines
5.7 KiB
JavaScript
Raw Normal View History

(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
coordProps = { 'x1': 1, 'x2': 1, 'y1': 1, 'y2': 1 },
supportsLineDash = fabric.StaticCanvas.supports('setLineDash');
if (fabric.Line) {
fabric.warn('fabric.Line is already defined');
return;
}
/**
2012-12-13 14:36:43 +00:00
* Line class
2013-04-25 18:21:32 +00:00
* @class fabric.Line
* @extends fabric.Object
*/
fabric.Line = fabric.util.createClass(fabric.Object, /** @lends fabric.Line.prototype */ {
/**
2012-12-13 14:36:43 +00:00
* Type of an object
* @type String
*/
2010-06-09 22:34:55 +00:00
type: 'line',
2010-06-09 22:34:55 +00:00
/**
* Constructor
2012-12-15 16:05:23 +00:00
* @param {Array} [points] Array of points
* @param {Object} [options] Options object
* @return {fabric.Line} thisArg
2010-06-09 22:34:55 +00:00
*/
initialize: function(points, options) {
options = options || { };
2010-06-09 22:34:55 +00:00
if (!points) {
points = [0, 0, 0, 0];
}
2010-06-09 22:34:55 +00:00
this.callSuper('initialize', options);
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]);
this._setWidthHeight(options);
},
/**
* @private
2012-12-13 14:36:43 +00:00
* @param {Object} [options] Options
*/
_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);
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
},
/**
* @private
* @param {String} key
* @param {Any} value
*/
_set: function(key, value) {
this[key] = value;
if (key in coordProps) {
this._setWidthHeight();
}
return this;
},
2010-06-09 22:34:55 +00:00
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
2010-06-09 22:34:55 +00:00
*/
_render: function(ctx) {
ctx.beginPath();
var isInPathGroup = this.group && this.group.type !== 'group';
if (isInPathGroup && !this.transformMatrix) {
ctx.translate(-this.group.width/2 + this.left, -this.group.height / 2 + this.top);
}
else {
ctx.translate(this.left, this.top);
}
if (!this.strokeDashArray || this.strokeDashArray && supportsLineDash) {
// 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));
}
2011-05-31 18:23:30 +00:00
ctx.lineWidth = this.strokeWidth;
// 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;
ctx.strokeStyle = this.stroke || ctx.fillStyle;
this._renderStroke(ctx);
ctx.strokeStyle = origStrokeStyle;
2010-06-09 22:34:55 +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-19 20:27:24 +00:00
* Returns complexity of an instance
2010-06-09 22:34:55 +00:00
* @return {Number} complexity
*/
complexity: function() {
return 1;
},
2010-06-09 22:34:55 +00:00
/**
* Returns object representation of an instance
2010-06-09 22:34:55 +00:00
* @methd toObject
* @param {Array} propertiesToInclude
* @return {Object} object representation of an instance
2010-06-09 22:34:55 +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-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
* @return {String} svg representation of an instance
*/
toSVG: function() {
var markup = [];
if (this.stroke && this.stroke.toLive) {
markup.push(this.stroke.toSVG(this, true));
}
markup.push(
'<line ',
'x1="', this.get('x1'),
'" y1="', this.get('y1'),
'" x2="', this.get('x2'),
'" y2="', this.get('y2'),
'" style="', this.getSvgStyles(),
'"/>'
);
return markup.join('');
2010-06-09 22:34:55 +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
* @see http://www.w3.org/TR/SVG/shapes.html#LineElement
*/
2010-10-19 20:27:24 +00:00
fabric.Line.ATTRIBUTE_NAMES = 'x1 y1 x2 y2 stroke stroke-width transform'.split(' ');
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
* @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
*/
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
];
return new fabric.Line(points, extend(parsedAttributes, options));
2010-06-09 22:34:55 +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
* @param {Object} object Object to create an instance from
* @return {fabric.Line} instance of fabric.Line
2010-06-09 22:34:55 +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];
return new fabric.Line(points, object);
2010-06-09 22:34:55 +00:00
};
2011-08-05 23:00:26 +00:00
})(typeof exports !== 'undefined' ? exports : this);