-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcake.js
7658 lines (6832 loc) · 211 KB
/
cake.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
/*
CAKE - Canvas Animation Kit Experiment
Copyright (C) 2007 Ilmari Heikkinen
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/**
Delete the first instance of obj from the array.
@param obj The object to delete
@return true on success, false if array contains no instances of obj
@type boolean
@addon
*/
Array.prototype.deleteFirst = function(obj) {
for (var i=0; i<this.length; i++) {
if (this[i] == obj) {
this.splice(i,1)
return true
}
}
return false
}
Array.prototype.stableSort = function(cmp) {
// hack to work around Chrome's qsort
for(var i=0; i<this.length; i++) {
this[i].__arrayPos = i;
}
return this.sort(Array.__stableSorter(cmp));
}
Array.__stableSorter = function(cmp) {
return (function(c1, c2) {
var r = cmp(c1,c2);
if (!r) { // hack to work around Chrome's qsort
return c1.__arrayPos - c2.__arrayPos
}
return r;
});
}
/**
Compares two arrays for equality. Returns true if the arrays are equal.
*/
Array.prototype.equals = function(array) {
if (!array) return false
if (this.length != array.length) return false
for (var i=0; i<this.length; i++) {
var a = this[i]
var b = array[i]
if (a.equals && typeof(a.equals) == 'function') {
if (!a.equals(b)) return false
} else if (a != b) {
return false
}
}
return true
}
/**
Rotates the first element of an array to be the last element.
Rotates last element to be the first element when backToFront is true.
@param {boolean} backToFront Whether to move the last element to the front or not
@return The last element when backToFront is false, the first element when backToFront is true
@addon
*/
Array.prototype.rotate = function(backToFront) {
if (backToFront) {
this.unshift(this.pop())
return this[0]
} else {
this.push(this.shift())
return this[this.length-1]
}
}
/**
Returns a random element from the array.
@return A random element
@addon
*/
Array.prototype.pick = function() {
return this[Math.floor(Math.random()*this.length)]
}
Array.prototype.flatten = function() {
var a = []
for (var i=0; i<this.length; i++) {
var e = this[i]
if (e.flatten) {
var ef = e.flatten()
for (var j=0; j<ef.length; j++) {
a[a.length] = ef[j]
}
} else {
a[a.length] = e
}
}
return a
}
Array.prototype.take = function() {
var a = []
for (var i=0; i<this.length; i++) {
var e = []
for (var j=0; j<arguments.length; j++) {
e[j] = this[i][arguments[j]]
}
a[i] = e
}
return a
}
if (!Array.prototype.pluck) {
Array.prototype.pluck = function(key) {
var a = []
for (var i=0; i<this.length; i++) {
a[i] = this[i][key]
}
return a
}
}
Array.prototype.set = function(key, value) {
for (var i=0; i<this.length; i++) {
this[i][key] = value
}
}
Array.prototype.allWith = function() {
var a = []
topLoop:
for (var i=0; i<this.length; i++) {
var e = this[i]
for (var j=0; j<arguments.length; j++) {
if (!this[i][arguments[j]])
continue topLoop
}
a[a.length] = e
}
return a
}
Element.prototype.append = function() {
for(var i=0; i<arguments.length; i++) {
if (typeof(arguments[i]) == 'string') {
this.appendChild(T(arguments[i]))
} else {
this.appendChild(arguments[i])
}
}
}
// some common helper methods
if (!Function.prototype.bind) {
/**
Creates a function that calls this function in the scope of the given
object.
var obj = { x: 'obj' }
var f = function() { return this.x }
window.x = 'window'
f()
// => 'window'
var g = f.bind(obj)
g()
// => 'obj'
@param object Object to bind this function to
@return Function bound to object
@addon
*/
Function.prototype.bind = function(object) {
var t = this
return function() {
return t.apply(object, arguments)
}
}
}
if (!Array.prototype.last) {
/**
Returns the last element of the array.
@return The last element of the array
@addon
*/
Array.prototype.last = function() {
return this[this.length-1]
}
}
if (!Array.prototype.indexOf) {
/**
Returns the index of obj if it is in the array.
Returns -1 otherwise.
@param obj The object to find from the array.
@return The index of obj or -1 if obj isn't in the array.
@addon
*/
Array.prototype.indexOf = function(obj) {
for (var i=0; i<this.length; i++)
if (obj == this[i]) return i
return -1
}
}
if (!Array.prototype.includes) {
/**
Returns true if obj is in the array.
Returns false if it isn't.
@param obj The object to find from the array.
@return True if obj is in the array, false if it isn't
@addon
*/
Array.prototype.includes = function(obj) {
return (this.indexOf(obj) >= 0);
}
}
/**
Iterate function f over each element of the array and return an array
of the return values.
@param f Function to apply to each element
@return An array of return values from applying f on each element of the array
@type Array
@addon
*/
Array.prototype.map = function(f) {
var na = new Array(this.length)
if (f)
for (var i=0; i<this.length; i++) na[i] = f(this[i], i, this)
else
for (var i=0; i<this.length; i++) na[i] = this[i]
return na
}
Array.prototype.forEach = function(f) {
for (var i=0; i<this.length; i++) f(this[i], i, this)
}
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(f, s) {
var i = 0
if (arguments.length == 1) {
s = this[0]
i++
}
for(; i<this.length; i++) {
s = f(s, this[i], i, this)
}
return s
}
}
if (!Array.prototype.find) {
Array.prototype.find = function(f) {
for(var i=0; i<this.length; i++) {
if (f(this[i], i, this)) return this[i]
}
}
}
if (!String.prototype.capitalize) {
/**
Returns a copy of this string with the first character uppercased.
@return Capitalized version of the string
@type String
@addon
*/
String.prototype.capitalize = function() {
return this.replace(/^./, this.slice(0,1).toUpperCase())
}
}
if (!String.prototype.escape) {
/**
Returns a version of the string that can be used as a string literal.
@return Copy of string enclosed in double-quotes, with double-quotes
inside string escaped.
@type String
@addon
*/
String.prototype.escape = function() {
return '"' + this.replace(/"/g, '\\"') + '"'
}
}
if (!String.prototype.splice) {
String.prototype.splice = function(start, count, replacement) {
return this.slice(0,start) + replacement + this.slice(start+count)
}
}
if (!String.prototype.strip) {
/**
Returns a copy of the string with preceding and trailing whitespace
removed.
@return Copy of string sans surrounding whitespace.
@type String
@addon
*/
String.prototype.strip = function() {
return this.replace(/^\s+|\s+$/g, '')
}
}
if (!window['$A']) {
/**
Creates a new array from an object with #length.
*/
$A = function(obj) {
var a = new Array(obj.length)
for (var i=0; i<obj.length; i++)
a[i] = obj[i]
return a
}
}
if (!window['$']) {
$ = function(id) {
return document.getElementById(id)
}
}
if (!Math.sinh) {
/**
Returns the hyperbolic sine of x.
@param x The value for x
@return The hyperbolic sine of x
@addon
*/
Math.sinh = function(x) {
return 0.5 * (Math.exp(x) - Math.exp(-x))
}
/**
Returns the inverse hyperbolic sine of x.
@param x The value for x
@return The inverse hyperbolic sine of x
@addon
*/
Math.asinh = function(x) {
return Math.log(x + Math.sqrt(x*x + 1))
}
}
if (!Math.cosh) {
/**
Returns the hyperbolic cosine of x.
@param x The value for x
@return The hyperbolic cosine of x
@addon
*/
Math.cosh = function(x) {
return 0.5 * (Math.exp(x) + Math.exp(-x))
}
/**
Returns the inverse hyperbolic cosine of x.
@param x The value for x
@return The inverse hyperbolic cosine of x
@addon
*/
Math.acosh = function(x) {
return Math.log(x + Math.sqrt(x*x - 1))
}
}
/**
Creates and configures a DOM element.
The tag of the element is given by name.
If params is a string, it is used as the innerHTML of the created element.
If params is a DOM element, it is appended to the created element.
If params is an object, it is treated as a config object and merged
with the created element.
If params is a string or DOM element, the third argument is treated
as the config object.
Special attributes of the config object:
* content
- if content is a string, it is used as the innerHTML of the
created element
- if content is an element, it is appended to the created element
* style
- the style object is merged with the created element's style
@param {String} name The tag for the created element
@param params The content or config for the created element
@param config The config for the created element if params is content
@return The created DOM element
*/
E = function(name, params, config) {
var el = document.createElement(name)
if (params) {
if (typeof(params) == 'string') {
el.innerHTML = params
params = config
} else if (params.DOCUMENT_NODE) {
el.appendChild(params)
params = config
}
if (params) {
if (params.style) {
var style = params.style
params = Object.clone(params)
delete params.style
Object.forceExtend(el.style, style)
}
if (params.content) {
if (typeof(params.content) == 'string') {
el.appendChild(T(params.content))
} else {
el.appendChild(params.content)
}
params = Object.clone(params)
delete params.content
}
Object.forceExtend(el, params)
}
}
return el
}
// Safari requires each canvas to have a unique id.
E.lastCanvasId = 0
/**
Creates and returns a canvas element with width w and height h.
@param {int} w The width for the canvas
@param {int} h The height for the canvas
@param config Optional config object to pass to E()
@return The created canvas element
*/
E.canvas = function(w,h,config) {
var id = 'canvas-uuid-' + E.lastCanvasId
E.lastCanvasId++
if (!config) config = {}
return E('canvas', Object.extend(config, {id: id, width: w, height: h}))
}
/**
Shortcut for document.createTextNode.
@param {String} text The text for the text node
@return The created text node
*/
T = function(text) {
return document.createTextNode(text)
}
/**
Merges the src object's attributes with the dst object, ignoring errors.
@param dst The destination object
@param src The source object
@return The dst object
@addon
*/
Object.forceExtend = function(dst, src) {
for (var i in src) {
try{ dst[i] = src[i] } catch(e) {}
}
return dst
}
// In case Object.extend isn't defined already, set it to Object.forceExtend.
if (!Object.extend)
Object.extend = Object.forceExtend
/**
Merges the src object's attributes with the dst object, preserving all dst
object's current attributes.
@param dst The destination object
@param src The source object
@return The dst object
@addon
*/
Object.conditionalExtend = function(dst, src) {
for (var i in src) {
if (dst[i] == null)
dst[i] = src[i]
}
return dst
}
/**
Creates and returns a shallow copy of the src object.
@param src The source object
@return A clone of the src object
@addon
*/
Object.clone = function(src) {
if (!src || src == true)
return src
switch (typeof(src)) {
case 'string':
return Object.extend(src+'', src)
break
case 'number':
return src
break
case 'function':
obj = eval(src.toSource())
return Object.extend(obj, src)
break
case 'object':
if (src instanceof Array) {
return Object.extend([], src)
} else {
return Object.extend({}, src)
}
break
}
}
/**
Creates and returns an Image object, with source URL set to src and
onload handler set to onload.
@param {String} src The source URL for the image
@param {Function} onload The onload handler for the image
@return The created Image object
@type {Image}
*/
Object.loadImage = function(src, onload) {
var img = new Image()
if (onload)
img.onload = onload
img.src = src
return img
}
/**
Returns true if image is fully loaded and ready for use.
@param image The image to check
@return Whether the image is loaded or not
@type {boolean}
@addon
*/
Object.isImageLoaded = function(image) {
if (image.tagName == 'CANVAS') return true
if (!image.complete) return false
if (image.naturalWidth == null) return true
return !!image.naturalWidth
}
/**
Sums two objects.
*/
Object.sum = function(a,b) {
if (a instanceof Array) {
if (b instanceof Array) {
var ab = []
for (var i=0; i<a.length; i++) {
ab[i] = a[i] + b[i]
}
return ab
} else {
return a.map(function(v){ return v + b })
}
} else if (b instanceof Array) {
return b.map(function(v){ return v + a })
} else {
return a + b
}
}
/**
Substracts b from a.
*/
Object.sub = function(a,b) {
if (a instanceof Array) {
if (b instanceof Array) {
var ab = []
for (var i=0; i<a.length; i++) {
ab[i] = a[i] - b[i]
}
return ab
} else {
return a.map(function(v){ return v - b })
}
} else if (b instanceof Array) {
return b.map(function(v){ return a - v })
} else {
return a - b
}
}
if (!window.Mouse) Mouse = {}
/**
Returns the coordinates for a mouse event relative to element.
Element must be the target for the event.
@param element The element to compare against
@param event The mouse event
@return An object of form {x: relative_x, y: relative_y}
*/
Mouse.getRelativeCoords = function(element, event) {
var xy = {x:0, y:0}
var osl = 0
var ost = 0
var el = element
while (el) {
osl += el.offsetLeft
ost += el.offsetTop
el = el.offsetParent
}
xy.x = event.pageX - osl
xy.y = event.pageY - ost
return xy
}
Browser = (function(){
var ua = window.navigator.userAgent
var khtml = ua.match(/KHTML/)
var gecko = ua.match(/Gecko/)
var webkit = ua.match(/WebKit\/\d+/)
var ie = ua.match(/Explorer/)
if (khtml) return 'KHTML'
if (gecko) return 'Gecko'
if (webkit) return 'Webkit'
if (ie) return 'IE'
return 'UNKNOWN'
})()
Mouse.LEFT = 0
Mouse.MIDDLE = 1
Mouse.RIGHT = 2
if (Browser == 'IE') {
Mouse.LEFT = 1
Mouse.MIDDLE = 4
}
/**
Klass is a function that returns a constructor function.
The constructor function calls #initialize with its arguments.
The parameters to Klass have their prototypes or themselves merged with the
constructor function's prototype.
Finally, the constructor function's prototype is merged with the constructor
function. So you can write Shape.getArea.call(this) instead of
Shape.prototype.getArea.call(this).
Shape = Klass({
getArea : function() {
raise('No area defined!')
}
})
Rectangle = Klass(Shape, {
initialize : function(x, y) {
this.x = x
this.y = y
},
getArea : function() {
return this.x * this.y
}
})
Square = Klass(Rectangle, {
initialize : function(s) {
Rectangle.initialize.call(this, s, s)
}
})
new Square(5).getArea()
//=> 25
@return Constructor object for the class
*/
Klass = function() {
var c = function() {
this.initialize.apply(this, arguments)
}
c.ancestors = $A(arguments)
c.prototype = {}
for(var i = 0; i<arguments.length; i++) {
var a = arguments[i]
if (a.prototype) {
Object.extend(c.prototype, a.prototype)
} else {
Object.extend(c.prototype, a)
}
}
Object.extend(c, c.prototype)
return c
}
Curves = {
angularDistance : function(a, b) {
var pi2 = Math.PI*2
var d = (b - a) % pi2
if (d > Math.PI) d -= pi2
if (d < -Math.PI) d += pi2
return d
},
linePoint : function(a, b, t) {
return [a[0]+(b[0]-a[0])*t, a[1]+(b[1]-a[1])*t]
},
quadraticPoint : function(a, b, c, t) {
// var d = this.linePoint(a,b,t)
// var e = this.linePoint(b,c,t)
// return this.linePoint(d,e,t)
var dx = a[0]+(b[0]-a[0])*t
var ex = b[0]+(c[0]-b[0])*t
var x = dx+(ex-dx)*t
var dy = a[1]+(b[1]-a[1])*t
var ey = b[1]+(c[1]-b[1])*t
var y = dy+(ey-dy)*t
return [x,y]
},
cubicPoint : function(a, b, c, d, t) {
var ax3 = a[0]*3
var bx3 = b[0]*3
var cx3 = c[0]*3
var ay3 = a[1]*3
var by3 = b[1]*3
var cy3 = c[1]*3
return [
a[0] + t*(bx3 - ax3 + t*(ax3-2*bx3+cx3 + t*(bx3-a[0]-cx3+d[0]))),
a[1] + t*(by3 - ay3 + t*(ay3-2*by3+cy3 + t*(by3-a[1]-cy3+d[1])))
]
},
linearValue : function(a,b,t) {
return a + (b-a)*t
},
quadraticValue : function(a,b,c,t) {
var d = a + (b-a)*t
var e = b + (c-b)*t
return d + (e-d)*t
},
cubicValue : function(a,b,c,d,t) {
var a3 = a*3, b3 = b*3, c3 = c*3
return a + t*(b3 - a3 + t*(a3-2*b3+c3 + t*(b3-a-c3+d)))
},
catmullRomPoint : function (a,b,c,d, t) {
var af = ((-t+2)*t-1)*t*0.5
var bf = (((3*t-5)*t)*t+2)*0.5
var cf = ((-3*t+4)*t+1)*t*0.5
var df = ((t-1)*t*t)*0.5
return [
a[0]*af + b[0]*bf + c[0]*cf + d[0]*df,
a[1]*af + b[1]*bf + c[1]*cf + d[1]*df
]
},
catmullRomAngle : function (a,b,c,d, t) {
var dx = 0.5 * (c[0] - a[0] + 2*t*(2*a[0] - 5*b[0] + 4*c[0] - d[0]) +
3*t*t*(3*b[0] + d[0] - a[0] - 3*c[0]))
var dy = 0.5 * (c[1] - a[1] + 2*t*(2*a[1] - 5*b[1] + 4*c[1] - d[1]) +
3*t*t*(3*b[1] + d[1] - a[1] - 3*c[1]))
return Math.atan2(dy, dx)
},
catmullRomPointAngle : function (a,b,c,d, t) {
var p = this.catmullRomPoint(a,b,c,d,t)
var a = this.catmullRomAngle(a,b,c,d,t)
return {point:p, angle:a}
},
lineAngle : function(a,b) {
return Math.atan2(b[1]-a[1], b[0]-a[0])
},
quadraticAngle : function(a,b,c,t) {
var d = this.linePoint(a,b,t)
var e = this.linePoint(b,c,t)
return this.lineAngle(d,e)
},
cubicAngle : function(a, b, c, d, t) {
var e = this.quadraticPoint(a,b,c,t)
var f = this.quadraticPoint(b,c,d,t)
return this.lineAngle(e,f)
},
lineLength : function(a,b) {
var x = (b[0]-a[0])
var y = (b[1]-a[1])
return Math.sqrt(x*x + y*y)
},
squareLineLength : function(a,b) {
var x = (b[0]-a[0])
var y = (b[1]-a[1])
return x*x + y*y
},
quadraticLength : function(a,b,c, error) {
var p1 = this.linePoint(a,b,2/3)
var p2 = this.linePoint(b,c,1/3)
return this.cubicLength(a,p1,p2,c, error)
},
cubicLength : (function() {
var bezsplit = function(v) {
var vtemp = [v.slice(0)]
for (var i=1; i < 4; i++) {
vtemp[i] = [[],[],[],[]]
for (var j=0; j < 4-i; j++) {
vtemp[i][j][0] = 0.5 * (vtemp[i-1][j][0] + vtemp[i-1][j+1][0])
vtemp[i][j][1] = 0.5 * (vtemp[i-1][j][1] + vtemp[i-1][j+1][1])
}
}
var left = []
var right = []
for (var j=0; j<4; j++) {
left[j] = vtemp[j][0]
right[j] = vtemp[3-j][j]
}
return [left, right]
}
var addifclose = function(v, error) {
var len = 0
for (var i=0; i < 3; i++) {
len += Curves.lineLength(v[i], v[i+1])
}
var chord = Curves.lineLength(v[0], v[3])
if ((len - chord) > error) {
var lr = bezsplit(v)
len = addifclose(lr[0], error) + addifclose(lr[1], error)
}
return len
}
return function(a,b,c,d, error) {
if (!error) error = 1
return addifclose([a,b,c,d], error)
}
})(),
quadraticLengthPointAngle : function(a,b,c,lt,error) {
var p1 = this.linePoint(a,b,2/3)
var p2 = this.linePoint(b,c,1/3)
return this.cubicLengthPointAngle(a,p1,p2,c, error)
},
cubicLengthPointAngle : function(a,b,c,d,lt,error) {
// this thing outright rapes the GC.
// how about not creating a billion arrays, hmm?
var len = this.cubicLength(a,b,c,d,error)
var point = a
var prevpoint = a
var lengths = []
var prevlensum = 0
var lensum = 0
var tl = lt*len
var segs = 20
var fac = 1/segs
for (var i=1; i<=segs; i++) { // FIXME get smarter
prevpoint = point
point = this.cubicPoint(a,b,c,d, fac*i)
prevlensum = lensum
lensum += this.lineLength(prevpoint, point)
if (lensum >= tl) {
if (lensum == prevlensum)
return {point: point, angle: this.lineAngle(a,b)}
var dl = lensum - tl
var dt = dl / (lensum-prevlensum)
return {point: this.linePoint(prevpoint, point, 1-dt),
angle: this.cubicAngle(a,b,c,d, fac*(i-dt)) }
}
}
return {point: d.slice(0), angle: this.lineAngle(c,d)}
}
}
/**
Color helper functions.
*/
Colors = {
/**
Converts an HSL color to its corresponding RGB color.
@param h Hue in degrees (0 .. 359)
@param s Saturation (0.0 .. 1.0)
@param l Lightness (0 .. 255)
@return The corresponding RGB color as [r,g,b]
@type Array
*/
hsl2rgb : function(h,s,l) {
var r,g,b
if (s == 0) {
r=g=b=v
} else {
var q = (l < 0.5 ? l * (1+s) : l+s-(l*s))
var p = 2 * l - q
var hk = (h % 360) / 360
var tr = hk + 1/3
var tg = hk
var tb = hk - 1/3
if (tr < 0) tr++
if (tr > 1) tr--
if (tg < 0) tg++
if (tg > 1) tg--
if (tb < 0) tb++
if (tb > 1) tb--
if (tr < 1/6)
r = p + ((q-p)*6*tr)
else if (tr < 1/2)
r = q
else if (tr < 2/3)
r = p + ((q-p)*6*(2/3 - tr))
else
r = p
if (tg < 1/6)
g = p + ((q-p)*6*tg)
else if (tg < 1/2)
g = q
else if (tg < 2/3)
g = p + ((q-p)*6*(2/3 - tg))
else
g = p
if (tb < 1/6)
b = p + ((q-p)*6*tb)
else if (tb < 1/2)
b = q
else if (tb < 2/3)
b = p + ((q-p)*6*(2/3 - tb))
else
b = p
}
return [r,g,b]
},
/**
Converts an HSV color to its corresponding RGB color.
@param h Hue in degrees (0 .. 359)
@param s Saturation (0.0 .. 1.0)
@param v Value (0 .. 255)
@return The corresponding RGB color as [r,g,b]
@type Array
*/
hsv2rgb : function(h,s,v) {
var r,g,b
if (s == 0) {
r=g=b=v
} else {
h = (h % 360)/60.0
var i = Math.floor(h)
var f = h-i
var p = v * (1-s)
var q = v * (1-s*f)
var t = v * (1-s*(1-f))
switch (i) {
case 0: