forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.class.js
106 lines (88 loc) · 2.47 KB
/
triangle.class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { });
if (fabric.Triangle) {
fabric.warn('fabric.Triangle is already defined');
return;
}
/**
* @class Triangle
* @extends fabric.Object
*/
fabric.Triangle = fabric.util.createClass(fabric.Object, /** @scope fabric.Triangle.prototype */ {
/**
* @property
* @type String
*/
type: 'triangle',
/**
* Constructor
* @method initialize
* @param options {Object} 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) {
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();
if (this.fill) {
ctx.fill();
}
if (this.stroke) {
ctx.stroke();
}
},
/**
* Returns complexity of an instance
* @method complexity
* @return {Number} complexity of this instance
*/
complexity: function() {
return 1;
},
/**
* Returns svg representation of an instance
* @method toSVG
* @return {string} svg representation of an instance
*/
toSVG: function() {
var widthBy2 = this.width / 2,
heightBy2 = this.height / 2;
var points = [
-widthBy2 + " " + heightBy2,
"0 " + -heightBy2,
widthBy2 + " " + heightBy2
].join(",");
return '<polygon ' +
'points="' + points + '" ' +
'style="' + this.getSvgStyles() + '" ' +
'transform="' + this.getSvgTransform() + '" ' +
'/>';
}
});
/**
* 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);
};
})(typeof exports != 'undefined' ? exports : this);