fabric.js/src/shapes/triangle.class.js

124 lines
3.2 KiB
JavaScript

(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { });
if (fabric.Triangle) {
fabric.warn('fabric.Triangle is already defined');
return;
}
/**
* Triangle class
* @class fabric.Triangle
* @extends fabric.Object
* @return {fabric.Triangle} thisArg
* @see {@link fabric.Triangle#initialize} for constructor definition
*/
fabric.Triangle = fabric.util.createClass(fabric.Object, /** @lends fabric.Triangle.prototype */ {
/**
* Type of an object
* @type String
* @default
*/
type: 'triangle',
/**
* Constructor
* @param {Object} [options] Options object
* @return {Object} thisArg
*/
initialize: function(options) {
options = options || { };
this.callSuper('initialize', options);
this.set('width', options.width || 100)
.set('height', options.height || 100);
},
/**
* @private
* @param ctx {CanvasRenderingContext2D} Context to render on
*/
_render: function(ctx) {
var widthBy2 = this.width / 2,
heightBy2 = this.height / 2;
ctx.beginPath();
ctx.moveTo(-widthBy2, heightBy2);
ctx.lineTo(0, -heightBy2);
ctx.lineTo(widthBy2, heightBy2);
ctx.closePath();
this._renderFill(ctx);
this._renderStroke(ctx);
},
/**
* @private
* @param ctx {CanvasRenderingContext2D} Context to render on
*/
_renderDashedStroke: function(ctx) {
var widthBy2 = this.width / 2,
heightBy2 = this.height / 2;
ctx.beginPath();
fabric.util.drawDashedLine(ctx, -widthBy2, heightBy2, 0, -heightBy2, this.strokeDashArray);
fabric.util.drawDashedLine(ctx, 0, -heightBy2, widthBy2, heightBy2, this.strokeDashArray);
fabric.util.drawDashedLine(ctx, widthBy2, heightBy2, -widthBy2, heightBy2, this.strokeDashArray);
ctx.closePath();
},
/* _TO_SVG_START_ */
/**
* Returns SVG representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup(),
widthBy2 = this.width / 2,
heightBy2 = this.height / 2,
points = [
-widthBy2 + ' ' + heightBy2,
'0 ' + -heightBy2,
widthBy2 + ' ' + heightBy2
]
.join(',');
markup.push(
'<polygon ',
'points="', points,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
'"/>'
);
return reviver ? reviver(markup.join('')) : markup.join('');
},
/* _TO_SVG_END_ */
/**
* Returns complexity of an instance
* @return {Number} complexity of this instance
*/
complexity: function() {
return 1;
}
});
/**
* Returns fabric.Triangle instance from an object representation
* @static
* @memberOf fabric.Triangle
* @param object {Object} object to create an instance from
* @return {Object} instance of Canvas.Triangle
*/
fabric.Triangle.fromObject = function(object) {
return new fabric.Triangle(object);
};
})(typeof exports !== 'undefined' ? exports : this);