mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-14 16:53:10 +00:00
- fabric.Text has now strokeDashArray support (only native support) - fabric.Text.fill = null should now work - Fix save/restore context in render methods => setLineDash affected drawBorder/drawControls - Add strokeLineCap (default "butt"), strokeLineJoin (default "miter") and strokeMiterLimit (default 10) - Add support for fabric.Object#fromElement for strokeDashArray (and other stroke properties) - Add @default tag to properties (JSDoc 3) - strokeDashArray now only works if stroke property is defined - Add trokeLineCap (default "round"), strokeLineJoin (default "round") to fabric.BaseBrush - Updated unit tests
205 lines
5.6 KiB
JavaScript
205 lines
5.6 KiB
JavaScript
(function(global) {
|
|
|
|
"use strict";
|
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
|
extend = fabric.util.object.extend,
|
|
min = fabric.util.array.min,
|
|
max = fabric.util.array.max,
|
|
toFixed = fabric.util.toFixed;
|
|
|
|
if (fabric.Polygon) {
|
|
fabric.warn('fabric.Polygon is already defined');
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Polygon class
|
|
* @class fabric.Polygon
|
|
* @extends fabric.Object
|
|
*/
|
|
fabric.Polygon = fabric.util.createClass(fabric.Object, /** @lends fabric.Polygon.prototype */ {
|
|
|
|
/**
|
|
* Type of an object
|
|
* @type String
|
|
* @default
|
|
*/
|
|
type: 'polygon',
|
|
|
|
/**
|
|
* Constructor
|
|
* @param {Array} points Array of points
|
|
* @param {Object} [options] Options object
|
|
* @param {Boolean} Whether points offsetting should be skipped
|
|
* @return {fabric.Polygon} thisArg
|
|
*/
|
|
initialize: function(points, options, skipOffset) {
|
|
options = options || { };
|
|
this.points = points;
|
|
this.callSuper('initialize', options);
|
|
this._calcDimensions(skipOffset);
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
_calcDimensions: function(skipOffset) {
|
|
|
|
var points = this.points,
|
|
minX = min(points, 'x'),
|
|
minY = min(points, 'y'),
|
|
maxX = max(points, 'x'),
|
|
maxY = max(points, 'y');
|
|
|
|
this.width = (maxX - minX) || 1;
|
|
this.height = (maxY - minY) || 1;
|
|
|
|
this.minX = minX;
|
|
this.minY = minY;
|
|
|
|
if (skipOffset) return;
|
|
|
|
var halfWidth = this.width / 2,
|
|
halfHeight = this.height / 2;
|
|
|
|
// change points to offset polygon into a bounding box
|
|
this.points.forEach(function(p) {
|
|
p.x -= halfWidth;
|
|
p.y -= halfHeight;
|
|
}, this);
|
|
},
|
|
|
|
/**
|
|
* Returns object representation of an instance
|
|
* @param {Array} propertiesToInclude
|
|
* @return {Object} object representation of an instance
|
|
*/
|
|
toObject: function(propertiesToInclude) {
|
|
return extend(this.callSuper('toObject', propertiesToInclude), {
|
|
points: this.points.concat()
|
|
});
|
|
},
|
|
|
|
/* _TO_SVG_START_ */
|
|
/**
|
|
* Returns svg representation of an instance
|
|
* @return {String} svg representation of an instance
|
|
*/
|
|
toSVG: function() {
|
|
var points = [],
|
|
markup = [];
|
|
|
|
for (var i = 0, len = this.points.length; i < len; i++) {
|
|
points.push(toFixed(this.points[i].x, 2), ',', toFixed(this.points[i].y, 2), ' ');
|
|
}
|
|
|
|
if (this.fill && this.fill.toLive) {
|
|
markup.push(this.fill.toSVG(this, false));
|
|
}
|
|
if (this.stroke && this.stroke.toLive) {
|
|
markup.push(this.stroke.toSVG(this, false));
|
|
}
|
|
|
|
markup.push(
|
|
'<polygon ',
|
|
'points="', points.join(''),
|
|
'" style="', this.getSvgStyles(),
|
|
'" transform="', this.getSvgTransform(),
|
|
'"/>'
|
|
);
|
|
|
|
return markup.join('');
|
|
},
|
|
/* _TO_SVG_END_ */
|
|
|
|
/**
|
|
* @private
|
|
* @param ctx {CanvasRenderingContext2D} context to render on
|
|
*/
|
|
_render: function(ctx) {
|
|
var point;
|
|
ctx.beginPath();
|
|
ctx.moveTo(this.points[0].x, this.points[0].y);
|
|
for (var i = 0, len = this.points.length; i < len; i++) {
|
|
point = this.points[i];
|
|
ctx.lineTo(point.x, point.y);
|
|
}
|
|
this._renderFill(ctx);
|
|
if (this.stroke || this.strokeDashArray) {
|
|
ctx.closePath();
|
|
this._renderStroke(ctx);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @param ctx {CanvasRenderingContext2D} context to render on
|
|
*/
|
|
_renderDashedStroke: function(ctx) {
|
|
var p1, p2;
|
|
|
|
ctx.beginPath();
|
|
for (var i = 0, len = this.points.length; i < len; i++) {
|
|
p1 = this.points[i];
|
|
p2 = this.points[i+1] || this.points[0];
|
|
fabric.util.drawDashedLine(ctx, p1.x, p1.y, p2.x, p2.y, this.strokeDashArray);
|
|
}
|
|
ctx.closePath();
|
|
},
|
|
|
|
/**
|
|
* Returns complexity of an instance
|
|
* @return {Number} complexity of this instance
|
|
*/
|
|
complexity: function() {
|
|
return this.points.length;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* List of attribute names to account for when parsing SVG element (used by `fabric.Polygon.fromElement`)
|
|
* @static
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#PolygonElement
|
|
*/
|
|
fabric.Polygon.ATTRIBUTE_NAMES = (
|
|
'fill fill-opacity opacity stroke stroke-width stroke-dasharray ' +
|
|
'stroke-linejoin stroke-linecap stroke-miterlimit transform'
|
|
).split(' ');
|
|
|
|
/**
|
|
* Returns {@link fabric.Polygon} instance from an SVG element
|
|
* @static
|
|
* @param {SVGElement} element Element to parse
|
|
* @param {Object} [options] Options object
|
|
* @return {fabric.Polygon}
|
|
*/
|
|
fabric.Polygon.fromElement = function(element, options) {
|
|
if (!element) {
|
|
return null;
|
|
}
|
|
options || (options = { });
|
|
|
|
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
|
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);
|
|
|
|
for (var i = 0, len = points.length; i < len; i++) {
|
|
// normalize coordinates, according to containing box (dimensions of which are passed via `options`)
|
|
points[i].x -= (options.width / 2) || 0;
|
|
points[i].y -= (options.height / 2) || 0;
|
|
}
|
|
|
|
return new fabric.Polygon(points, extend(parsedAttributes, options), true);
|
|
};
|
|
|
|
/**
|
|
* Returns fabric.Polygon instance from an object representation
|
|
* @static
|
|
* @param {Object} object Object to create an instance from
|
|
* @return {fabric.Polygon}
|
|
*/
|
|
fabric.Polygon.fromObject = function(object) {
|
|
return new fabric.Polygon(object.points, object, true);
|
|
};
|
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|