2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
"use strict";
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { });
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
if (fabric.Triangle) {
|
|
|
|
|
fabric.warn('fabric.Triangle is already defined');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-10-14 00:53:12 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Triangle class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Triangle
|
2010-10-14 21:42:39 +00:00
|
|
|
* @extends fabric.Object
|
2013-04-25 18:21:32 +00:00
|
|
|
* @return {fabric.Triangle} thisArg
|
2010-10-14 21:42:39 +00:00
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Triangle = fabric.util.createClass(fabric.Object, /** @lends fabric.Triangle.prototype */ {
|
2012-10-14 00:53:12 +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-07-24 05:03:28 +00:00
|
|
|
type: 'triangle',
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-24 05:03:28 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Constructor
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options object
|
2010-07-24 05:03:28 +00:00
|
|
|
* @return {Object} thisArg
|
|
|
|
|
*/
|
|
|
|
|
initialize: function(options) {
|
|
|
|
|
options = options || { };
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-24 05:03:28 +00:00
|
|
|
this.callSuper('initialize', options);
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-24 05:03:28 +00:00
|
|
|
this.set('width', options.width || 100)
|
|
|
|
|
.set('height', options.height || 100);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-24 05:03:28 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param ctx {CanvasRenderingContext2D} Context to render on
|
2010-07-24 05:03:28 +00:00
|
|
|
*/
|
2012-10-14 00:53:12 +00:00
|
|
|
_render: function(ctx) {
|
2010-08-02 18:50:07 +00:00
|
|
|
var widthBy2 = this.width / 2,
|
|
|
|
|
heightBy2 = this.height / 2;
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-08-02 18:50:07 +00:00
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.moveTo(-widthBy2, heightBy2);
|
|
|
|
|
ctx.lineTo(0, -heightBy2);
|
|
|
|
|
ctx.lineTo(widthBy2, heightBy2);
|
|
|
|
|
ctx.closePath();
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2013-04-22 13:16:58 +00:00
|
|
|
this._renderFill(ctx);
|
2013-05-04 16:25:57 +00:00
|
|
|
this._renderStroke(ctx);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param ctx {CanvasRenderingContext2D} Context to render on
|
|
|
|
|
*/
|
|
|
|
|
_renderDashedStroke: function(ctx) {
|
|
|
|
|
var widthBy2 = this.width / 2,
|
|
|
|
|
heightBy2 = this.height / 2;
|
2013-04-22 13:16:58 +00:00
|
|
|
|
2013-05-04 16:25:57 +00:00
|
|
|
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();
|
2010-07-24 05:03:28 +00:00
|
|
|
},
|
2012-10-14 00:53:12 +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-07-25 17:39:21 +00:00
|
|
|
var markup = this._createBaseSVGMarkup(),
|
2013-02-23 16:02:52 +00:00
|
|
|
widthBy2 = this.width / 2,
|
2012-01-02 21:14:20 +00:00
|
|
|
heightBy2 = this.height / 2;
|
|
|
|
|
|
|
|
|
|
var points = [
|
|
|
|
|
-widthBy2 + " " + heightBy2,
|
|
|
|
|
"0 " + -heightBy2,
|
|
|
|
|
widthBy2 + " " + heightBy2
|
|
|
|
|
].join(",");
|
|
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
markup.push(
|
|
|
|
|
'<polygon ',
|
|
|
|
|
'points="', points,
|
|
|
|
|
'" style="', this.getSvgStyles(),
|
|
|
|
|
'" transform="', this.getSvgTransform(),
|
|
|
|
|
'"/>'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return markup.join('');
|
2013-05-09 18:19:06 +00:00
|
|
|
},
|
|
|
|
|
/* _TO_SVG_END_ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns complexity of an instance
|
|
|
|
|
* @return {Number} complexity of this instance
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return 1;
|
2010-07-24 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
});
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-24 05:03:28 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns fabric.Triangle instance from an object representation
|
2010-07-24 05:03:28 +00:00
|
|
|
* @static
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Triangle
|
2010-07-24 05:03:28 +00:00
|
|
|
* @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);
|
|
|
|
|
};
|
2011-08-05 23:00:26 +00:00
|
|
|
|
2013-04-22 13:16:58 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|