-
Notifications
You must be signed in to change notification settings - Fork 2
/
svgshadow.js
1763 lines (1631 loc) · 58.9 KB
/
svgshadow.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
// http://www.w3.org/TR/SVG/paths.html
function RayToLineSegment(x, y, dx, dy, x0, y0, x1, y1)
{
var r, s, d;
//Make sure the lines aren't parallel
if (dy / dx != (y1 - y0) / (x1 - x0))
{
d = ((dx * (y1 - y0)) - dy * (x1 - x0));
if (d != 0)
{
r = (((y - y0) * (x1 - x0)) - (x - x0) * (y1 - y0)) / d;
s = (((y - y0) * dx) - (x - x0) * dy) / d;
if (r >= 0 && s >= 0 && s <= 1)
{
var intersectionX = x + r * dx;
var intersectionY = y + r * dy;
return {
x: intersectionX,
y: intersectionY,
s: r * Math.sqrt(dx * dx + dy * dy),
first: { type: 'L', startX: x0, startY: y0, endX: intersectionX, endY: intersectionY },
second: { type: 'L', startX: intersectionX, startY: intersectionY, endX: x1, endY: y1 },
intersections: 1
};
}
}
}
return null;
}
// https://www.particleincell.com/2013/cubic-line-intersection/
function SplitCubicBezier(t, x0, y0, x1, y1, x2, y2, x3, y3)
{
// Split the bezier curve at the time
// https://en.wikipedia.org/wiki/De_Casteljau's_algorithm
var x4 = x0 + (x1 - x0) * t;
var y4 = y0 + (y1 - y0) * t;
var x5 = x1 + (x2 - x1) * t;
var y5 = y1 + (y2 - y1) * t;
var x6 = x2 + (x3 - x2) * t;
var y6 = y2 + (y3 - y2) * t;
var x7 = x4 + (x5 - x4) * t;
var y7 = y4 + (y5 - y4) * t;
var x8 = x5 + (x6 - x5) * t;
var y8 = y5 + (y6 - y5) * t;
var x9 = x7 + (x8 - x7) * t;
var y9 = y7 + (y8 - y7) * t;
return {
first:
{
type: 'C',
startX: x0, startY: y0,
x1: x4, y1: y4,
x2: x7, y2: y7,
endX: x9, endY: y9
}, second:
{
type: 'C',
startX: x9, startY: y9,
x1: x8, y1: y8,
x2: x6, y2: y6,
endX: x3, endY: y3
}
};
}
// http://stackoverflow.com/a/27176424/254381
function CubicRoots(a, b, c, d)
{
var CubicRoot = function(x)
{
var y = Math.pow(Math.abs(x), 1 / 3);
return x < 0 ? -y : y;
};
if (Math.abs(a) < 1e-8) // Quadratic case, ax^2+bx+c=0
{
a = b;
b = c;
c = d;
if (Math.abs(a) < 1e-8) // Linear case, ax+b=0
{
a = b;
b = c;
if (Math.abs(a) < 1e-8) // Degenerate case
{
return [];
}
return [-b / a];
}
var D = b*b - 4*a*c;
if (Math.abs(D) < 1e-8)
{
return [-b / (2 * a)];
}
else if (D > 0)
{
return [(-b + Math.sqrt(D)) / (2 * a), (-b - Math.sqrt(D)) / (2 * a)];
}
return [];
}
// Convert to depressed cubic t^3+pt+q = 0 (subst x = t - b/3a)
var p = (3 * a * c - b * b)/(3 * a * a);
var q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
var roots;
if (Math.abs(p) < 1e-8) // p = 0 -> t^3 = -q -> t = -q^1 / 3
{
roots = [CubicRoot(-q)];
}
else if (Math.abs(q) < 1e-8) // q = 0 -> t^3 + pt = 0 -> t(t^2+p)=0
{
roots = [0].concat(p < 0 ? [Math.sqrt(-p), -Math.sqrt(-p)] : []);
}
else
{
var D = q * q / 4 + p * p * p / 27;
if (Math.abs(D) < 1e-8) // D = 0 -> two roots
{
roots = [-1.5 * q / p, 3 * q / p];
}
else if (D > 0) // Only one real root
{
var u = CubicRoot(-q / 2 - Math.sqrt(D));
roots = [u - p / (3 * u)];
}
else // D < 0, three roots, but needs to use complex numbers/trigonometric solution
{
var u = 2*Math.sqrt(-p / 3);
var t = Math.acos(3 * q / p / u) / 3; // D < 0 implies p < 0 and acos argument in [-1..1]
var k = 2 * Math.PI / 3;
roots = [u * Math.cos(t), u * Math.cos(t - k), u * Math.cos(t - 2 * k)];
}
}
// Convert back from depressed cubic
for (var i = 0; i < roots.length; i++)
{
roots[i] -= b / (3 * a);
}
return roots;
}
function BezierCoefficients(p0, p1, p2, p3)
{
return [
-p0 + 3 * p1 + -3 * p2 + p3,
3 * p0 - 6 * p1 + 3 * p2,
-3 * p0 + 3 * p1,
p0
];
}
function RayToCubicBezier(x, y, dx, dy, x0, y0, x1, y1, x2, y2, x3, y3)
{
var result = null;
var xCoefficients = BezierCoefficients(x0, x1, x2, x3);
var yCoefficients = BezierCoefficients(y0, y1, y2, y3);
var r = CubicRoots(
dy * xCoefficients[0] - dx * yCoefficients[0], // t^3
dy * xCoefficients[1] - dx * yCoefficients[1], // t^2
dy * xCoefficients[2] - dx * yCoefficients[2], // t
dy * xCoefficients[3] - dx * yCoefficients[3] + x * -dy + y * dx // 1
);
var smallestS = Number.MAX_VALUE;
var intersections = 0;
// Verify the roots are in bounds of the linear segment.
for (var i = 0; i < 3; ++i)
{
var t = r[i];
var x_ = xCoefficients[0] * t * t * t + xCoefficients[1] * t * t + xCoefficients[2] * t + xCoefficients[3];
var y_ = yCoefficients[0] * t * t * t + yCoefficients[1] * t * t + yCoefficients[2] * t + yCoefficients[3];
// Determine if the intersection is after the position in the direction of the ray.
var s;
if (dx != 0)
{
s = (x_ - x) / dx;
}
else
{
s = (y_ - y) / dy;
}
// Record only the first intersection.
if (t > 0 && t < 1.0 && s > 0)
{
intersections++;
if (s < smallestS)
{
smallestS = s;
result = SplitCubicBezier(t, x0, y0, x1, y1, x2, y2, x3, y3);
result.x = x_;
result.y = y_;
result.s = s;
}
result.intersections = intersections;
}
}
return result;
}
function CubicBezierTimeAtTangent(x0, y0, x1, y1, x2, y2, x3, y3, tx, ty)
{
// http://stackoverflow.com/a/34837312/254381
var tvals = [];
var ax = 3 * x3 - 9 * x2 + 9 * x1 - 3 * x0;
var ay = 3 * y3 - 9 * y2 + 9 * y1 - 3 * y0;
var bx = 6*x2 - 12 * x1 + 6 * x0;
var by = 6 * y2 - 12 * y1 + 6 * y0;
var cx = 3 * x1 - 3 * x0;
var cy = 3 * y1 - 3 * y0;
var den = 2 * ax * ty - 2 * ay * tx;
if (Math.abs(den) < 1E-10)
{
var num = ax * cy - ay * cx;
var den = ax * by - ay * bx;
if (den != 0)
{
var t = -num / den;
if (t >= 0 && t <= 1)
{
tvals.push(t);
}
}
}
else
{
var delta = (bx * bx - 4 * ax * cx) * ty * ty + (-2 * bx * by + 4 * ay * cx + 4 * ax * cy) * tx * ty + (by * by - 4 * ay * cy) * tx * tx;
var k = bx * ty - by * tx;
tvals = [];
if (delta >= 0 && den != 0)
{
var d = Math.sqrt(delta);
var t0 = -(k + d) / den;
var t1 = (-k + d) / den;
if (t0 >= 0 && t0 < 1)
{
tvals.push(t0);
}
if (t1 >= 0 && t1 < 1)
{
tvals.push(t1);
}
tvals.sort();
}
}
return tvals;
}
function RayToArc(x, y, dx, dy, cx, cy, rx, ry, rotation, angle, deltaAngle)
{
var testVX = Math.cos(angle + deltaAngle / 2);
var testVY = Math.sin(angle + deltaAngle / 2);
var angleVX = Math.cos(angle);
var angleVY = Math.sin(angle);
x -= cx;
y -= cy;
// Transform the ray position and direction relative to the axis-aligned ellipse.
var x_ = Math.cos(rotation) * x + Math.sin(rotation) * y;
var y_ = -Math.sin(rotation) * x + Math.cos(rotation) * y;
var dx_ = Math.cos(rotation) * dx + Math.sin(rotation) * dy;
var dy_ = -Math.sin(rotation) * dx + Math.cos(rotation) * dy;
x = x_;
y = y_;
dx = dx_;
dy = dy_;
var intersections = 0;
var rotateResult = function(resultX, resultY, distance)
{
var vx = resultX / rx;
var vy = resultY / ry;
var resultAngle = Math.atan2(vy, vx);
if (resultAngle < 0)
{
resultAngle += 2 * Math.PI;
}
var divideArc = function(inputAngle, inputDeltaAngle)
{
var x0 = Math.cos(rotation) * (rx * Math.cos(inputAngle)) - Math.sin(rotation) * (ry * Math.sin(inputAngle)) + cx;
var y0 = Math.sin(rotation) * (rx * Math.cos(inputAngle)) + Math.cos(rotation) * (ry * Math.sin(inputAngle)) + cy;
var x1 = Math.cos(rotation) * (rx * Math.cos(inputAngle + inputDeltaAngle)) - Math.sin(rotation) * (ry * Math.sin(inputAngle + inputDeltaAngle)) + cx;
var y1 = Math.sin(rotation) * (rx * Math.cos(inputAngle + inputDeltaAngle)) + Math.cos(rotation) * (ry * Math.sin(inputAngle + inputDeltaAngle)) + cy;
var largeArcFlag = Math.abs(inputDeltaAngle) > Math.PI ? 1 : 0;
var sweepFlag = inputDeltaAngle > 0 ? 1 : 0;
return { type: 'A', cx: cx, cy: cy, rx: rx, ry: ry, rotation: rotation, angle: inputAngle, deltaAngle: inputDeltaAngle, largeArcFlag: largeArcFlag, sweepFlag: sweepFlag, startX: x0, startY: y0, endX: x1, endY: y1 };
}
var s;
if (dx_ == 0)
{
s = distance / dy_;
}
else
{
s = distance / dx_;
}
var resultDeltaAngle = Math.asin(angleVX * vy - angleVY * vx);
if (deltaAngle > 0 && resultDeltaAngle < 0)
{
resultDeltaAngle += 2 * Math.PI;
}
else if (deltaAngle < 0 && resultDeltaAngle > 0)
{
resultDeltaAngle -= 2 * Math.PI;
}
var result =
{
x: Math.cos(rotation) * resultX - Math.sin(rotation) * resultY + cx,
y: Math.sin(rotation) * resultX + Math.cos(rotation) * resultY + cy,
s: s,
angle: resultAngle,
first: divideArc(angle, resultDeltaAngle),
second: divideArc(angle + resultDeltaAngle, deltaAngle - resultDeltaAngle),
};
// Make sure the result is within our arc. If not return null.
// acos(dot(a, b)) returns the angle between two normalized vectors
if (Math.acos(testVX * vx + testVY * vy) < Math.abs(deltaAngle / 2))
{
intersections++;
result.intersections = intersections;
return result;
}
else
{
return null;
}
}
if (dx == 0)
{
var ty = (ry / rx) * Math.sqrt(rx * rx - x * x);
if (dy * (ty - y) > 0)
{
return rotateResult(x, ty, Math.abs(ty - y));
}
if (dy * (-ty - y) > 0)
{
return rotateResult(x, -ty, Math.abs(-ty - y));
}
}
else
{
var closestIntersection = null;
var closestIntersectionDistance = Number.MAX_VALUE;
var a = dy / dx;
var b = (y - a * x);
var r = a * a * rx * rx + ry * ry;
var s = 2 * a * b * rx * rx;
var t = rx * rx * b * b - rx * rx * ry * ry;
var d = s * s - 4 * r * t;
if (d > 0)
{
var xi1 = (-s + Math.sqrt(d)) / (2 * r);
var xi2 = (-s - Math.sqrt(d)) / (2 * r);
var yi1 = a * xi1 + b;
var yi2 = a * xi2 + b;
var distance1 = (xi1 - x) * (xi1 - x) + (yi1 - y) * (yi1 - y);
if (dx * (xi1 - x) + dy * (yi1 - y) > 0)
{
closestIntersection = rotateResult(xi1, yi1, Math.sqrt(distance1));
if (closestIntersection)
{
closestIntersectionDistance = distance1;
}
}
if (dx * (xi2 - x) + dy * (yi2 - y) > 0)
{
var distance2 = (xi2 - x) * (xi2 - x) + (yi2 - y) * (yi2 - y);
// This must be calculated because we want to find out how many intersections there were.
var intersection = rotateResult(xi2, yi2, Math.sqrt(distance2));
if (distance2 < closestIntersectionDistance && intersection)
{
closestIntersection = intersection;
}
}
return closestIntersection;
}
else if (d == 0)
{
var xi = -s / (2 * r);
var yi = a * xi + b;
if (dx * (xi - x) + dy * (yi - y) > 0)
{
return rotateResult(xi, yi, Math.sqrt((xi - x) * (xi - x) + (yi - y) * (yi - y)));
}
}
}
return null;
}
function CubicBezierPosition(t, x0, y0, x1, y1, x2, y2, x3, y3)
{
var x4 = x0 + (x1 - x0) * t;
var y4 = y0 + (y1 - y0) * t;
var x5 = x1 + (x2 - x1) * t;
var y5 = y1 + (y2 - y1) * t;
var x6 = x2 + (x3 - x2) * t;
var y6 = y2 + (y3 - y2) * t;
var x7 = x4 + (x5 - x4) * t;
var y7 = y4 + (y5 - y4) * t;
var x8 = x5 + (x6 - x5) * t;
var y8 = y5 + (y6 - y5) * t;
var x9 = x7 + (x8 - x7) * t;
var y9 = y7 + (y8 - y7) * t;
return { x: x9, y: y9 };
}
function ArcMidPoint(cx, cy, rx, ry, rotation, angle, deltaAngle)
{
var midPointAngle = angle + deltaAngle / 2;
return {
x: Math.cos(rotation) * (rx * Math.cos(midPointAngle)) - Math.sin(rotation) * (ry * Math.sin(midPointAngle)) + cx,
y: Math.sin(rotation) * (rx * Math.cos(midPointAngle)) + Math.cos(rotation) * (ry * Math.sin(midPointAngle)) + cy
};
}
function PathToSVGShadowPath(svgPath, width, height)
{
var largeEpsilon = 0.01;
var epsilon = 0.001;
var lightDirection = { x: 1, y: 1 };
var length = Math.sqrt(lightDirection.x * lightDirection.x + lightDirection.y * lightDirection.y);
lightDirection.x /= length;
lightDirection.y /= length;
var lightDirectionNormal = { x: lightDirection.y, y: -lightDirection.x };
var startPosition = { x: 0, y: 0 };
var position = { x: 0, y: 0 };
var fullPaths = [];
var fullPath = [];
var AddPath = function(path)
{
fullPath.push(path);
};
var EndPath = function()
{
if (fullPath.length != 0)
{
var startX = fullPath[0].startX;
var startY = fullPath[0].startY;
var endX = fullPath[fullPath.length - 1].endX;
var endY = fullPath[fullPath.length - 1].endY;
if ((startX - endX) * (startX - endX) + (startY - endY) * (startY - endY) > epsilon)
{
fullPath.push({ type: 'L', startX: endX, startY: endY, endX: startX, endY: startY });
}
fullPaths.push(fullPath);
fullPath = [];
}
};
svgPath.split(/(?=[A-Za-z])/).forEach(function(operation, operationIndex)
{
var operationType = operation[0];
var args = operation.split(/[A-Za-z ]/);
args = args.map(function(arg)
{
arg = arg.split(',');
arg = arg.map(function(value) { return parseFloat(value); });
if (arg.length == 2)
{
return { x: arg[0], y: arg[1] };
}
else
{
return arg[0];
}
});
args.shift();
//console.log(operationIndex, ' ', operationType, ' ', args);
// Only M, L, C, A, H, V, Z are used
switch (operationType)
{
// Start a new sub-path at the given (x,y) coordinate. M (uppercase) indicates that absolute coordinates will follow; m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs of coordinates, the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands will be relative if the moveto is relative, and absolute if the moveto is absolute. If a relative moveto (m) appears as the first element of the path, then it is treated as a pair of absolute coordinates. In this case, subsequent pairs of coordinates are treated as relative even though the initial moveto is interpreted as an absolute moveto.
case 'M': // moveto (absolute) Parameters: (x y)+
if (fullPath.length != 0)
{
if (Math.abs(position.x - startPosition.x) > epsilon || Math.abs(position.y - startPosition.y) > epsilon)
{
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: startPosition.x, endY: startPosition.y });
}
EndPath();
}
position.x = args[0].x;
position.y = args[0].y;
startPosition = { x: position.x, y: position.y };
for (var itr = 1; itr < args.length; ++itr)
{
var x = args[itr].x;
var y = args[itr].y;
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: y });
position.x = x;
position.y = y;
}
break;
case 'm': // moveto (relative)
if (operationIndex == 0)
{
position.x = args[0].x;
position.y = args[0].y;
}
else
{
position.x += args[0].x;
position.y += args[0].y;
}
startPosition = { x: position.x, y: position.y };
for (var itr = 1; itr < args.length; ++itr)
{
var x = position.x + args[itr].x;
var y = position.y + args[itr].y;
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: y });
position.x = x;
position.y = y;
}
break;
// Close the current subpath by drawing a straight line from the current point to current subpath's initial point. Since the Z and z commands take no parameters, they have an identical effect.
case 'Z': // closepath (absolute) Parameters: (none)
case 'z': // closepath (relative)
if (position.x != startPosition.x && startPosition.y != position.y)
{
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: startPosition.x, endY: startPosition.y });
}
EndPath();
break;
// Draw a line from the current point to the given (x,y) coordinate which becomes the new current point. L (uppercase) indicates that absolute coordinates will follow; l (lowercase) indicates that relative coordinates will follow. A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the new current point is set to the final set of coordinates provided.
case 'L': // lineto (absolute) Parameters: (x y)+
for (var itr = 0; itr < args.length; ++itr)
{
var x = args[itr].x;
var y = args[itr].y;
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: y });
position.x = x;
position.y = y;
}
break;
case 'l': // lineto (relative)
for (var itr = 0; itr < args.length; itr += 3)
{
var x = position.x + args[itr].x;
var y = position.y + args[itr].y;
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: y });
position.x = x;
position.y = y;
}
break;
// Draws a horizontal line from the current point (cpx, cpy) to (x, cpy). H (uppercase) indicates that absolute coordinates will follow; h (lowercase) indicates that relative coordinates will follow. Multiple x values can be provided (although usually this doesn't make sense). At the end of the command, the new current point becomes (x, cpy) for the final value of x.
case 'H': // horizontal lineto (absolute) Parameters: x+
for (var itr = 0; itr < args.length; ++itr)
{
var x = args[itr];
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: position.y });
position.x = x;
}
break;
case 'h': // horizontal lineto (relative)
for (var itr = 0; itr < args.length; ++itr)
{
var x = position.x + args[itr];
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: x, endY: position.y });
position.x = x;
}
break;
// Draws a vertical line from the current point (cpx, cpy) to (cpx, y). V (uppercase) indicates that absolute coordinates will follow; v (lowercase) indicates that relative coordinates will follow. Multiple y values can be provided (although usually this doesn't make sense). At the end of the command, the new current point becomes (cpx, y) for the final value of y.
case 'V': // vertical lineto (absolute) Parameters: y+
for (var itr = 0; itr < args.length; ++itr)
{
var y = args[itr];
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: position.x, endY: y });
position.y = y;
}
break;
case 'v': // vertical lineto (relative)
for (var itr = 0; itr < args.length; ++itr)
{
var y = position.y + args[itr];
AddPath({ type: 'L', startX: position.x, startY: position.y, endX: position.x, endY: y });
position.y = y;
}
break;
// Draws a cubic Bézier curve from the current point to (x,y) using (x1,y1) as the control point at the beginning of the curve and (x2,y2) as the control point at the end of the curve. C (uppercase) indicates that absolute coordinates will follow; c (lowercase) indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.
case 'C': // curveto (absolute) Parameters: (x1 y1 x2 y2 x y)+
for (var itr = 0; itr < args.length; itr += 3)
{
var x1 = args[itr].x;
var y1 = args[itr].y;
var x2 = args[itr + 1].x;
var y2 = args[itr + 1].y;
var x = args[itr + 2].x;
var y = args[itr + 2].y;
var tvals = CubicBezierTimeAtTangent(position.x, position.y, x1, y1, x2, y2, x, y, lightDirection.x, lightDirection.y);
if (tvals.length > 0)
{
var split1 = SplitCubicBezier(tvals[0], position.x, position.y, x1, y1, x2, y2, x, y);
AddPath(split1.first);
if (tvals.length > 1)
{
var split2 = SplitCubicBezier((tvals[1] - tvals[0]) / (1 - tvals[0]), split1.second.startX, split1.second.startY, split1.second.x1, split1.second.y1, split1.second.x2, split1.second.y2, split1.second.endX, split1.second.endY);
AddPath(split2.first);
AddPath(split2.second);
}
else
{
AddPath(split1.second);
}
}
else
{
var normal = { x: y - position.y, y: position.x - x };
AddPath({ type: 'C', startX: position.x, startY: position.y, x1: x1, y1: y1, x2: x2, y2: y2, endX: x, endY: y });
}
position.x = x;
position.y = y;
}
break;
case 'c': // curveto (relative)
for (var itr = 0; itr < args.length; itr += 3)
{
var x1 = position.x + args[itr].x;
var y1 = position.y + args[itr].y;
var x2 = position.x + args[itr + 1].x;
var y2 = position.y + args[itr + 1].y;
var x = position.x + args[itr + 2].x;
var y = position.y + args[itr + 2].y;
var tvals = CubicBezierTimeAtTangent(position.x, position.y, x1, y1, x2, y2, x, y, lightDirection.x, lightDirection.y);
if (tvals.length > 0)
{
var split1 = SplitCubicBezier(tvals[0], position.x, position.y, x1, y1, x2, y2, x, y);
AddPath(split1.first);
if (tvals.length > 1)
{
var split2 = SplitCubicBezier((tvals[1] - tvals[0]) / (1 - tvals[0]), split1.second.startX, split1.second.startY, split1.second.x1, split1.second.y1, split1.second.x2, split1.second.y2, split1.second.endX, split1.second.endY);
AddPath(split2.first);
AddPath(split2.second);
}
else
{
AddPath(split1.second);
}
}
else
{
var normal = { x: y - position.y, y: position.x - x };
AddPath({ type: 'C', startX: position.x, startY: position.y, x1: x1, y1: y1, x2: x2, y2: y2, endX: x, endY: y });
}
position.x = x;
position.y = y;
}
break;
// Draws a cubic Bézier curve from the current point to (x,y). The first control point is assumed to be the reflection of the second control point on the previous command relative to the current point. (If there is no previous command or if the previous command was not an C, c, S or s, assume the first control point is coincident with the current point.) (x2,y2) is the second control point (i.e., the control point at the end of the curve). S (uppercase) indicates that absolute coordinates will follow; s (lowercase) indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.
case 'S': // shorthand/smooth curveto (absolute) Parameters: (x2 y2 x y)+
throw 'Not implemented';
break;
case 's': // shorthand/smooth curveto (relative)
throw 'Not implemented';
break;
// Draws a quadratic Bézier curve from the current point to (x,y) using (x1,y1) as the control point. Q (uppercase) indicates that absolute coordinates will follow; q (lowercase) indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.
case 'Q': // quadratic Bézier curveto (absolute) Parameters: (x1 y1 x y)+
throw 'Not implemented';
break;
case 'q': // quadratic Bézier curveto (relative)
throw 'Not implemented';
break;
// Draws a quadratic Bézier curve from the current point to (x,y). The control point is assumed to be the reflection of the control point on the previous command relative to the current point. (If there is no previous command or if the previous command was not a Q, q, T or t, assume the control point is coincident with the current point.) T (uppercase) indicates that absolute coordinates will follow; t (lowercase) indicates that relative coordinates will follow. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.
case 'T': // Shorthand/smooth quadratic Bézier curveto (absolute) Parameters: (x y)+
throw 'Not implemented';
break;
case 't': // Shorthand/smooth quadratic Bézier curveto (relative)
throw 'Not implemented';
break;
// Draws an elliptical arc from the current point to (x, y). The size and orientation of the ellipse are defined by two radii (rx, ry) and an x-axis-rotation, which indicates how the ellipse as a whole is rotated relative to the current coordinate system. The center (cx, cy) of the ellipse is calculated automatically to satisfy the constraints imposed by the other parameters. large-arc-flag and sweep-flag contribute to the automatic calculations and help determine how the arc is drawn.
case 'A': // elliptical arc (absolute) Parameters: (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+
for (var itr = 0; itr < args.length; itr += 4)
{
// TODO: Technically if rx or ry are 0 then this needs to be treated like a lineto operation.
var rx = args[itr].x;
var ry = args[itr].y;
var rotation = Math.PI / 180 * (args[itr + 1] % 360);
var largeArcFlag = args[itr + 2].x == 1;
var sweepFlag = args[itr + 2].y == 1;
var x = args[itr + 3].x;
var y = args[itr + 3].y;
// http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
// Use the conversion from endpoint to center parameterization in the linked documentation
var x1_ = Math.cos(rotation) * ((position.x - x) / 2) + Math.sin(rotation) * ((position.y - y) / 2);
var y1_ = -Math.sin(rotation) * ((position.x - x) / 2) + Math.cos(rotation) * ((position.y - y) / 2);
var radiiCheck = x1_ * x1_ / (rx * rx) + y1_ * y1_ / (ry * ry);
if (radiiCheck > 1)
{
rx = Math.sqrt(radiiCheck) * rx;
ry = Math.sqrt(radiiCheck) * ry;
}
var c = (largeArcFlag != sweepFlag ? 1 : -1) * Math.sqrt((rx * rx * ry * ry - rx * rx * y1_ * y1_ - ry * ry * x1_ * x1_) / (rx * rx * y1_ * y1_ + ry * ry * x1_ * x1_));
var cx_ = c * rx * y1_ / ry;
var cy_ = c * -ry * x1_ / rx;
var cx = Math.cos(rotation) * cx_ - Math.sin(rotation) * cy_ + (position.x + x) / 2;
var cy = Math.sin(rotation) * cx_ + Math.cos(rotation) * cy_ + (position.y + y) / 2;
var angleBetweenVectors = function(ux, uy, vx, vy)
{
return (ux * vy - uy * vx <= 0 ? -1 : 1) * Math.acos(Math.max(-1, (ux * vx + uy * vy) / (Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy))));
}
var angle = Math.atan2((y1_ - cy_) / ry, (x1_ - cx_) / rx);
if (angle < 0)
{
angle = 2 * Math.PI + angle;
}
var deltaAngle = angleBetweenVectors((x1_ - cx_) / rx, (y1_ - cy_) / ry, (-x1_ - cx_) / rx, (-y1_ - cy_) / ry);
deltaAngle %= Math.PI * 2;
if (!sweepFlag && deltaAngle > 0)
{
deltaAngle -= Math.PI * 2;
}
else if (sweepFlag && deltaAngle < 0)
{
deltaAngle += Math.PI * 2;
}
// Rotate the light direction by the inverse of the rotation matrix. transformLightDirectionX isn't used
//var transformLightDirectionX = Math.cos(rotation) * lightDirection.x + Math.sin(rotation) * lightDirection.y;
var transformLightDirectionY = -Math.sin(rotation) * lightDirection.x + Math.cos(rotation) * lightDirection.y;
// This will only work for light directions in the range (-1, 0) aka 180 degrees to (0, 1) 270 degrees. Our light direction is (-0.707, 0.707) aka 225 degrees.
var theta1 = Math.acos(-transformLightDirectionY * rx / Math.sqrt(transformLightDirectionY * transformLightDirectionY * rx * rx - transformLightDirectionY * transformLightDirectionY * ry * ry + ry * ry));
var theta2 = theta1 + Math.PI;
// Use the conversion from center to endpoint parameterization in the linked documentation
var addArc = function(inputAngle, inputDeltaAngle)
{
var x1 = Math.cos(rotation) * (rx * Math.cos(inputAngle)) - Math.sin(rotation) * (ry * Math.sin(inputAngle)) + cx;
var y1 = Math.sin(rotation) * (rx * Math.cos(inputAngle)) + Math.cos(rotation) * (ry * Math.sin(inputAngle)) + cy;
var x2 = Math.cos(rotation) * (rx * Math.cos(inputAngle + inputDeltaAngle)) - Math.sin(rotation) * (ry * Math.sin(inputAngle + inputDeltaAngle)) + cx;
var y2 = Math.sin(rotation) * (rx * Math.cos(inputAngle + inputDeltaAngle)) + Math.cos(rotation) * (ry * Math.sin(inputAngle + inputDeltaAngle)) + cy;
var largeArcFlag = Math.abs(inputDeltaAngle) > Math.PI ? 1 : 0;
var sweepFlag = inputDeltaAngle > 0 ? 1 : 0;
AddPath({ type: 'A', cx: cx, cy: cy, rx: rx, ry: ry, rotation: rotation, angle: inputAngle, deltaAngle: inputDeltaAngle, largeArcFlag: largeArcFlag, sweepFlag: sweepFlag, startX: x1, startY: y1, endX: x2, endY: y2 });
}
var startAngle = angle;
var endAngle = angle + deltaAngle;
if (deltaAngle > 0)
{
if (theta1 < startAngle)
{
theta1 += 2 * Math.PI;
}
if (theta2 < startAngle)
{
theta2 += 2 * Math.PI;
}
if (theta1 >= endAngle)
{
theta1 -= 2 * Math.PI;
}
if (theta2 >= endAngle)
{
theta2 -= 2 * Math.PI;
}
if (theta1 > startAngle && theta1 < endAngle && theta2 > startAngle && theta2 < endAngle)
{
var minAngle = Math.min(theta1, theta2);
var maxAngle = Math.max(theta1, theta2);
addArc(startAngle, minAngle - startAngle);
addArc(minAngle, maxAngle - minAngle);
addArc(maxAngle, endAngle - maxAngle);
}
if (theta1 > startAngle && theta1 < endAngle)
{
addArc(startAngle, theta1 - startAngle);
addArc(theta1, endAngle - theta1);
}
else if (theta2 > startAngle && theta2 < endAngle)
{
addArc(startAngle, theta2 - startAngle);
addArc(theta2, endAngle - theta2);
}
else
{
addArc(startAngle, endAngle - startAngle);
}
}
else
{
if (theta1 < endAngle)
{
theta1 += 2 * Math.PI;
}
if (theta2 < endAngle)
{
theta2 += 2 * Math.PI;
}
if (theta1 >= startAngle)
{
theta1 -= 2 * Math.PI;
}
if (theta2 >= startAngle)
{
theta2 -= 2 * Math.PI;
}
if (theta1 < startAngle && theta1 > endAngle && theta2 < startAngle && theta2 > endAngle)
{
var minAngle = Math.max(theta1, theta2);
var maxAngle = Math.min(theta1, theta2);
addArc(startAngle, minAngle - startAngle);
addArc(minAngle, maxAngle - minAngle);
addArc(maxAngle, endAngle - maxAngle);
}
else if (theta1 < startAngle && theta1 > endAngle)
{
addArc(startAngle, theta1 - startAngle);
addArc(theta1, endAngle - theta1);
}
else if (theta2 < startAngle && theta2 > endAngle)
{
addArc(startAngle, theta2 - startAngle);
addArc(theta2, endAngle - theta2);
}
else
{
addArc(startAngle, endAngle - startAngle);
}
}
position.x = x;
position.y = y;
}
break;
case 'a': // elliptical arc (relative)
for (var itr = 0; itr < args.length; itr += 4)
{
var rx = args[itr].x;
var ry = args[itr].y;
var rotation = args[itr + 1];
var largeArcFlag = args[itr + 2].x == 1;
var sweepFlag = args[itr + 2].y == 1;
var x = position.x + args[itr + 3].x;
var y = position.y + args[itr + 3].y;
throw 'Not implemented';
position.x = x;
position.y = y;
}
break;
}
});
EndPath();
var frontfacePaths = [];
var frontfacePath = [];
AddPath = function(path)
{
frontfacePath.push(path);
};
EndPath = function()
{
if (frontfacePath.length != 0)
{
frontfacePaths.push(frontfacePath);
frontfacePath = [];
}
};
// This method is kind of fragile due to floating point errors.
var PointInPath = function(fullPath, x, y)
{
// We use the ray (1, 2) for sanity since it bypasses a few edge cases when using (1, 0). Essentially when using (1, 0) the RayToCubicBezier has cases where the coefficients are near zero causing calculation problems later.
var rayX = 1 / Math.sqrt(1 * 1 + 2 * 2);
var rayY = 2 / Math.sqrt(1 * 1 + 2 * 2);
var inside = false;
for (var itr = 0; itr < fullPaths.length; ++itr)
{
var fullPath = fullPaths[itr];
for (var itrPart = 0; itrPart < fullPath.length; ++itrPart)
{
var part = fullPath[itrPart];
switch (part.type)
{
case 'L':
var intersection = RayToLineSegment(x, y, rayX, rayY, part.startX, part.startY, part.endX, part.endY);
if (intersection)
{
inside = !inside;
}
break;
case 'C':
var intersection = RayToCubicBezier(x, y, rayX, rayY, part.startX, part.startY, part.x1, part.y1, part.x2, part.y2, part.endX, part.endY);
if (intersection && intersection.intersections % 2 == 1)
{
inside = !inside;
}
break;
case 'A':
var intersection = RayToArc(x, y, rayX, rayY, part.cx, part.cy, part.rx, part.ry, part.rotation, part.angle, part.deltaAngle);
if (intersection && intersection.intersections == 1)
{
inside = !inside;
}
break;
}
}
}
return inside;
};
for (var itr = 0; itr < fullPaths.length; ++itr)
{
var fullPath = fullPaths[itr];
for (var itrPart = 0; itrPart < fullPath.length; ++itrPart)
{
var part = fullPath[itrPart];
var normalX = part.endY - part.startY;
var normalY = part.startX - part.endX;
var length = Math.sqrt(normalX * normalX + normalY * normalY);
normalX /= length;
normalY /= length;
var startDistance = Math.sqrt((part.startX + 1) * lightDirectionNormal.x + (part.startY - (height + 1)) * lightDirectionNormal.y);
var endDistance = Math.sqrt((part.endX + 1) * lightDirectionNormal.x + (part.endY - (height + 1)) * lightDirectionNormal.y);
switch (part.type)
{