fabric.js/src/triangle.class.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { });
if (fabric.Triangle) {
fabric.warn('fabric.Triangle is already defined');
return;
}
/**
2012-12-13 14:36:43 +00:00
* Triangle class
* @class Triangle
* @extends fabric.Object
*/
fabric.Triangle = fabric.util.createClass(fabric.Object, /** @scope fabric.Triangle.prototype */ {
/**
2012-12-13 14:36:43 +00:00
* Type of an object
* @property
* @type String
*/
type: 'triangle',
/**
* Constructor
* @method initialize
2012-12-13 14:36:43 +00:00
* @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
* @method _render
* @param ctx {CanvasRenderingContext2D} Context to render on
*/
_render: function(ctx) {
2010-08-02 18:50:07 +00:00
var widthBy2 = this.width / 2,
heightBy2 = this.height / 2;
2010-08-02 18:50:07 +00:00
ctx.beginPath();
ctx.moveTo(-widthBy2, heightBy2);
ctx.lineTo(0, -heightBy2);
ctx.lineTo(widthBy2, heightBy2);
ctx.closePath();
this._renderFill(ctx);
if (this.stroke) {
ctx.stroke();
}
},
/**
* Returns complexity of an instance
* @method complexity
* @return {Number} complexity of this instance
*/
complexity: function() {
return 1;
},
/**
2012-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
* @method toSVG
2012-12-13 14:36:43 +00:00
* @return {String} svg representation of an instance
*/
toSVG: function() {
var markup = [],
widthBy2 = this.width / 2,
heightBy2 = this.height / 2;
var points = [
-widthBy2 + " " + heightBy2,
"0 " + -heightBy2,
widthBy2 + " " + heightBy2
].join(",");
if (this.fill && this.fill.toLive) {
markup.push(this.fill.toSVG(this, true));
}
if (this.stroke && this.stroke.toLive) {
markup.push(this.stroke.toSVG(this, true));
}
markup.push(
'<polygon ',
'points="', points,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
'"/>'
);
return markup.join('');
}
});
/**
* Returns fabric.Triangle instance from an object representation
* @static
* @method Canvas.Trangle.fromObject
* @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
})(typeof exports !== 'undefined' ? exports : this);