1 (function(global) { 2 3 "use strict"; 4 5 var fabric = global.fabric || (global.fabric = { }); 6 7 if (fabric.Triangle) { 8 fabric.warn('fabric.Triangle is already defined'); 9 return; 10 } 11 12 /** 13 * @class Triangle 14 * @extends fabric.Object 15 */ 16 fabric.Triangle = fabric.util.createClass(fabric.Object, /** @scope fabric.Triangle.prototype */ { 17 18 /** 19 * @property 20 * @type String 21 */ 22 type: 'triangle', 23 24 /** 25 * Constructor 26 * @method initialize 27 * @param options {Object} options object 28 * @return {Object} thisArg 29 */ 30 initialize: function(options) { 31 options = options || { }; 32 33 this.callSuper('initialize', options); 34 35 this.set('width', options.width || 100) 36 .set('height', options.height || 100); 37 }, 38 39 /** 40 * @private 41 * @method _render 42 * @param ctx {CanvasRenderingContext2D} Context to render on 43 */ 44 _render: function(ctx) { 45 var widthBy2 = this.width / 2, 46 heightBy2 = this.height / 2; 47 48 ctx.beginPath(); 49 ctx.moveTo(-widthBy2, heightBy2); 50 ctx.lineTo(0, -heightBy2); 51 ctx.lineTo(widthBy2, heightBy2); 52 ctx.closePath(); 53 54 if (this.fill) { 55 ctx.fill(); 56 } 57 if (this.stroke) { 58 ctx.stroke(); 59 } 60 }, 61 62 /** 63 * Returns complexity of an instance 64 * @method complexity 65 * @return {Number} complexity of this instance 66 */ 67 complexity: function() { 68 return 1; 69 } 70 }); 71 72 /** 73 * Returns fabric.Triangle instance from an object representation 74 * @static 75 * @method Canvas.Trangle.fromObject 76 * @param object {Object} object to create an instance from 77 * @return {Object} instance of Canvas.Triangle 78 */ 79 fabric.Triangle.fromObject = function(object) { 80 return new fabric.Triangle(object); 81 }; 82 })(this);