-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.js
528 lines (505 loc) · 17.8 KB
/
Path.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//Sets stage items
/*Path --> Defines a stage path
* @param {Point[]} points An array of points for the path
* @param {double} width Half the path's width (path will extend width from center line)
* @param {String} color The color to draw the path
* @param {String} highlightColor The color of the highlight for gradient
* @param {String} shadowColor The color to draw the dropShadow of the path.
* @param {String} startColor The color to draw the start of a path
* @param {String} startHighlightColor The color of the start for gradient
* @param {String} endColor The color to draw the end of a path
* @param {String} endHighlightColor The color to draw the end for gradient
* @param {Boolean} hide If false, the path is not drawn. If true it is drawn
*/
function Path(
points,
width,
color,
highlightColor,
shadowColor,
startColor,
startHighlightColor,
endColor,
endHighlightColor,
hide
) {
//passed fields
this.points = new Array();
for (var i = 0; i < points.length; i++) {
this.points.push(points[i]);
}
this.width = width;
this.color = color;
this.highlightColor = highlightColor;
this.shadowColor = shadowColor;
this.startColor = startColor;
this.startHighlightColor = startHighlightColor;
this.endColor = endColor;
this.endHighlightColor = endHighlightColor;
this.hide = hide;
//derived fields
this.vectors = new Array(); //normalized direction vectors between points
this.rotations = new Array(); //The rotations between points (in radians)
this.vectorLengths = new Array(); //The length between each point in the path
this.totalLength; //the total length of the path.
this.rectangles = new Array(); //the points of each rectangle (used for gradients)
this.rectanglesPrint = new Array(); //The points for each rectangle (used for printing)
this.rectanglesBig = new Array(); //rectangles with 1.2 width padding (used for tower placement)
this.getRectangles();
this.intersections = new Array(); //The points for the intersections between rectangles
this.getIntersections();
this.startPoints = new Array(); //The points for the starting cap
this.getStartPoints();
this.endPoints = new Array(); //The points for the ending cap
this.getEndPoints();
this.uvInverses = new Array(); //transformation inverses (used to check if a point is inside path)
this.getUVInverses();
}
/*Draws all parts of the paths
* @param {CanvasRenderingContext2D} ctx
*/
Path.prototype.draw = function (ctx) {
if (!this.hide) {
this.drawRectangles(ctx);
this.drawIntersections(ctx);
this.drawStartCap(ctx);
this.drawEndCap(ctx);
}
};
/*Draws the path "rectangles"
* @param {CanvasRenderingContext2D} ctx
*/
Path.prototype.drawRectangles = function (ctx) {
//loop through all rectangles
for (var i = 0; i < this.rectangles.length; i++) {
//set up gradients
ctx.fillStyle = this.color;
var grd = ctx.createLinearGradient(
this.rectangles[i][0].x,
this.rectangles[i][0].y,
this.rectangles[i][3].x,
this.rectangles[i][3].y
);
grd.addColorStop(0.0, this.color);
grd.addColorStop(0.5, this.highlightColor);
grd.addColorStop(1.0, this.color);
ctx.fillStyle = grd;
//draw geometry
ctx.beginPath();
ctx.moveTo(this.rectanglesPrint[i][0].x, this.rectanglesPrint[i][0].y);
ctx.lineTo(this.rectanglesPrint[i][1].x, this.rectanglesPrint[i][1].y);
ctx.lineTo(this.rectanglesPrint[i][2].x, this.rectanglesPrint[i][2].y);
ctx.lineTo(this.rectanglesPrint[i][3].x, this.rectanglesPrint[i][3].y);
ctx.lineTo(this.rectanglesPrint[i][0].x, this.rectanglesPrint[i][0].y);
ctx.closePath();
ctx.fill();
}
};
/* Draws all intersections between each rectangle
* @param {CanvasRenderingContext2D} ctx
*/
Path.prototype.drawIntersections = function (ctx) {
//loop through intersections
for (var i = 0; i < this.intersections.length; i++) {
//gradients
ctx.fillStyle = this.color;
var grd = ctx.createLinearGradient(
this.rectangles[i][0].x,
this.rectangles[i][0].y,
this.rectangles[i][3].x,
this.rectangles[i][3].y
);
grd.addColorStop(0.0, this.color);
grd.addColorStop(0.5, this.highlightColor);
grd.addColorStop(1.0, this.color);
var grd2 = ctx.createLinearGradient(
this.rectangles[i + 1][0].x,
this.rectangles[i + 1][0].y,
this.rectangles[i + 1][3].x,
this.rectangles[i + 1][3].y
);
grd2.addColorStop(0.0, this.color);
grd2.addColorStop(0.5, this.highlightColor);
grd2.addColorStop(1.0, this.color);
ctx.fillStyle = grd2;
//geometry
ctx.beginPath();
ctx.moveTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.lineTo(this.intersections[i][1].x, this.intersections[i][1].y);
ctx.quadraticCurveTo(
this.intersections[i][2].x,
this.intersections[i][2].y,
this.intersections[i][3].x,
this.intersections[i][3].y
);
ctx.lineTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.closePath();
ctx.fill();
//geometry for other gradient
//clip
ctx.save();
ctx.fillStyle = grd;
ctx.beginPath();
ctx.moveTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.lineTo(this.intersections[i][1].x, this.intersections[i][1].y);
ctx.lineTo(this.intersections[i][2].x, this.intersections[i][2].y);
ctx.lineTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.clip();
ctx.beginPath();
ctx.moveTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.lineTo(this.intersections[i][1].x, this.intersections[i][1].y);
ctx.quadraticCurveTo(
this.intersections[i][2].x,
this.intersections[i][2].y,
this.intersections[i][3].x,
this.intersections[i][3].y
);
ctx.lineTo(this.intersections[i][0].x, this.intersections[i][0].y);
ctx.closePath();
ctx.fill();
ctx.restore();
}
};
/*Draws the start cap
* @param {CanvasRenderingContext2D} ctx
*/
Path.prototype.drawStartCap = function (ctx) {
//gradient
ctx.fillStyle = this.startColor;
var startGrd = ctx.createLinearGradient(
this.rectangles[0][0].x,
this.rectangles[0][0].y,
this.rectangles[0][3].x,
this.rectangles[0][3].y
);
startGrd.addColorStop(0.0, this.startColor);
startGrd.addColorStop(0.5, this.startHighlightColor);
startGrd.addColorStop(1.0, this.startColor);
ctx.fillStyle = startGrd;
//geometry
ctx.beginPath();
ctx.moveTo(this.startPoints[0].x, this.startPoints[0].y);
ctx.lineTo(this.startPoints[1].x, this.startPoints[1].y);
ctx.lineTo(this.startPoints[2].x, this.startPoints[2].y);
ctx.quadraticCurveTo(
this.startPoints[3].x,
this.startPoints[3].y,
this.startPoints[0].x,
this.startPoints[0].y
);
ctx.closePath();
ctx.fill();
};
/*Draws the ending cap
* @param {CanvasRenderingContext2D} ctx
*/
Path.prototype.drawEndCap = function (ctx) {
//gradient
ctx.fillStyle = this.endColor;
var endGrd = ctx.createLinearGradient(
this.rectangles[this.rectangles.length - 1][0].x,
this.rectangles[this.rectangles.length - 1][0].y,
this.rectangles[this.rectangles.length - 1][3].x,
this.rectangles[this.rectangles.length - 1][3].y
);
endGrd.addColorStop(0.0, this.endColor);
endGrd.addColorStop(0.5, this.endHighlightColor);
endGrd.addColorStop(1.0, this.endColor);
ctx.fillStyle = endGrd;
//geometry
ctx.beginPath();
ctx.moveTo(this.endPoints[0].x, this.endPoints[0].y);
ctx.lineTo(this.endPoints[1].x, this.endPoints[1].y);
ctx.lineTo(this.endPoints[2].x, this.endPoints[2].y);
ctx.quadraticCurveTo(
this.endPoints[3].x,
this.endPoints[3].y,
this.endPoints[0].x,
this.endPoints[0].y
);
ctx.closePath();
ctx.fill();
};
/*calculates the rectangles between each point
* After this function, the following arrays should be populated:
* this.rectangles, this.rectanglesPrint, this.vectors
*/
Path.prototype.getRectangles = function () {
for (var i = 0, n = this.points.length - 1; i < n; i++) {
//get vectors
var dx = this.points[i + 1].x - this.points[i].x;
var dy = this.points[i + 1].y - this.points[i].y;
//get a normalized dx, dy
var dMag = Math.sqrt(dx * dx + dy * dy);
//push the length of the vector
this.vectorLengths.push(dMag);
var pVector;
if (dMag == 0) {
//zero length vectors... just push the original ones
pVector = new Point(dx, dy);
} else {
pVector = new Point(dx / dMag, dy / dMag);
}
this.vectors.push(pVector);
//find rotation
var rotation = Math.acos(pVector.x);
if (dy < 0) {
rotation = Math.PI * 2 - rotation;
}
this.rotations.push(rotation);
//console.log(dx + "," + dy);
//get perpendicular vectors
var pdx = -1 * dy;
var pdy = dx;
//normalize the perpendicular vectors and multiply by width
var pMag = Math.sqrt(pdx * pdx + pdy * pdy);
var pdx = (pdx * this.width) / pMag;
var pdy = (pdy * this.width) / pMag;
//get the points for each rectangle
//point 1
var x1 = this.points[i].x + pdx;
var y1 = this.points[i].y + pdy;
var p1 = new Point(x1, y1);
var p1Print = new Point(x1, y1);
var p1Big = new Point(
p1.x + pdx * 1.1 - pVector.x * 1.3 * this.width,
p1.y + pdy * 1.1 - pVector.y * 1.3 * this.width
);
//set up the rectangles
var curRectangle = new Array();
var curRectanglePrint = new Array();
var curRectangleBig = new Array();
curRectangle.push(p1);
curRectanglePrint.push(p1Print);
curRectangleBig.push(p1Big);
//point 2
var x2 = x1 + dx;
var y2 = y1 + dy;
var p2 = new Point(x2, y2);
var p2Print = new Point(x2, y2);
var p2Big = new Point(
x2 + pdx * 1.1 + pVector.x * 1.3 * this.width,
y2 + pdy * 1.1 + pVector.y * 1.3 * this.width
);
curRectangle.push(p2);
curRectanglePrint.push(p2Print);
curRectangleBig.push(p2Big);
//point 3
var x3 = x2 + -2 * pdx;
var y3 = y2 + -2 * pdy;
var p3 = new Point(x3, y3);
var p3Print = new Point(x3, y3);
var p3Big = new Point(
x3 - pdx * 1.1 + pVector.x * 1.3 * this.width,
y3 - pdy * 1.1 + pVector.y * 1.3 * this.width
);
curRectangle.push(p3);
curRectanglePrint.push(p3Print);
curRectangleBig.push(p3Big);
//point 4
var x4 = x3 + -1 * dx;
var y4 = y3 + -1 * dy;
var p4 = new Point(x4, y4);
var p4Print = new Point(x4, y4);
var p4Big = new Point(
x4 - pdx * 1.1 - pVector.x * 1.3 * this.width,
y4 - pdy * 1.1 - pVector.y * 1.3 * this.width
);
curRectangle.push(p4);
curRectanglePrint.push(p4Print);
curRectangleBig.push(p4Big);
this.rectangles.push(curRectangle);
this.rectanglesPrint.push(curRectanglePrint);
this.rectanglesBig.push(curRectangleBig);
}
this.totalLength = 0;
//get the total length
for (var i = 0; i < this.vectorLengths.length; i++) {
this.totalLength += this.vectorLengths[i];
}
};
//get the intersections between boxes
Path.prototype.getIntersections = function () {
//console.log(this.rectangles.length);
for (var i = 0; i < this.rectangles.length - 1; i++) {
var intersection = new Array();
//get p and q points (for top (1) and bottom (2))
var p1 = new Point(this.rectangles[i][0].x, this.rectangles[i][0].y);
var p2 = new Point(this.rectangles[i][3].x, this.rectangles[i][3].y);
var q1 = new Point(
this.rectangles[i + 1][1].x,
this.rectangles[i + 1][1].y
);
var q2 = new Point(
this.rectangles[i + 1][2].x,
this.rectangles[i + 1][2].y
);
//get r and s vectors (for top(1) and bottom(2))
var r1 = new Point(
this.rectangles[i][1].x - p1.x,
this.rectangles[i][1].y - p1.y
);
var r2 = new Point(
this.rectangles[i][2].x - p2.x,
this.rectangles[i][2].y - p2.y
);
var s1 = new Point(
this.rectangles[i + 1][0].x - q1.x,
this.rectangles[i + 1][0].y - q1.y
);
var s2 = new Point(
this.rectangles[i + 1][3].x - q2.x,
this.rectangles[i + 1][3].y - q2.y
);
//find r cross s (for top(1) and bottom(2))
var rxs1 = this.crossProduct(r1, s1);
var rxs2 = this.crossProduct(r2, s2);
//console.log(rxs1 + " " + rxs2);
//collinear
if (Math.abs(rxs1) <= 0.00001) {
intersection.push(p1);
intersection.push(p1);
intersection.push(p1);
intersection.push(p1);
} else {
//not collinear
//find q - p (for top(1) and bottom(2)))
var qMinusP1 = new Point(q1.x - p1.x, q1.y - p1.y);
var qMinusP2 = new Point(q2.x - p2.x, q2.y - p2.y);
//console.log(qMinusP1.x + " " + qMinusP1.y + "," + qMinusP2.x + " " + qMinusP2.y);
//calculate t and u
var t1 = this.crossProduct(qMinusP1, s1) / rxs1;
var t2 = this.crossProduct(qMinusP2, s2) / rxs2;
var u1 = this.crossProduct(qMinusP1, r1) / rxs1;
var u2 = this.crossProduct(qMinusP2, r2) / rxs2;
//console.log(this.crossProduct(qMinusP1, s1));
//console.log(t1 + " " + t2 + " " + u1 + " " + u2);
//solve for the intersections (for top(1) and bottom(2))
var i1 = new Point(p1.x + t1 * r1.x, p1.y + t1 * r1.y);
var i2 = new Point(p2.x + t2 * r2.x, p2.y + t2 * r2.y);
//test which line intersects... push appropriate geometries
if (t1 < 1) {
intersection.push(i1);
intersection.push(this.rectangles[i][2]);
intersection.push(i2);
intersection.push(this.rectangles[i + 1][3]);
this.rectanglesPrint[i][1] = i1;
this.rectanglesPrint[i + 1][0] = i1;
} else {
intersection.push(i2);
intersection.push(this.rectangles[i][1]);
intersection.push(i1);
intersection.push(this.rectangles[i + 1][0]);
this.rectanglesPrint[i][2] = i2;
this.rectanglesPrint[i + 1][3] = i2;
}
}
this.intersections.push(intersection);
}
};
//gets the start points for the start of the path
Path.prototype.getStartPoints = function () {
var p0 = new Point(this.rectangles[0][0].x, this.rectangles[0][0].y);
var p1 = new Point(
this.points[0].x + this.vectors[0].x * this.width * 2,
this.points[0].y + this.vectors[0].y * this.width * 2
);
var p2 = new Point(this.rectangles[0][3].x, this.rectangles[0][3].y);
var p3 = new Point(
this.points[0].x + -1 * this.vectors[0].x * this.width,
this.points[0].y + -1 * this.vectors[0].y * this.width
);
this.startPoints.push(p0);
this.startPoints.push(p1);
this.startPoints.push(p2);
this.startPoints.push(p3);
};
//gets the points for the end of the path
Path.prototype.getEndPoints = function () {
var p0 = new Point(
this.rectangles[this.rectangles.length - 1][1].x,
this.rectangles[this.rectangles.length - 1][1].y
);
var p1 = new Point(
this.points[this.rectangles.length].x +
this.vectors[this.rectangles.length - 1].x * -this.width * 2,
this.points[this.rectangles.length].y +
this.vectors[this.rectangles.length - 1].y * -this.width * 2
);
var p2 = new Point(
this.rectangles[this.rectangles.length - 1][2].x,
this.rectangles[this.rectangles.length - 1][2].y
);
var p3 = new Point(
this.points[this.rectangles.length].x +
this.vectors[this.rectangles.length - 1].x * this.width,
this.points[this.rectangles.length].y +
this.vectors[this.rectangles.length - 1].y * this.width
);
this.endPoints.push(p0);
this.endPoints.push(p1);
this.endPoints.push(p2);
this.endPoints.push(p3);
};
/*returns the crossProduct of two vectors
@param {Point} v The first vector to cross
@param {point} w The second vector to cross
@return {Number} The v X w
*/
Path.prototype.crossProduct = function (v, w) {
return v.x * w.y - v.y * w.x;
};
/*creates transformation inverses... used to decide if a point is inside the path
*/
Path.prototype.getUVInverses = function () {
for (var i = 0, n = this.rectanglesBig.length; i < n; i++) {
//for(var i = 1, n = 2; i < n; i++){
//rectangle vectors (u and v)
var u = new Point(
this.rectanglesBig[i][1].x - this.rectanglesBig[i][0].x,
this.rectanglesBig[i][1].y - this.rectanglesBig[i][0].y
);
var v = new Point(
this.rectanglesBig[i][3].x - this.rectanglesBig[i][0].x,
this.rectanglesBig[i][3].y - this.rectanglesBig[i][0].y
);
//find length squared
var uMagSquared = u.x * u.x + u.y * u.y;
var vMagSquared = v.x * v.x + v.y * v.y;
//find the inverse transposed vector
var uInverse = new Point(u.x / uMagSquared, u.y / uMagSquared);
var vInverse = new Point(v.x / vMagSquared, v.y / vMagSquared);
var uvInverse = new Array();
//add this transposed vector to the array of inverses
uvInverse.push(uInverse);
uvInverse.push(vInverse);
this.uvInverses.push(uvInverse);
}
};
/*tells whether a point is inside the pathname
@param {Point} p The point to check
@return {Boolean} true (inside the rectangle (inclusive)) false (outside the rectangle)
*/
Path.prototype.inPath = function (p) {
for (var i = 0, n = this.uvInverses.length; i < n; i++) {
//subtract current rectangles
var pPrime = new Point(
p.x - this.rectanglesBig[i][0].x,
p.y - this.rectanglesBig[i][0].y
);
//multiply by inverse matrix
var pTransformed = new Point(
this.uvInverses[i][0].x * pPrime.x + this.uvInverses[i][0].y * pPrime.y,
this.uvInverses[i][1].x * pPrime.x + this.uvInverses[i][1].y * pPrime.y
);
if (
pTransformed.x >= 0 &&
pTransformed.x <= 1 &&
pTransformed.y >= 0 &&
pTransformed.y <= 1
) {
return true;
}
}
return false;
};