forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.class.js
168 lines (145 loc) · 4.61 KB
/
line.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//= require "object.class"
(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
parentSet = fabric.Object.prototype.set,
coordProps = { 'x1': 1, 'x2': 1, 'y1': 1, 'y2': 1 };
if (fabric.Line) {
fabric.warn('fabric.Line is already defined');
return;
}
/**
* @class Line
* @extends fabric.Object
*/
fabric.Line = fabric.util.createClass(fabric.Object, /** @scope fabric.Line.prototype */ {
/**
* @property
* @type String
*/
type: 'line',
/**
* Constructor
* @method initialize
* @param {Array} points Array of points
* @param {Object} [options] Options object
* @return {fabric.Line} thisArg
*/
initialize: function(points, options) {
if (!points) {
points = [0, 0, 0, 0];
}
this.callSuper('initialize', options);
this.set('x1', points[0]);
this.set('y1', points[1]);
this.set('x2', points[2]);
this.set('y2', points[3]);
this._setWidthHeight();
},
_setWidthHeight: function() {
this.set('width', (this.x2 - this.x1) || 1);
this.set('height', (this.y2 - this.y1) || 1);
this.set('left', this.x1 + this.width / 2);
this.set('top', this.y1 + this.height / 2);
},
set: function(name, value) {
parentSet.call(this, name, value);
if (name in coordProps) {
this._setWidthHeight();
}
return this;
},
/**
* @private
* @method _render
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
ctx.beginPath();
// move from center (of virtual box) to its left/top corner
ctx.moveTo(this.width === 1 ? 0 : (-this.width / 2), this.height === 1 ? 0 : (-this.height / 2));
ctx.lineTo(this.width === 1 ? 0 : (this.width / 2), this.height === 1 ? 0 : (this.height / 2));
ctx.lineWidth = this.strokeWidth;
// TODO: test this
// make sure setting "fill" changes color of a line
// (by copying fillStyle to strokeStyle, since line is stroked, not filled)
var origStrokeStyle = ctx.strokeStyle;
ctx.strokeStyle = ctx.fillStyle;
ctx.stroke();
ctx.strokeStyle = origStrokeStyle;
},
/**
* Returns complexity of an instance
* @method complexity
* @return {Number} complexity
*/
complexity: function() {
return 1;
},
/**
* Returns object representation of an instance
* @methd toObject
* @return {Object}
*/
toObject: function() {
return extend(this.callSuper('toObject'), {
x1: this.get('x1'),
y1: this.get('y1'),
x2: this.get('x2'),
y2: this.get('y2')
});
},
/**
* Returns svg representation of an instance
* @method toSVG
* @return {string} svg representation of an instance
*/
toSVG: function() {
return [
'<line ',
'x1="', this.get('x1'), '" ',
'y1="', this.get('y1'), '" ',
'x2="', this.get('x2'), '" ',
'y2="', this.get('y2'), '" ',
'style="', this.getSvgStyles(), '" ',
'/>'
].join('');
}
});
/**
* List of attribute names to account for when parsing SVG element (used by `fabric.Line.fromElement`)
* @static
* @see http://www.w3.org/TR/SVG/shapes.html#LineElement
*/
fabric.Line.ATTRIBUTE_NAMES = 'x1 y1 x2 y2 stroke stroke-width transform'.split(' ');
/**
* Returns fabric.Line instance from an SVG element
* @static
* @method fabric.Line.fromElement
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {fabric.Line} instance of fabric.Line
*/
fabric.Line.fromElement = function(element, options) {
var parsedAttributes = fabric.parseAttributes(element, fabric.Line.ATTRIBUTE_NAMES);
var points = [
parsedAttributes.x1 || 0,
parsedAttributes.y1 || 0,
parsedAttributes.x2 || 0,
parsedAttributes.y2 || 0
];
return new fabric.Line(points, extend(parsedAttributes, options));
};
/**
* Returns fabric.Line instance from an object representation
* @static
* @method fabric.Line.fromObject
* @param {Object} object Object to create an instance from
* @return {fabric.Line} instance of fabric.Line
*/
fabric.Line.fromObject = function(object) {
var points = [object.x1, object.y1, object.x2, object.y2];
return new fabric.Line(points, object);
};
})(typeof exports != 'undefined' ? exports : this);