forked from AToMPM/atompm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsvg.js
442 lines (369 loc) · 10.3 KB
/
libsvg.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
* Copyright 2011 by the AToMPM team and licensed under the LGPL
* See COPYING.lesser and README.md in the root of this project for full details
*/
/********************************** IMPORTS ***********************************/
try
{
var _utils = (typeof(utils) == 'undefined' ? require('./utils') : utils);
}
catch(ex)
{
throw 'missing utils.js dependency';
}
var SVG = {'types':{},'fns':{}};
/**************************** CONVENIENCE WRAPPERS ****************************/
/*---------------------------------- PUBLIC ----------------------------------*/
SVG.fns.getPointOnPathAtRatio =
function(path,ratio)
{
return new SVG.types.LinearPath(path).getPointOnPathAtRatio(ratio);
};
/*--------------------------------- INTERNAL ---------------------------------*/
SVG.fns.__getRotationMatrix =
function(a,cx,cy)
{
cx = cx || 0;
cy = cy || 0;
a *= Math.PI/180;
var cosa = Math.cos(a),
sina = Math.sin(a),
_T = SVG.fns.__getTranslationMatrix(cx,cy),
R = new SVG.types.Matrix(cosa,sina,-sina,cosa,0,0),
T = SVG.fns.__getTranslationMatrix(-cx,-cy);
return T.mult(R).mult(_T);
};
SVG.fns.__getScalingMatrix =
function(sx,sy,cx,cy)
{
cx = cx || 0;
cy = cy || 0;
sy = sy||sx;
var _T = SVG.fns.__getTranslationMatrix(cx,cy),
S = new SVG.types.Matrix(sx,0,0,sy,0,0),
T = SVG.fns.__getTranslationMatrix(-cx,-cy);
return T.mult(S).mult(_T);
};
/* return a transformation matrix given a transformation string
supported string formats are SVG and RaphaelJS
SVG: space separated mix of
rotate(_[,_,_])
translate(_,_)
scale(_[,_])
matrix(_,_,_,_,_,_)
RaphaelJS: mix of
r_[,_,_]
t_,_
s_[_[,_,_]]
m_,_,_,_,_,_,_ */
SVG.fns.__getTransformationMatrix =
function(tstr)
{
if( tstr == undefined )
return;
var M = new SVG.types.Matrix(),
f;
if( tstr.indexOf('translate') >= 0 ||
tstr.indexOf('rotate') >= 0 ||
tstr.indexOf('scale') >= 0 )
{
tstr.split(' ').forEach(
function(t)
{
if( (_ = t.match(/^rotate\((.*),(.*),(.*)\)$/)) ||
(_ = t.match(/^rotate\((.*)\)$/)) )
f = SVG.fns.__getRotationMatrix;
else if( (_ = t.match(/^translate\((.*),(.*)\)$/)) )
f = SVG.fns.__getTranslationMatrix;
else if( (_ = t.match(/^scale\((.*),(.*)\)$/)) ||
(_ = t.match(/^scale\((.*)\)$/)) )
f = SVG.fns.__getScalingMatrix;
else if( (_ = t.match(
/^matrix\((.*),(.*),(.*),(.*),(.*),(.*)\)$/)) )
f = SVG.types.Matrix;
else
throw 'invalid transformation string :: '+tstr;
M = M.mult( f.apply(this,_.slice(1).map(parseFloat)) );
});
}
else
{
tstr.match(/([rst][-\d\.,]+)/g).forEach(
function(t)
{
if( (_ = t.match(/r(.*),(.*),(.*)$/)) ||
(_ = t.match(/^r(.*)$/)) )
f = SVG.fns.__getRotationMatrix;
else if( (_ = t.match(/^t(.*),(.*)$/)) )
f = SVG.fns.__getTranslationMatrix;
else if( (_ = t.match(/^s(.*),(.*),(.*),(.*)$/)) ||
(_ = t.match(/^s(.*),(.*)$/)) ||
(_ = t.match(/^s(.*)$/)) )
f = SVG.fns.__getScalingMatrix;
else if( (_ = t.match(/^m(.*),(.*),(.*),(.*),(.*),(.*)$/)) )
f = SVG.types.Matrix;
else
throw 'invalid transformation string :: '+tstr;
M = M.mult( f.apply(this,_.slice(1).map(parseFloat)) );
});
}
return M;
};
SVG.fns.__getTranslationMatrix =
function(dx,dy)
{
return new SVG.types.Matrix(1,0,0,1,dx,dy);
};
/*********************************** TYPES ************************************/
/*------------------------------ TRANSFORMABLE -------------------------------*/
SVG.types.ATransformable =
function()
{
throw 'can\'t instantiate abstract type ATransformable';
};
/* rotate this element by 'a' degrees around (cx,cy) or (0,0) */
SVG.types.ATransformable.prototype.rotate =
function(a,cx,cy)
{
return this.__transform(SVG.fns.__getRotationMatrix(a,cx,cy));
};
/* scale this element by 'sx,sy' w.r.t. (cx,cy) or (0,0) */
SVG.types.ATransformable.prototype.scale =
function(sx,sy,cx,cy)
{
return this.__transform(SVG.fns.__getScalingMatrix(sx,sy,cx,cy));
};
/* apply the given transformation string to this element */
SVG.types.ATransformable.prototype.transform =
function(tstr)
{
return this.__transform(SVG.fns.__getTransformationMatrix(tstr));
};
/* apply the given transformation matrix to this element */
SVG.types.ATransformable.prototype.__transform =
function(T)
{
throw 'ATransformable subtype must overwrite __transform :: '+
this.constructor.name;
};
/* translate this element by 'dx,dy' */
SVG.types.ATransformable.prototype.translate =
function(dx,dy)
{
return this.__transform(SVG.fns.__getTranslationMatrix(dx,dy));
};
/*---------------------------------- MATRIX ----------------------------------*/
/* an SVG matrix:
[ a c e
b d f
0 0 1 ]
(default init is Identity) */
SVG.types.Matrix =
function(a,b,c,d,e,f)
{
if( !(this instanceof SVG.types.Matrix) )
return new SVG.types.Matrix(a,b,c,d,e,f);
if( a == undefined )
{
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.e = 0;
this.f = 0;
}
else
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
};
/* return the result of multiplying this matrix by another */
SVG.types.Matrix.prototype.mult =
function(M)
{
return new SVG.types.Matrix(
this.a*M.a + this.c*M.b,
this.b*M.a + this.d*M.b,
this.a*M.c + this.c*M.d,
this.b*M.c + this.d*M.d,
this.a*M.e + this.c*M.f + this.e,
this.b*M.e + this.d*M.f + this.f );
};
/*---------------------------------- POINT -----------------------------------*/
/* a 2D point:
{x:_,y:_}
callable via
SVG.types.Point(x,y)
SVG.types.Point(pointStr)
SVG.types.Point(point) */
SVG.types.Point =
function(__1,__2)
{
if( !(this instanceof SVG.types.Point) )
return new SVG.types.Point(__1,__2);
if( typeof(__1) == 'string' )
{
var _p = __1.split(',');
this.x = parseFloat(_p[0]);
this.y = parseFloat(_p[1]);
}
else if( __1 instanceof SVG.types.Point )
{
this.x = __1.x;
this.y = __1.y;
}
else
{
this.x = __1;
this.y = __2;
}
this._x = this.x;
this._y = this.y;
};
/* return the distance between this point and another */
SVG.types.Point.prototype.dist =
function(p)
{
return Math.sqrt(Math.pow(this.x-p.x,2) + Math.pow(this.y-p.y,2));
};
/* return the slope of an imaginary line between 2 points as the counter-
clockwise angle between line p1->p2 and the X-axis */
SVG.types.Point.prototype.slope =
function(p)
{
var dx = p.x - this.x,
dy = p.y - this.y;
return Math.atan2(dy,dx)*180/Math.PI;
};
/* apply the given transformation matrix to this point */
SVG.types.Point.prototype.__transform =
function(T)
{
if( T )
{
var x = T.a*this.x + T.c*this.y + T.e,
y = T.b*this.x + T.d*this.y + T.f;
this.x = x;
this.y = y;
}
else
{
this.x = this._x;
this.y = this._y;
}
return this;
};
_utils.extend(SVG.types.Point, SVG.types.ATransformable);
/*---------------------------------- PATH ------------------------------------*/
/* an SVG path made only of Ms and Ls
callable via
SVG.types.LinearPath(pathstr)
SVG.types.Point(points) */
SVG.types.LinearPath =
function(__1)
{
if( !(this instanceof SVG.types.LinearPath) )
return new SVG.types.LinearPath(__1);
if( typeof(__1) == 'string' )
this.points = __1.split(/[ML]/).slice(1).map(SVG.types.Point);
else
this.points = _utils.clone(__1);
this.length = -1;
this.dists = [];
};
/* compute total length while remembering each control point's distance
from the start */
SVG.types.LinearPath.prototype.__buildControlPointToLengthMap =
function()
{
this.length = 0;
this.dists = [0];
for( var i = 1; i<this.points.length; i++ )
{
this.length += this.points[i-1].dist(this.points[i]);
this.dists.push(this.length);
}
};
/* return the coordinates and orientation of the point along the path at the
specified ratio of the path's length
1. return first point if ratio is <= 0
2. return last point if ratio is >= 1
3. otherwise,
a. traverse 'dists' until find control points between which answer lies
b. interpolate between said points and return answer
#. in every case, the points orientation is returned along with it...
this is computed by finding the slope between
case 1: first and second point
case 2: next-to-last and last point
case 3: identified bounding points */
SVG.types.LinearPath.prototype.getPointOnPathAtRatio =
function(ratio)
{
var p;
if( ratio <= 0 )
{
p = new SVG.types.Point(_utils.head(this.points));
p.O = p.slope(this.points[1]);
}
else if( ratio >= 1 )
{
p = new SVG.types.Point(_utils.tail(this.points));
p.O = this.points[this.points.length-2].slope(p);
}
else
{
if( this.length == -1 )
this.__buildControlPointToLengthMap();
for( var i = 1; i<this.points.length; i++ )
if( this.dists[i]/this.length >= ratio )
{
var a = this.dists[i-1]/this.length,
b = this.dists[i]/this.length,
t = (ratio-a)/(b-a),
p1 = this.points[i-1],
p2 = this.points[i];
p = new SVG.types.Point(
p1.x+t*(p2.x-p1.x),
p1.y+t*(p2.y-p1.y));
p.O = p1.slope(p2);
break;
}
}
return p;
};
/* add/remove control points */
SVG.types.LinearPath.prototype.splice =
function(i,n,toadd)
{
//TBC
this.__buildControlPointToLengthMap();
};
/* apply the given transformation matrix to this path */
SVG.types.LinearPath.prototype.__transform =
function(T)
{
this.points.forEach(
function(p)
{
p.__transform(T);
});
return this;
};
_utils.extend(SVG.types.LinearPath, SVG.types.ATransformable);
/*----------------------------------------------------------------------------*/
SVG.types.CubicPath = function() {}; //TBC
SVG.types.Circle = function() {}; //TBC
SVG.types.Ellipse = function() {}; //TBC
SVG.types.Rectangle = function() {}; //TBC
SVG.types.Polygon = function() {}; //TBC
SVG.types.Star = function() {}; //TBC
SVG.types.Image = function() {}; //TBC
/* NOTE: 'exports' exists in back-end 'require', but not in browser import...
this ensures no errors are reported during browser imports */
var exports = exports || {};
exports.SVG = SVG;