-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCee.js
2069 lines (1904 loc) · 74.1 KB
/
Cee.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
/**
* Cee.js v0.3.0
*
* https://github.com/roblarsen/CeeJS
*
* Copyright 2014, Rob Larsen
*
* MIT license http://roblarsen.github.io/CeeJS/MIT-license.txt
*
* build: 1420043916991
*
*/
/**
* @todo animations -
* https://developer.mozilla.org/en/Canvas_tutorial/Basic_animations
* @todo Manage current position better. We're fuzzy on what x,y actually means.
* It's easy enough to give options, we just need to come up with
* a default.
* For starters, ADD boundingBox property for shapes, which we can then
* expose as whatever current X,Y scheme we'd default to and then then
* whatever people want then set up a configruation piece. Set default
* position when the Canvas is created and then allow overrides at
* any point.
* Something like ctx.setOrigin ( args )
*/
(function( window ){
"use strict";
var document = window.document;
var Cee = ( function() {
// Define a local copy of Cee
var Cee = function( selector, params ) {
return new Cee.prototype._init( selector , params );
};
Cee.prototype = {
/**
* Creates a new Cee object
*
* @name Cee.js
* @function
* @constructor
* @param {string} selector A string indicating the id of an HTML
* Canvas element or the name of the Canvas
* Element to be created
* @param {object} params An object containing the following parameters
* @param {string} params.fillStyle Default fill style. Defaults to
* "#000000";
* @param {string} params.font Default font. Defaults to "10px
* sans-serif";
* @param {string} params.globalAlpha Default alpha. Defaults to 1;
* @param {string} params.globalCompositeOperation Default global
* composition
* operation. Defaults
* to "source-over";
* @param {string} params.lineCap Default line cap. Defaults to
* "butt";
* @param {string} params.lineJoin Default line join. Defaults to
* "miter";
* @param {string} params.lineWidth Default line width. Defaults to 1;
* @param {string} params.miterLimit Default miter limit. Defaults
* to 10;
* @param {string} params.shadowBlur Default shadowBlur. Defaults
* to 0;
* @param {string} params.shadowColor Default shadowColor. Defaults to
* "rgba(0, 0, 0, 0)";
* @param {string} params.shadowOffsetX Default shadodOffsetX.
* Defaults to 0;
* @param {string} params.shadowOffsetY Default shadowOffsetY.
* Defaults to 0;
* @param {string} params.strokeStyle Default strokeStyle. Defaults
* to "#000000";
* @param {string} params.textAlign Default textAlign. Defaults to
* "start";
* @param {string} params.textBaseline Default textBaseline. Defaults
* to "alphabetic";
* @property {number} width The width of the current canvas element
* @property {number} height The height of the current canvas element
*/
_init: function( selector, params ) {
params = params || {};
var container, context;
if ( document.getElementById( selector ) ) {
container = document.getElementById( selector );
} else {
container = document.createElement("canvas");
container.width = params.width || container.width;
container.height = params.height || container.height;
container.id = selector;
}
if ( container.nodeName.toLowerCase() !== "canvas") {
var canvas = document.createElement("canvas");
canvas.width = container.offsetHeight;
canvas.height = container.offsetWidth;
canvas.id = "bigc";
container.appendChild( canvas );
context = document.getElementById("bigc");
throw "The provided ID wasn't a canvas element. A canvas element" +
"with id 'bigc' created as a child of the supplied node.";
}
// Default properties
// These are all available to set at intialization
var xCurrentPos = 0,
yCurrentPos = 0,
bbCurrent = null;
context = container.getContext("2d");
context.fillStyle = params.fillStyle || context.fillStyle;
context.font = params.font || context.font;
context.globalAlpha = params.globalAlpha || context.globalAlpha;
context.globalCompositeOperation = params.globalCompositeOperation ||
context.globalCompositeOperation;
context.lineCap = params.lineCap || context.lineCap;
context.lineJoin = params.lineJoin || context.lineJoin;
context.lineWidth = params.lineWidth || context.lineWidth;
context.miterLimit = params.miterLimit || context.miterLimit;
context.shadowBlur = params.shadowBlur || context.shadowBlur;
context.shadowColor = params.shadowColor || context.shadowColor;
context.shadowOffsetX = params.shadodOffsetX || context.shadowOffsetX;
context.shadowOffsetY = params.shadowOffsetY || context.shadowOffsetY;
context.strokeStyle = params.strokeStyle || context.strokeStyle;
context.textAlign = params.textAlign || context.textAlign;
context.textBaseline = params.textBaseline || context.textBaseline;
/**
* Get or set the current x and y coordinates of the 'cursor'
* @name currentPos
* @function
* @param {number} x the x coordinate
* @param {number} y the new y coordinate
*/
var currentPos = function( x, y ) {
if ( x !== undefined &&
y !== undefined &&
typeof( x ) === "number" &&
typeof( y ) === "number") {
xCurrentPos = x;
yCurrentPos = y;
return {
x: xCurrentPos,
y: yCurrentPos
};
} else {
return {
x: xCurrentPos,
y: yCurrentPos
};
}
},
/**
* @ignore
*/
_valOrDefault = function( x, current ){
if ( x !== undefined &&
typeof( x ) === "number"){
return x;
} else {
return current;
}
},
/**
* Adds an arc with the given control points and radius to the current
* subpath, connected to the previous point by a straight line.
*
* @name arc
* @function
* @param {number} x the x coordinate
* @param {number} y the y coordinate
* @param {number} radius the radius of the arc
* @param {number} start the starting angle
* @param {number} end the ending angle
* @param {Boolean} [counter] Omitted or set to false this argument
* will arc counter/anti clockwise
*/
arc = function( x, y, radius, start, end, counter ) {
var relativeStart = start % (2 * Math.PI);
var relativeEnd = end % (2 * Math.PI);
if (counter){
relativeStart = end % (2 * Math.PI);
relativeEnd = start % (2 * Math.PI);
}
var extremes = math.getArcCircleExtremes( x, y, radius,
relativeStart, relativeEnd );
var startX = x + radius * Math.cos( relativeStart );
var startY = y + radius * Math.sin( relativeStart );
var endX = x + radius * Math.cos( relativeEnd );
var endY = y + radius * Math.sin( relativeEnd );
var xArr = [startX, endX].concat(extremes[0]);
var yArr = [startY, endY].concat(extremes[1]);
context.arc( x, y, radius, start, end, counter || false );
currentPos( x, y );
_boundingBox({
x1: Math.max.apply(this, xArr),
y1: Math.max.apply(this, yArr),
x2: Math.min.apply(this, xArr),
y2: Math.min.apply(this, yArr)
});
return this;
},
/**
* Adds points to the subpath such that the arc described by the
* circumference of the circle described by the arguments, starting at
* the given start angle and ending at the given end angle, going in
* the given direction ( defaulting to clockwise ), is added to the
* path, connected to the previous point by a straight line.
*
* @name arcTo
* @function
* @param {number} x1 the starting x coordinate
* @param {number} y1 the starting y coordinate
* @param {number} x2 the ending x coordinate
* @param {number} y2 the ending y coordinate
* @param {number} radius the radius of the arc
*/
arcTo = function( x1, y1, x2, y2, radius ) {
var x0 = currentPos().x;
var y0 = currentPos().y;
// calculate lines between control points
var m1 = ( y1 - y0 ) / ( x1 - x0 );
if (m1 === Infinity){
m1 = ( y1 - y0 );
}
var yL = y0 - m1 * x0;
var yR = m1 * container.width - m1 * x1 + y1;
var m2 = ( y2 - y1 ) / ( x2 - x1 );
if (m2 === Infinity){
m2 = ( y2 - y1 );
}
yL = y1 - m2 * x1;
yR = m2 * container.width - m2 * x2 + y2;
// calculate the tangent points
// var m3 = ( m1 + m2 ) / 2; // m3 is never used
var theta = Math.atan( ( m1 - m2 ) / ( 1 + m1 * m2 ) );
var h = radius / Math.tan( theta / 2);
// var d = Math.sqrt( h * h + radius * radius ); // d is never used
var xinc = Math.abs( Math.sqrt( h * h / ( 1 + m1 * m1 ) ) );
var yinc = Math.abs( xinc * m1 );
var xi1, yi1, xi2, yi2;
var xc, yc;
if ( x1 > x0 ) {
xi1 = x1 - xinc;
} else {
xi1 = x1 + xinc;
}
if (y1 > y0) {
yi1 = y1 - yinc;
} else {
yi1 = y1 + yinc;
}
xinc = Math.abs( Math.sqrt( h * h / ( 1 + m2 * m2 ) ) );
yinc = Math.abs( xinc * m2 );
if (x2 > x1) {
xi2 = x1 + xinc;
} else {
xi2 = x1 - xinc;
}
if ( y2 > y1 ) {
yi2 = y1 + yinc;
} else {
yi2 = y1 - yinc;
}
var m1inv = -1 / m1;
xinc = Math.sqrt( radius * radius / ( 1 + m1inv * m1inv ) );
yinc = xinc * m1inv;
if (xi1 < x1 && x1 > xi2){
xc = xi1 - xinc;
} else {
xc = xi1 + xinc;
}
if (yi1 < y1 && y1 > yi2){
yc = yi1 - yinc;
} else {
yc = yi1 + yinc;
}
var start = Math.atan2(
yi1 - yc,
xi1 - xc
);
var end = Math.atan2(
yi2 - yc,
xi2 - xc
);
if ( ((start < 0) && (end > 0)) || ((start > 0) && (end < 0)) ) {
start += Math.PI;
end += Math.PI;
}
if (end < start) {
var temp = start;
start = end;
end = temp;
}
var extremes = math.getArcCircleExtremes( xc, yc, radius, start,
end );
var xArr = [x0, xi1, xi2].concat(extremes[0]);
var yArr = [y0, yi1, yi2].concat(extremes[1]);
context.moveTo(x0, y0);
context.arcTo( x1, y1, x2, y2, radius );
currentPos( xi2, yi2 );
_boundingBox({
x1: Math.max.apply(this, xArr),
y1: Math.max.apply(this, yArr),
x2: Math.min.apply(this, xArr),
y2: Math.min.apply(this, yArr)
});
return this;
},
/**
* Resets the current path.
*
* @name beginPath
* @function
*/
beginPath = function() {
context.beginPath();
return this;
},
/**
* Adds the given point to the current subpath, connected to the
* previous one by a cubic Bezier curve with the given control points.
*
* @name bezierCurveTo
* @function
* @param {number} cp1x the starting control point x coordinate
* @param {number} cp1y the starting control point x coordinate
* @param {number} cp2y the ending control point x coordinate
* @param {number} cp2y the ending control point x coordinate
* @param {number} x the ending x coordinate
* @param {number} y the ending y coordinate
*/
bezierCurveTo = function( cp1x, cp1y, cp2x, cp2y, x, y ) {
context.bezierCurveTo( cp1x, cp1y, cp2x, cp2y, x, y );
currentPos( x,y );
return this;
},
/**
* @private
* @name _boundingBox
* @function
*/
_boundingBox = function( params ){
var h,w,leftx,topy;
if ( params === undefined ){
return bbCurrent;
} else if ( params.x1 !== undefined &&
params.y1 !== undefined &&
params.x2 !== undefined &&
params.y2 !== undefined ){
h = Math.abs( params.y2 - params.y1);
w = Math.abs( params.x2 - params.x1);
leftx = ( params.x1 < params.x2) ? params.x1 : params.x2;
topy = ( params.y1 < params.y2) ? params.y1 : params.y2;
} else if ( params.x !== undefined &&
params.y !== undefined &&
params.w !== undefined &&
params.h !== undefined ){
h = params.h;
w = params.w;
leftx = params.x;
topy = params.y;
} else if ( params.cx !== undefined &&
params.cy !== undefined &&
params.r !== undefined ){
h = w = 2 * params.r;
leftx = params.cx - params.r;
topy = params.cy - params.r;
} else if ( params.x !== undefined &&
params.y !== undefined ){
var current = currentPos();
_boundingBox({x1:params.x, y1:params.y, x2: current.x,
y2: current.y});
} else {
return bbCurrent;
}
var tl = {x: leftx, y: topy},
t = {x: leftx + w / 2, y: topy},
tr = {x: leftx + w, y: topy},
r = {x: leftx + w, y: topy + h / 2},
br = {x: leftx + w, y: topy + h},
b = {x: leftx + w / 2, y: topy + h},
bl = {x: leftx, y: topy + h},
l = {x: leftx, y: topy + h / 2};
bbCurrent = {
tl: tl,
t: t,
tr: tr,
r: r,
br: br,
b: b,
bl: bl,
l: l
};
return bbCurrent;
},
/**
* Returns the current bounding box of the last drawn shape-
* specifically an object containing the top left, top, top right,
* right, bottom right, bottom, bottom left and left coordinates.
*
* @name boundingBox
* @function
*/
boundingBox = function(){
return _boundingBox();
},
/**
* Draws a circle with the supplied starting x,y, radius, fillStyle,
* and strokeStyle
*
* @name circle
* @function
* @param params {object} a parameter object
* @param params.x {number} the starting x coordinate
* @param params.y {number} the starting y coordinate
* @param params.radius {radius} the radius of the circle
* @param params.fillStyle {Any} the fill style for the circle or
* false to suppress the fill
* @param params.strokeStyle {Any} the stroke style for the circle or
* false to suppress the stroke
*/
circle = function( params ) {
//@todo expand params to set any style appliable to a rectangle
params = params || {};
var x = _valOrDefault( params.x, xCurrentPos ),
y = _valOrDefault( params.y, yCurrentPos ),
radius = params.radius || 10,
fillStyle = params.fillStyle || false,
strokeStyle = params.strokeStyle || false;
moveTo( x, y );
beginPath();
arc( x, y, radius, 0, 2 * Math.PI);
if ( fillStyle ) {
context.fillStyle = fillStyle;
context.fill();
}
if ( strokeStyle ) {
context.strokeStyle = strokeStyle;
stroke();
}
closePath();
_boundingBox({cx:x,cy:y,r:radius});
return this;
},
/**
* Clears a rectangular area, making it fully transparent
* @name clearRect
* @function
* @param params {object} a parameter object
* @param params.x {number} Starting x coordinate. Defaults to the
* current position.
* @param params.y {number} Starting y coordinate. Defaults to the
* current position.
* @param params.width {number} Rectangle width. Defaults to 0.
* @param params.height {number} Rectangle height. Defaults to 0.
*/
clearRect = function( params ) {
params = params || {};
var x = _valOrDefault( params.x, xCurrentPos ),
y = _valOrDefault( params.y, yCurrentPos ),
width = params.width || 0,
height = params.height || 0;
context.clearRect( x, y, width, height );
_boundingBox({x:x, y:y, w:width, h:height});
return this;
},
/**
* Further constrains the clipping region to the current default path.
* @name clip
* @function
*/
clip = function() {
context.clip();
return this;
},
/**
* Marks the current subpath as closed, and starts a new subpath with a
* point the same as the start and end of the newly closed subpath.
*
* @name closePath
* @function
*/
closePath = function() {
context.closePath();
return this;
},
/**
* Passed a height and width, returns an ImageData object with the
* given dimensions in CSS pixels ( which might map to a different
* number of actual device pixels exposed by the object itself ). All
* the pixels in the returned object are transparent black.
* passed an imageData object, returns an ImageData object with the
* same dimensions as the argument. All the pixels in the returned
* object are transparent black.
*
* @name createImageData
* @function
* @param {number} height the height of the image data object
* @param {number} width the width of the image data object
* @param {object} imageData an imageData object
*/
createImageData = function() {
if ( arguments[0].data !== undefined ){
//height is actually imageData
return context.createImageData( arguments[0] );
} else {
return context.createImageData( arguments[0], arguments[1] );
}
},
/**
* Creates a linear gradient. Takes four arguments that represent the
* start point ( x0, y0) and end point ( x1, y1) of the gradient. The
* method must return a linear CanvasGradient initialized with the
* specified line.
*
* @name createLinearGradient
* @function
* @param {number} x0 the starting x coordinate
* @param {number} y0 the starting y coordinate
* @param {number} x1 the ending x coordinate
* @param {number} y1 the ending y coordinate
*/
createLinearGradient = function( x0, y0, x1, y1 ) {
return context.createLinearGradient( x0, y0, x1, y1 );
},
/**
* Returns a CanvasPattern object that repeats the specified img in the
* specified repetition direction.
* @name createPattern
* @function
* @param {HTMLElement} img the image to repeate
* @param {string} repetition the direction to repeat. One of four
* values repeat, repeat-x, repeat-y, no-repeat
*/
createPattern = function( img, repetition ) {
return context.createPattern ( img, repetition );
},
/**
* Returns an object that represents a radial or circular gradient to
* use in a canvas context.
*
* @name createRadialGradient
* @function
* @param {number} x0 The x-coordinate of the starting circle of the
* gradient.
* @param {number} y0 The y-coordinate of the starting circle of the
* gradient.
* @param {number} r0 The radius of the starting circle.
* @param {number} x1 The x-coordinate of the ending circle of the
* gradient.
* @param {number} y1 The y-coordinate of the ending circle of the
* gradient.
* @param {number} r1 The radius of the ending circle.
*/
createRadialGradient = function( x0, y0, r0, x1, y1, r1 ){
return context.createRadialGradient( x0, y0, r0, x1, y1, r1 );
},
/**
* Draws a specified image onto a canvas
*
* @name drawImage
* @function
* @param {number} x Starting x coordinate.
* @param {number} y Starting y coordinate.
* @param {HTMLElement} img the image to draw onto the Canvas
*
*/
drawImage = function( img, x, y ) {
if ( img.nodeName == null ) {
var newImg = new Image();
newImg.src = img;
img = newImg;
}
img.onload = function() {
context.drawImage( img, x, y );
};
x = _valOrDefault( x, xCurrentPos );
y = _valOrDefault( y, yCurrentPos );
currentPos( x,y );
return this;
},
/**
* Fills subpaths by using the current fill style.
* @name fill
* @function
*/
fill = function() {
context.fill();
return this;
},
/**
* Draws a circle with the supplied starting x,y, radius and fillStyle
* and no stroke
* @name fillCircle
* @function
* @param params {object} a parameters object
* @param params.x {number} the starting x coordinate
* @param params.y {number} the starting y coordinate
* @param params.radius {radius} the radius of the circle
* @param params.fillStyle {Any} the fill style for the circle. a
* falsey value will use the current
* context fillStyle
*/
fillCircle = function( params ){
params = params || {};
var x = _valOrDefault( params.x, xCurrentPos ),
y = _valOrDefault( params.y, yCurrentPos ),
radius = params.radius || 10,
fillStyle = params.fillStyle || context.fillStyle;
circle({
x: x,
y: y,
radius: radius,
fillStyle: fillStyle,
strokeStyle: false
});
return this;
},
/**
* Paints a rectangle onto the canvas using the current fill style.
* @name fillRect
* @function
* @param {number} x Starting x coordinate
* @param {number} y Starting y coordinate
* @param {number} width Rectangle width
* @param {number} height Rectangle height
*/
fillRect = function( x, y, width, height ) {
context.fillRect( x, y, width, height );
currentPos( x,y );
_boundingBox({x:x, y:y, w:width, h:height});
return this;
},
/**
* Called with a color argument, sets the fillStyle. Called without,
* returns the current fillStyle.
* @name fillStyle
* @function
* @param {Any} color the fill style
*/
fillStyle = function( color ) {
if ( color !== undefined ) {
context.fillStyle = color;
return this;
} else {
return context.fillStyle;
}
},
/**
* Writes text onto the canvas using the current text style.
* @name fillText
* @function
* @param {string} text the text to write into the canvas
* @param {number} x Starting x coordinate
* @param {number} y Starting y coordinate
* @param {number} maxWidth the maximum width of the text box
*/
fillText = function( text, x, y, maxWidth ) {
if ( maxWidth === undefined ){
context.fillText( text, x, y );
} else {
context.fillText( text, x, y, maxWidth );
}
currentPos( x,y );
return this;
},
/**
* Called with a declaration argument, sets the context font. Called
* without, returns the current context font.
* @name font
* @function
* @param {string} declaration the font style
*/
font = function( declaration ) {
if ( declaration !== undefined ) {
context.font = declaration;
return this;
} else {
return context.font;
}
},
/**
* Returns an ImageData object representing the pixel data for the
* area of the canvas defined by the height and width provided starting
* at the provided x, y coordinates
*
* @name getImageData
* @function
* @param {number} x Starting x coordinate
* @param {number} y Starting y coordinate
* @param {number} width imageData width
* @param {number} height imageData height
*/
getImageData = function( x, y, width, height ){
currentPos( x,y );
return context.getImageData( x, y, width, height );
},
/**
* Returns an object with the rgba value of a given pixel
* @name getPixelColor
* @function
* @param {number} x The x coordinate of the pixel to test
* @param {number} y The y coordinate of the pixel to test
*/
getPixelColor = function(x, y) {
// thanks https://gist.github.com/codepo8/5631638
var pixels = context.getImageData(
0, 0, container.width, container.height
),
index = ((y * (pixels.width * 4)) + (x * 4));
return {
r:pixels.data[index],
g:pixels.data[index + 1],
b:pixels.data[index + 2],
a:pixels.data[index + 3]
};
},
/**
* Returns the RGB color of a specific pixel
* @name getPixelsByColor
* @function
* @param {number} r The red value
* @param {number} g The green value
* @param {number} b The blue value
*/
getPixelsByColor = function(r, g, b) {
//thanks https://gist.github.com/codepo8/5631638
var pixels = context.getImageData(0, 0, container.width,
container.height),
all = pixels.data.length,
amount = 0;
for (var i = 0; i < all; i += 4) {
if (pixels.data[i] === r &&
pixels.data[i+1] === g &&
pixels.data[i+2] === b) {
amount++;
}
}
return amount;
},
/**
* Called with a num argument, sets the context alpha/transparency.
* Called without, returns the current context globalAlpha.
* @name globalAlpha
* @function
* @param {Floating-point} num the new globalAlpha value in a range
* between 0 and 1.
*/
globalAlpha = function( num ) {
if ( num !== undefined ) {
context.globalAlpha = num;
return this;
} else {
return context.globalAlpha;
}
},
/**
* Gets or sets a value that indicates how source images are drawn onto
* a destination image.
*
* @name globalCompositeOperation
* @function
* @param {string} op one of the following options (from
* http://www.w3.org/TR/2dcontext/#compositing)
* <br>
* <strong>source-atop</strong><br>
* A atop B. Display the source image wherever both
* images are opaque. Display the destination image
* wherever the destination image is opaque but the
* source image is transparent. Display
* transparency elsewhere.<br>
* <strong>source-in</strong><br>
* A in B. Display the source image wherever both
* the source image and destination image are
* opaque. Display transparency elsewhere.<br>
* <strong>source-out</strong><br>
* A out B. Display the source image wherever the
* source image is opaque and the destination image
* is transparent. Display transparency
* elsewhere.<br>
* <strong>source-over (default)</strong><br>
* A over B. Display the source image wherever the
* source image is opaque. Display the destination
* image elsewhere.<br>
* <strong>destination-atop</strong><br>
* B atop A. Same as source-atop but using the
* destination image instead of the source image
* and vice versa.<br>
* <strong>destination-in</strong><br>
* B in A. Same as source-in but using the
* destination image instead of the source image
* and vice versa.<br>
* <strong>destination-out</strong><br>
* B out A. Same as source-out but using the
* destination image instead of the source image
* and vice versa.<br>
* <strong>destination-over</strong><br>
* B over A. Same as source-over but using the
* destination image instead of the source image
* and vice versa.<br>
* <strong>lighter</strong><br>
* A plus B. Display the sum of the source image
* and destination image, with color values
* approaching 255 (100%) as a limit.<br>
* <strong>copy</strong><br>
* A (B is ignored). Display the source image
* instead of the destination image.<strong><br>
* xor</strong><br>
* A xor B. Exclusive OR of the source image and
* destination image.<br>
* <strong>vendorName-operationName</strong><br>
* Vendor-specific extensions to the list of
* composition operators should use this syntax.
*/
globalCompositeOperation = function( op ) {
if ( op !== undefined ) {
context.globalCompositeOperation = op;
return this;
} else {
return context.globalCompositeOperation;
}
},
/**
* Determines if the specified point is in the current path.
*
* @name isPointInPath
* @function
* @param {number} x The x coordinate to test
* @param {number} y The y coordinate to test
*/
isPointInPath = function( x, y ){
//@todo does this make sense to update the x, y?
return context.isPointInPath( x, y );
},
/**
* Draws a line from a point (x,y) at a pixel distance at a provided
* angle
*
* @name line
* @function
* @param params {object} a parameter object
* @param {number} params.x the starting x coordinate
* @param {number} params.y the starting y coordinate
* @param {number} params.distance the length of the line
* @param {number} params.angle The angle of the line
*/
line = function( params ) {
params = params || {};
var x = _valOrDefault( params.x, xCurrentPos ),
y = _valOrDefault( params.y, yCurrentPos ),
hypotenuse = params.distance || 0,
angle = params.angle % 360 || 0,
radians = math.radians( angle ),
a = Math.sin( radians ) * hypotenuse,
b = Math.cos( radians ) * hypotenuse,
newX = x + b,
newY = y + a;
context.moveTo( x, y );
context.lineTo( newX, newY );
currentPos( newX,newY );
_boundingBox({x1:x, y1:y, x2:newX, y2: newY});
return this;
},
/**
* Gets or sets the lineCap style for lines
*
* @name lineCap
* @function
* @param {string} cap defines the line cap. One of the following three
* options: <br>
* <b>butt</b><br>
* Default. A flat edge is put perpendicular to
* each end of the line with no cap added.<br>
* <b>round</b><br>
* A semicircle or rounded end cap is added to each
* end of the line.<br>
* <b>square</b><br>
* A square end cap is added to each end of the
* line.<br>
*/
lineCap = function( cap ) {
if ( cap !== undefined ) {
context.lineCap = cap;
return this;
} else {
return context.lineCap;
}
},
/**
* Gets or sets the type of connection created when two lines meet
*
* @name lineJoin
* @function
* @param {string} join defines the style of line join. One of the
* following three options:<br>
* <strong>bevel</strong><br>
* A filled triangle connects the two lines that
* are joined, creating a beveled corner.<br>
* <strong>round</strong><br>
* A filled arc connects the two lines, creating
* a rounded corner.<br>
* <strong>miter</strong><br>
* Default. The outside edges of the lines are
* continued until they intersect and the
* resulting triangle is filled, creating a sharp
* or pointed corner.
*/
lineJoin = function( join ) {
if ( join !== undefined ) {
context.lineJoin = join;
return this;
} else {
return context.lineJoin;
}
},
/**
* Draws a line from the current point in the canvas to a new x/y pair
* provided as an argument
*
* @name lineTo
* @function
* @param x {number} the ending x coordinate
* @param y {number} the ending y coordinate
*/
lineTo = function( x, y ) {