polyline.class.js
7.66 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
min = fabric.util.array.min,
max = fabric.util.array.max,
toFixed = fabric.util.toFixed;
if (fabric.Polyline) {
fabric.warn('fabric.Polyline is already defined');
return;
}
/**
* Polyline class
* @class fabric.Polyline
* @extends fabric.Object
* @see {@link fabric.Polyline#initialize} for constructor definition
*/
fabric.Polyline = fabric.util.createClass(fabric.Object, /** @lends fabric.Polyline.prototype */ {
/**
* Type of an object
* @type String
* @default
*/
type: 'polyline',
/**
* Points array
* @type Array
* @default
*/
points: null,
cacheProperties: fabric.Object.prototype.cacheProperties.concat('points'),
/**
* Constructor
* @param {Array} points Array of points (where each point is an object with x and y)
* @param {Object} [options] Options object
* @return {fabric.Polyline} thisArg
* @example
* var poly = new fabric.Polyline([
* { x: 10, y: 10 },
* { x: 50, y: 30 },
* { x: 40, y: 70 },
* { x: 60, y: 50 },
* { x: 100, y: 150 },
* { x: 40, y: 100 }
* ], {
* stroke: 'red',
* left: 100,
* top: 100
* });
*/
initialize: function(points, options) {
options = options || {};
this.points = points || [];
this.callSuper('initialize', options);
this._setPositionDimensions(options);
},
_setPositionDimensions: function(options) {
var calcDim = this._calcDimensions(options), correctLeftTop;
this.width = calcDim.width;
this.height = calcDim.height;
if (!options.fromSVG) {
correctLeftTop = this.translateToGivenOrigin(
{ x: calcDim.left - this.strokeWidth / 2, y: calcDim.top - this.strokeWidth / 2 },
'left',
'top',
this.originX,
this.originY
);
}
if (typeof options.left === 'undefined') {
this.left = options.fromSVG ? calcDim.left : correctLeftTop.x;
}
if (typeof options.top === 'undefined') {
this.top = options.fromSVG ? calcDim.top : correctLeftTop.y;
}
this.pathOffset = {
x: calcDim.left + this.width / 2,
y: calcDim.top + this.height / 2
};
},
/**
* Calculate the polygon min and max point from points array,
* returning an object with left, top, widht, height to measure the
* polygon size
* @return {Object} object.left X coordinate of the polygon leftmost point
* @return {Object} object.top Y coordinate of the polygon topmost point
* @return {Object} object.width distance between X coordinates of the polygon leftmost and rightmost point
* @return {Object} object.height distance between Y coordinates of the polygon topmost and bottommost point
* @private
*/
_calcDimensions: function() {
var points = this.points,
minX = min(points, 'x') || 0,
minY = min(points, 'y') || 0,
maxX = max(points, 'x') || 0,
maxY = max(points, 'y') || 0,
width = (maxX - minX),
height = (maxY - minY);
return {
left: minX,
top: minY,
width: width,
height: height
};
},
/**
* Returns object representation of an instance
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object} Object representation of an instance
*/
toObject: function(propertiesToInclude) {
return extend(this.callSuper('toObject', propertiesToInclude), {
points: this.points.concat()
});
},
/* _TO_SVG_START_ */
/**
* Returns svg representation of an instance
* @return {Array} an array of strings with the specific svg representation
* of the instance
*/
_toSVG: function() {
var points = [], diffX = this.pathOffset.x, diffY = this.pathOffset.y,
NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS;
for (var i = 0, len = this.points.length; i < len; i++) {
points.push(
toFixed(this.points[i].x - diffX, NUM_FRACTION_DIGITS), ',',
toFixed(this.points[i].y - diffY, NUM_FRACTION_DIGITS), ' '
);
}
return [
'<' + this.type + ' ', 'COMMON_PARTS',
'points="', points.join(''),
'" />\n'
];
},
/* _TO_SVG_END_ */
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
commonRender: function(ctx) {
var point, len = this.points.length,
x = this.pathOffset.x,
y = this.pathOffset.y;
if (!len || isNaN(this.points[len - 1].y)) {
// do not draw if no points or odd points
// NaN comes from parseFloat of a empty string in parser
return false;
}
ctx.beginPath();
ctx.moveTo(this.points[0].x - x, this.points[0].y - y);
for (var i = 0; i < len; i++) {
point = this.points[i];
ctx.lineTo(point.x - x, point.y - y);
}
return true;
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
if (!this.commonRender(ctx)) {
return;
}
this._renderPaintInOrder(ctx);
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderDashedStroke: function(ctx) {
var p1, p2;
ctx.beginPath();
for (var i = 0, len = this.points.length; i < len; i++) {
p1 = this.points[i];
p2 = this.points[i + 1] || p1;
fabric.util.drawDashedLine(ctx, p1.x, p1.y, p2.x, p2.y, this.strokeDashArray);
}
},
/**
* Returns complexity of an instance
* @return {Number} complexity of this instance
*/
complexity: function() {
return this.get('points').length;
}
});
/* _FROM_SVG_START_ */
/**
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Polyline.fromElement})
* @static
* @memberOf fabric.Polyline
* @see: http://www.w3.org/TR/SVG/shapes.html#PolylineElement
*/
fabric.Polyline.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat();
/**
* Returns fabric.Polyline instance from an SVG element
* @static
* @memberOf fabric.Polyline
* @param {SVGElement} element Element to parser
* @param {Function} callback callback function invoked after parsing
* @param {Object} [options] Options object
*/
fabric.Polyline.fromElementGenerator = function(_class) {
return function(element, callback, options) {
if (!element) {
return callback(null);
}
options || (options = { });
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric[_class].ATTRIBUTE_NAMES);
parsedAttributes.fromSVG = true;
callback(new fabric[_class](points, extend(parsedAttributes, options)));
};
};
fabric.Polyline.fromElement = fabric.Polyline.fromElementGenerator('Polyline');
/* _FROM_SVG_END_ */
/**
* Returns fabric.Polyline instance from an object representation
* @static
* @memberOf fabric.Polyline
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
*/
fabric.Polyline.fromObject = function(object, callback) {
return fabric.Object._fromObject('Polyline', object, callback, 'points');
};
})(typeof exports !== 'undefined' ? exports : this);