fabric.js/src/shapes/line.class.js

385 lines
10 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
2013-10-05 18:21:28 +00:00
* @see {@link fabric.Line#initialize} for constructor definition
*/
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
* @default
*/
2010-06-09 22:34:55 +00:00
type: 'line',
/**
* x value or first line edge
* @type Number
* @default
*/
2014-01-18 17:01:19 +00:00
x1: 0,
/**
* y value or first line edge
* @type Number
* @default
*/
2014-01-18 17:01:19 +00:00
y1: 0,
/**
* x value or second line edge
* @type Number
* @default
*/
2014-01-18 17:01:19 +00:00
x2: 0,
/**
* y value or second line edge
* @type Number
* @default
*/
2014-01-18 17:01:19 +00:00
y2: 0,
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 = { });
this.width = Math.abs(this.x2 - this.x1) || 1;
this.height = Math.abs(this.y2 - this.y1) || 1;
2012-06-28 19:14:19 +00:00
this.left = 'left' in options
? options.left
: this._getLeftToOriginX();
this.top = 'top' in options
? options.top
: this._getTopToOriginY();
2010-06-09 22:34:55 +00:00
},
/**
* @private
* @param {String} key
* @param {Any} value
*/
_set: function(key, value) {
this[key] = value;
2014-01-17 16:51:16 +00:00
if (typeof coordProps[key] !== 'undefined') {
this._setWidthHeight();
}
return this;
},
/**
* @private
* @return {Number} leftToOriginX Distance from left edge of canvas to originX of Line.
*/
_getLeftToOriginX: makeEdgeToOriginGetter(
{ // property names
origin: 'originX',
axis1: 'x1',
axis2: 'x2',
dimension: 'width',
},
{ // possible values of origin
nearest: 'left',
center: 'center',
farthest: 'right',
}
),
/**
* @private
* @return {Number} topToOriginY Distance from top edge of canvas to originY of Line.
*/
_getTopToOriginY: makeEdgeToOriginGetter(
{ // property names
origin: 'originY',
axis1: 'y1',
axis2: 'y2',
dimension: 'height',
},
{ // possible values of origin
nearest: 'top',
center: 'center',
farthest: 'bottom',
}
),
/**
* @private
* @return {Number} centerToCenterX Distance from center of path group to horizontal center of Line.
*/
_getCenterToCenterX: makeCenterToCenterGetter(
{ // property names
origin: 'originX',
coordinate: 'left',
dimension: 'width',
},
{ // possible non-center values of origin
nearest: 'left',
farthest: 'right',
}
),
/**
* @private
* @return {Number} centerToOriginY Distance from center of path group to vertical center of Line.
*/
_getCenterToCenterY: makeCenterToCenterGetter(
{ // property names
origin: 'originY',
coordinate: 'top',
dimension: 'height',
},
{ // possible non-center values of origin
nearest: 'top',
farthest: 'bottom',
}
),
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 === 'path-group';
if (isInPathGroup && !this.transformMatrix) {
ctx.translate(
this._getCenterToCenterX(),
this._getCenterToCenterY()
);
}
if (!this.strokeDashArray || this.strokeDashArray && supportsLineDash) {
// move from center (of virtual box) to its left/top corner
// we can't assume x1, y1 is top left and x2, y2 is bottom right
var xMult = this.x1 <= this.x2 ? -1 : 1;
var yMult = this.y1 <= this.y2 ? -1 : 1;
ctx.moveTo(
this.width === 1 ? 0 : (xMult * this.width / 2),
this.height === 1 ? 0 : (yMult * this.height / 2));
ctx.lineTo(
this.width === 1 ? 0 : (xMult * -1 * this.width / 2),
this.height === 1 ? 0 : (yMult * -1 * 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.stroke && 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
xMult = this.x1 <= this.x2 ? -1 : 1,
yMult = this.y1 <= this.y2 ? -1 : 1,
x = this.width === 1 ? 0 : xMult * this.width / 2,
y = this.height === 1 ? 0 : yMult * 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
/**
* Returns object representation of an instance
2010-06-09 22:34:55 +00:00
* @methd toObject
2013-09-26 12:12:02 +00:00
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @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')
});
},
/* _TO_SVG_START_ */
/**
2012-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
2012-12-13 14:36:43 +00:00
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup();
markup.push(
'<line ',
'x1="', this.get('x1'),
'" y1="', this.get('y1'),
'" x2="', this.get('x2'),
'" y2="', this.get('y2'),
'" style="', this.getSvgStyles(),
'"/>'
);
return reviver ? reviver(markup.join('')) : markup.join('');
},
/* _TO_SVG_END_ */
/**
* Returns complexity of an instance
* @return {Number} complexity
*/
complexity: function() {
return 1;
2010-06-09 22:34:55 +00:00
}
});
/* _FROM_SVG_START_ */
/**
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
* @memberOf fabric.Line
* @see http://www.w3.org/TR/SVG/shapes.html#LineElement
*/
fabric.Line.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat('x1 y1 x2 y2'.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
* @memberOf fabric.Line
* @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
};
/* _FROM_SVG_END_ */
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
* @memberOf fabric.Line
* @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
/**
* Produces a function that calculates distance from canvas edge to Line origin.
*/
function makeEdgeToOriginGetter(propertyNames, originValues) {
var origin = propertyNames.origin,
axis1 = propertyNames.axis1,
axis2 = propertyNames.axis2,
dimension = propertyNames.dimension,
nearest = originValues.nearest,
center = originValues.center,
farthest = originValues.farthest;
return function() {
switch (this.get(origin)) {
case nearest:
return Math.min(this.get(axis1), this.get(axis2));
case center:
return Math.min(this.get(axis1), this.get(axis2)) + (0.5 * this.get(dimension));
case farthest:
return Math.max(this.get(axis1), this.get(axis2));
}
};
}
/**
* Produces a function that calculates distance from path group center to center of Line dimension.
*
*/
function makeCenterToCenterGetter(propertyNames, originValues) {
var origin = propertyNames.origin,
coordinate = propertyNames.coordinate,
dimension = propertyNames.dimension,
nearest = originValues.nearest,
farthest = originValues.farthest;
return function() {
/*
* Line coords are distances from left-top of canvas to origin of line.
*
* To render line in a path-group, we need to translate them to distances
* from center of path-group to center of line.
*/
var toPathGroupEdge = (-0.5 * this.group.get(dimension))
var toLineCenter = 0; // assume center
if (this.get(origin) === nearest) {
toLineCenter = +0.5 * this.get(dimension);
} else if (this.get(origin) === farthest) {
toLineCenter = -0.5 * this.get(dimension);
} // else center, don't change the initial value
return toPathGroupEdge + this.get(coordinate) + toLineCenter;
};
}
})(typeof exports !== 'undefined' ? exports : this);