-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamples.htm
849 lines (792 loc) · 29.6 KB
/
samples.htm
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
<!DOCTYPE html>
<html manifest="data.appcache">
<head>
<meta charset="utf-8">
<title>Breakout</title>
<meta name="description" content="Time Game">
<meta name="author" content="Andrew Ippoliti">
<!-- IN ANDROID BROWSER - enter "about:debug" in the address bar
It should give you some extra options and a 'js console' -->
<!-- Mobile viewport optimization http://goo.gl/b9SaQ -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=10.0;">-->
<!--<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no" />-->
<meta name="viewport" content="width=320, height=320, user-scalable=no" />
<!-- Hide browser chrome (iOS only) -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<!-- Supposedly style the status bar -->
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style type="text/css">
html, body, canvas {
margin: 0px;
/* disable selections */
-webkit-user-select: none;
/* disable callout, image save panel on long press */
-webkit-touch-callout: none;
/* make highlighting transparent */
-webkit-tap-highlight-color: transparent;
}
.preview-module {
padding: 16px;
margin: 32px;
text-align: center;
vertical-align: middle;
border: 1px solid #000;
border-radius: 4px;
}
.preview-module .text {
display: inline-block;
width: 60%;
text-align: center;
vertical-align: middle;
}
.preview-module canvas {
text-align: center;
vertical-align: middle;
border: 1px dashed #000;
margin: 3%;
}
.preview-module .text pre {
float: left;
text-align: left;
vertical-align: middle;
clear: left;
padding-top: 1em;
}
.preview-module .text textarea {
float: left;
text-align: left;
vertical-align: middle;
clear: left;
margin-top: 1em;
width: 90%;
min-height: 3em;
}
.preview-module .text a {
float: left;
text-align: left;
width:100%;
display:block;
font-weight: bold;
font-size: 2em;
}
.preview-module .text .desc {
float: left;
text-align: left;
vertical-align: middle;
display: block;
}
</style>
</head>
<body>
<!--
<div id="div-main" style="position:absolute; width: 128px; height: 128px;">
<canvas id="canvas-main" width=128 height=128 style="border: 1px dashed #000"></canvas>
</div>
-->
<script type="text/javascript" src="./gfx.js"></script>
<script type="text/javascript" id="script-doc">
requestAnimFrame = (function(){
/// Compatibility for animation frame on older browsers
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 1000 / 60);
};
})();
// ------------------------------------------------------------- [ Simple Test ] -
var TemplateStatic = (function($gfx){
var mod = {};
mod.start = function(){
// Code here
}
return mod;
}($gfxlib));
var TemplateAnim = (function($gfx){
var mod = {};
mod.frameLastTime = 0;
mod.frameThisTime = 0;
mod.frameMaxDt = 0.5;
mod.update = function(){
// Compute the difference in time between this frame and the last.
mod.frameLastTime = mod.frameThisTime;
mod.frameThisTime = (new Date()).getTime();
// The times are in milliseconds: we /1000 to convert to seconds.
var dt = (mod.frameThisTime - mod.frameLastTime)*0.001;
if(dt > mod.frameMaxDt){ dt = mod.frameMaxDt; }
// Update Code Here
// setup another update
requestAnimFrame(mod.update);
}
mod.start = function(){
mod.frameLastTime = (new Date()).getTime();
mod.frameThisTime = mod.frameLastTime;
// Startup Code Here
requestAnimFrame(mod.update);
}
return mod;
}($gfxlib));
var ViewSetByCorners1 = (function($gfx){
var mod = {};
mod.start = function(){
$gfx.setCanvasNode(document.getElementById("canvas-main"));
$gfx.viewSetByCorners(0,0,30,30);
$gfx.drawRect(10,10,10,10);
}
return mod;
}($gfxlib));
var NewCanvas = (function($gfx){
var mod = {};
mod.start = function(){
var parentNode = document.getElementById("div-main");
var newNode = $gfxlib.createCanvasNode(parentNode,"canvas-id",0);
$gfx.setCanvasNode(newNode.domNode);
$gfx.drawRect(10,10,10,10);
}
return mod;
}($gfxlib));
DrawRect = {
name: "Rectangle",
code: "$gfxlib.drawRect(32,32,48,48);",
desc: "How to draw a retangle. The (left,top) corner is (32,32). \n"+
"The (width,height) is (32,32)."
};
SetStyle = {
name: "Color Styles",
code: "$gfxlib.setStyle('red','blue',1.0);\n"+
"$gfxlib.drawRect(64,64,64,64);",
desc: "Draws a rectangle that is filled red, stroked blue \n"+
"and has an alpha level of 1.0 (fully visible)."
};
SetLine = {
name: "Line Styles",
code: "$gfxlib.setStyle('red','blue',1.0);\n"+
"$gfxlib.setLineStyle(4,'round','round');\n"+
"$gfxlib.drawRect(64,64,64,64);",
desc: "The line is set to be 4 pixels wide. This line is capped with "+
"a 'round' cap but you may also use 'butt' or 'square'. "+
"Line segments are joined via a 'round' joint but you could "+
"use 'miter' or 'bevel'."
};
DrawRectCenter = {
name: "Centered Rectangle",
code: "$gfxlib.setStyle('red','red',1.0);\n"+
"$gfxlib.drawRect(64,64,64,64);\n"+
"$gfxlib.setStyle('blue','blue',1.0);\n"+
"$gfxlib.drawRectCenter(64,64,32,32);",
desc: "The blue rectangle is drawn with its center (x,y) "+
"at (64,64). Half of its width is 32; half of its "+
"height is 32."
};
BasicCircle = {
name: "Basic Circle",
code: "$gfxlib.setStyle('yellow','black',1.0);\n"+
"$gfxlib.drawCircle(64,32,16);",
desc: "The circle's center (x,y) is (64,32). It has a "+
"radius of 16."
};
GradientLinear = {
name: "Linear Gradient",
code: "var grad = $gfxlib.createGradientLinear(0,0,128,64,\n"+
" ['#FF0','red','#0000F9']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);",
desc: "Linear gradients are defined with a starting x,y location "+
"(0,0), an ending x,y location (128,64), and an array of "+
"colors ['#FF0','red','#0000F9']. After the gradient (grad) "+
"has been defined it can be used a fill or stroke 'color'."
};
GradientRadial1 = {
name: "Radial Gradient 1",
code: "var grad = $gfxlib.createGradientRadial(64,64,16,64,64,32,\n"+
" ['#FF0','red','#0000F9']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);",
desc: "Circular gradients are defined by 2 circles. The simplest "+
"case is when the circles have the same center (64,64). The "+
"radius (16) of the first circle should be smaller than the "+
"radius (32) of the outer circle. The list of colors goes "+
"from the inner circle to the outer circle."
};
GradientRadial2 = {
name: "Radial Gradient 2",
code: "var grad = $gfxlib.createGradientRadial(32,32,0,64,64,64,\n"+
" ['#0F0','#00F']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);",
desc: "By moving the centers of the circles it is possible to "+
"achieve various effects. Note that the first circle smaller "+
"and is located to the upper left. The second circle is "+
"larger and defines an 'outer boundary' of sorts for the "+
"gradient. Make sure the outer circle is big enough; "+
"otherwise, you may not see a gradient."
};
Checkerboard = {
name: "Checkerboard",
code: "$gfxlib.setStyle('black','yellow',1.0);\n"+
"$gfxlib.drawCheckerBoard(0,0,16,16,8,8);",
desc: "You can make a nice checkerboard pattern. This example "+
"starts at (0,0). Each 'block' is 16 units wide and 16 "+
"units high. There are 8 'blocks' in a row and 8 "+
"'rows' of blocks."
};
ClearAll = {
name: "Clear All",
code: "$gfxlib.setStyle('black','yellow',1.0);\n"+
"$gfxlib.drawCheckerBoard(0,0,16,16,8,8);\n"+
"$gfxlib.drawClearAll();",
desc: "To get a blank canvas just use the clear all function. "+
"It will make the canvas transparent (until you draw "+
"over it)."
};
DrawStar = {
name: "Draw Star",
code: "$gfxlib.setStyle('yellow','#000',1.0);\n"+
"$gfxlib.drawStar(64,64,8,24,3,0);",
desc: "Stars have spokes. This star is centered at (x,y) (64,64). "+
"It has an inner radius of 8 and an outer radius or spoke "+
"length of 24. There are 3 spokes. The first spoke points "+
"toward 0 degrees"
};
DrawPoly = {
name: "Regular Polygon",
code: "$gfxlib.setStyle('#0F0','#000',1.0);\n"+
"$gfxlib.drawPoly(64,64,24,5,15);",
desc: "Regular polygons have sides or equal length and all of "+
"their vertecies are the same distance away from their "+
"center. This polygon is centered at (64,64). Its verticies "+
"are 24 units away from its center. It has 5 sides and its "+
"first vertex points toward 15 degrees."
};
InvertAll = {
name: "Invert all colors",
code: "var grad = $gfxlib.createGradientRadial(64,64,0,64,64,64,\n"+
" ['red','yellow','green','blue']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);\n"+
"$gfxlib.filterApplyToAll($gfxlib.filterFuncInvert,{});",
desc: "Converting a photographic negative to a normal image is "+
"like inverting all of the colors of am image. Technically "+
"the pixels are being replaced by 255 minus the orginal "+
"pixel's color channel values. Red is 255 red, 0 green and "+
"0 blue; its negative is 0 red, 255 green and 255 blue."
};
Filters = {
name: "Filters",
code: "var grad = $gfxlib.createGradientRadial(64,64,0,64,64,64,\n"+
" ['red','yellow','green','blue']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);\n"+
"$gfxlib.filterApplyToRegion(0,0,64,64,$gfxlib.filterFuncNoBlue,{});\n"+
"$gfxlib.filterApplyToRegion(64,0,64,64,$gfxlib.filterFuncNoRed,{});\n"+
"$gfxlib.filterApplyToRegion(0,64,64,64,$gfxlib.filterFuncNoGreen,{});\n",
desc: "You can use special filters to apply other effects."
};
InvertRegion = {
name: "Invert a region",
code: "var grad = $gfxlib.createGradientRadial(64,64,0,64,64,64,\n"+
" ['red','yellow','green','blue']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);\n"+
"$gfxlib.filterApplyToRegion(0,0,64,128,$gfxlib.filterFuncInvert,{});\n"+
"$gfxlib.filterApplyToRegion(0,64,128,64,$gfxlib.filterFuncToLuma,{});",
desc: "You can just invert a specific region. This code inverts "+
"a region whose (top,left) corner is (0,0) and is 64 pixels "+
"wide by 128 pixels high."
};
FilterColorSwap = {
name: "Swap Colors",
code: "var grad = $gfxlib.createGradientRadial(64,64,0,64,64,64,\n"+
" ['red','yellow','green','blue']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);\n"+
"$gfxlib.filterApplyToRegion(0,0,64,128,$gfxlib.filterFuncColorSwap,\n"+
"\t{fR:255,fG:0,fB:0,tR:0,tG:0,tB:255,minDist:45,maxDist:255});\n",
desc: "You can just invert a specific region. This code inverts "+
"a region whose (top,left) corner is (0,0) and is 64 pixels "+
"wide by 128 pixels high."
};
FilterHueShift = {
name: "Hue Shifting",
code: "var grad = $gfxlib.createGradientRadial(64,64,0,64,64,64,\n"+
" ['red','yellow','green','blue']);\n"+
"$gfxlib.setStyle(grad,'black',1.0);\n"+
"$gfxlib.drawRect(14,14,100,100);\n"+
"$gfxlib.filterApplyToRegion(0,0,64,128,$gfxlib.filterFuncShiftHue,\n"+
"\t{degrees:120});\n",
desc: "You can shift the hue of your image by a specific number "+
"of degrees."
};
Convolution = {
name: "3x3 Convolution",
code: "$gfxlib.setStyle('yellow','#000',1.0);\n"+
"$gfxlib.drawStar(32,64,8,24,5,0);\n"+
"$gfxlib.drawStar(96,64,8,24,5,0);\n"+
"$gfxlib.filterConvolve3x3(64,0,64,128,\n"+
"[1,1,1, 1,-7,1, 1,1,1]);\n",
desc: "You can use a general convultion matrix to apply some cool "+
"effects to an image like bluring, sharpening, embossing or "+
"even edge detection as shown in the example. The image on "+
"the right side 64 pixels wide, 128 pixels high, starting at "+
"(64,0) has been convolved with the shown matrix."
// http://docs.gimp.org/nl/plug-in-convmatrix.html
// blur [0.1,0.1,0.1, 0.1,0.1,0.1, 0.1,0.1,0.1]
// more blur [1,2,1, 2,4,2, 1,2,1]
// sharpen [0,-2,0, -2,11,-2, 0.0,-2,0.0]
// mean removal [-1,-1,-1, -1,9,-1, -1,-1,-1]
// emboss [-2,-1,0, -1,1,1, 0,1,2]
// lighten [0,0,0, 0,1.5,0, 0,0,0]
// darken [0,0,0, 0,1.5,0, 0,0,0]
// edge find [0,1,0, 1,-4,1, 0,1,0]
// edge detect [1,1,1, 1,-7,1, 1,1,1]
// edge detect 2 [-5,0,0, 0,0,0, 0,0,5]
};
FastConvolution = {
name: "Fast Convolution",
code: "$gfxlib.setStyle('yellow','#000',1.0);\n"+
"$gfxlib.drawStar(32,64,8,24,5,0);\n"+
"$gfxlib.drawStar(96,32,8,24,5,0);\n"+
"$gfxlib.drawStar(96,96,8,24,5,0);\n"+
"for( var i=0; i<20; i+=1 ){\n"+
" $gfxlib.filterFastConvolve3(64,0,64,64,[0.2,0.6,0.2]);\n"+
"}\n"+
"for( var i=0; i<20; i+=1 ){\n"+
" $gfxlib.filterConvolve3x3(64,64,64,64,[0.04,0.12,0.04, 0.12,0.36,0.12, 0.04,0.12,0.04]);\n"+
"}",
desc: "Some convolution kernels are symmetric (ie a Guassian "+
"kernel). They can be written as a row times it's transpose. "+
"When your convolution is of this form you can cheat a little "+
"and ignore the non-middle elements for a slight performance "+
"boost. The following compares a 'cheat' blur (top) to a "+
"'real' blur (bottom)."
};
DrawText = {
name: "Drawing Text",
code: "$gfxlib.setStyle('#F00','#0F0',1.0);\n"+
"$gfxlib.textSetFont('20px monospace');\n"+
"$gfxlib.textSetAlign('center','middle');\n"+
"$gfxlib.textDraw(64,64,'Hello World!');",
desc: "You need the set the font size (20px) and name "+
"(monospace) as shown above. textSetAlign controls "+
"how the text is positioned. textDraw writes 'Hello "+
"World!' at (64,64)."
};
MeasureText = {
name: "Measuring Text",
code: "$gfxlib.textSetFont('12px monospace');\n"+
"$gfxlib.textSetAlign('left','top');\n"+
"var w = $gfxlib.textGetWidth('Hello World!');\n"+
"$gfxlib.setStyle('yellow','red',1.0);\n"+
"$gfxlib.drawRect(32,32,w,12);\n"+
"$gfxlib.setStyle('black','none',1.0);\n"+
"$gfxlib.textDraw(32,32,'Hello World!');",
desc: "You can measure the width of your text after you style "+
"it and before you draw it."
};
PathSimple = {
name: "Simple Paths",
code: "$gfxlib.setStyle('yellow','red',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathMoveTo(64,32).pathLine();\n"+
"$gfxlib.pathMoveTo(64,64).pathLine();\n"+
"$gfxlib.pathMoveTo(32,64).pathLine();\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can use a path to connect several x,y "+
"coordinates with lines."
};
PathMove = {
name: "Path Move",
code: "$gfxlib.setStyle('yellow','red',1.0);\n"+
"$gfxlib.pathBegin(48,48);\n"+
"$gfxlib.pathMoveTo(80,48);\n"+
"$gfxlib.pathMoveTo(80,80);\n"+
"$gfxlib.pathMoveTo(48,48).pathLine();\n"+
"$gfxlib.pathMoveTo(48,64).pathDoMove();\n"+
"$gfxlib.pathMoveTo(64,80);\n"+
"$gfxlib.pathMoveTo(48,80);\n"+
"$gfxlib.pathMoveTo(48,64).pathLine();\n"+
"$gfxlib.pathDraw();",
desc: "You can use the move commands to go to a "+
"different point without a line."
};
PathRelative = {
name: "Relative Paths",
code: "$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathMoveBy(32,0).pathLine();\n"+
"$gfxlib.pathMoveBy(0,32).pathLine();\n"+
"$gfxlib.pathMoveBy(-32,0).pathLine();\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can specify coordinates in a relative manner. "+
"Instead of giving absolute coordinates, give the "+
"amount moved in each direction. In this example "+
"we start the square at 32,32. The point then draws "+
"a line 32 units to the right of it's last position to "+
"form the top. Then it goes 32 pixels down to form the "+
"right edge. Then it -32 pixels right (or 32 pixels "+
"left) to form the bottom before closing."
};
PathDirection = {
name: "Directional Paths",
code: "$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathTurnTo(0,32).pathLine();\n"+
"$gfxlib.pathTurnTo(-90,32).pathLine();\n"+
"$gfxlib.pathTurnTo(-180,32).pathLine();\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "Instead of specifying coordinates you can "+
"specify directions and lengths. The shape begins "+
"at 0,32. Then is goes 32 units in the direction of "+
"0 degrees. Then 32 units at -90 degrees. Finally, "+
"32 units at -180 degrees. 0 degrees is positve x ("+
"right), 90 degrees is positive y (down), 180 degrees "+
"is negative x (left), 270 is negative y (up). Going from "+
"0 to 360 is turning clockwise. If you are more "+
"comfortable turning counterclockwise (ie cartiesian "+
"coordinates), negate your angles."
};
PathDirectionRelative = {
name: "Relative Directional Paths",
code: "$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathTurnTo(0,32).pathLine();\n"+
"$gfxlib.pathTurnBy(90,32).pathLine();\n"+
"$gfxlib.pathTurnBy(90,32).pathLine();\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can also specify relative directions."+
"This is like turning. The line goes for 32 "+
"units in the direction of 0 degrees then it "+
"turns 90 degrees to its right and goes 32 "+
"units. Then it turns 90 degrees and goes "+
"another 32 units."
};
PathCurveTo = {
name: "Curves",
code: "$gfxlib.setStyle('yellow','red',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathMoveTo(64,32).pathLine();\n"+
"$gfxlib.pathMoveTo(32,64).pathCurve(-30);\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can draw a curve to a specific point. "+
"In this case the curve goes to (32,64). The "+
"curve is 'curved' -30 pixels to the right of "+
"the line (or 30 pixels to the left). The line "+
"is the line from the curve's starting point "+
"(64,32) to it's ending point (32,64)."
};
PathCurveTurn = {
name: "Curve Turns",
code: "$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathTurnTo(0,32).pathLine();\n"+
"$gfxlib.pathTurnBy(90,32).pathLine();\n"+
"$gfxlib.pathTurnBy(90,32).pathCurve(32);\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can also use relative directions with curves."
};
PathBezierTo = {
name: "Bezier Curves",
code: "$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathMoveTo(64,32);\n"+
"$gfxlib.pathMoveTo(64,64);\n"+
"$gfxlib.pathMoveTo(32,64).pathBezier();\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();",
desc: "You can draw bezier curves. The curve approaches "+
"control point one (64,32) then is pulled toward the "+
"second control point (64,64) until it finally "+
"reaches (32,64)."
};
PathHasPoint = {
name: "Point in Path",
code: "$gfxlib.setStyle('#0FF','black',1.0);\n"+
"$gfxlib.pathBegin(32,32);\n"+
"$gfxlib.pathMoveTo(64,32);\n"+
"$gfxlib.pathMoveTo(64,64);\n"+
"$gfxlib.pathMoveTo(32,64).pathBezier();\n"+
"var x = 64, y = 32;\n"+
"if( $gfxlib.pathHasPoint(x,y) ){\n"+
"\t$gfxlib.setStyle('red','black',1.0);\n"+
"}\n"+
"$gfxlib.pathClose();\n"+
"$gfxlib.pathDraw();\n"+
"$gfxlib.drawRectCenter(x,y,1,1);\n",
desc: "For simple collision detection you can see if a "+
"point is contained within a path. Go ahead and "+
"change the x and y to 48, 48. Then change it to "+
"52, 38."
};
ScratchSimple = {
name: "Scratch Area",
code: "$gfxlib.scratchNewCanvas(32,32);\n"+
"$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,5,0);\n"+
"var scratch = $gfxlib.scratchEndCanvas();\n"+
"$gfxlib.drawScratch(scratch,0,0);\n"+
"$gfxlib.drawScratch(scratch,64,64);\n"+
"$gfxlib.drawScratch(scratch,96,96);\n",
desc: "You can use a scratch canvas to cache drawings "+
"and improve speed."
};
ScratchScaling = {
name: "Scratch Scaling",
code: "$gfxlib.scratchNewCanvas(32,32);\n"+
"$gfxlib.setStyle('#FF0','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,5,0);\n"+
"var scratch = $gfxlib.scratchEndCanvas();\n"+
"$gfxlib.drawScratchScaled(scratch,0,0,2,2);\n"+
"$gfxlib.drawScratchScaled(scratch,64,64,1,1);\n"+
"$gfxlib.drawScratchScaled(scratch,96,96,0.5,0.5);\n",
desc: "You can draw the scratch canvas scaled to various sizes."
};
ScratchRegion = {
name: "Scratch Parts",
code: "$gfxlib.scratchNewCanvas(64,64);\n"+
"$gfxlib.setStyle('#F00','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,3,0);\n"+
"$gfxlib.drawStar(48,16,8,16,4,0);\n"+
"$gfxlib.drawStar(16,48,8,16,5,0);\n"+
"$gfxlib.drawStar(48,48,8,16,6,0);\n"+
"var scratch = $gfxlib.scratchEndCanvas();\n"+
"$gfxlib.drawScratchPart(scratch,0,0,32,32,0,48);\n"+
"$gfxlib.drawScratchPart(scratch,32,0,64,32,32,48);\n"+
"$gfxlib.drawScratchPart(scratch,0,32,32,64,64,48);\n"+
"$gfxlib.drawScratchPart(scratch,32,32,64,64,96,48);\n",
desc: "You can specify what region of the scratch area you "+
"would like to draw."
};
ScratchRegionScaling = {
name: "Scratch Scaled Region",
code: "$gfxlib.scratchNewCanvas(64,64);\n"+
"$gfxlib.setStyle('#F0F','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,3,0);\n"+
"$gfxlib.drawStar(48,16,8,16,4,0);\n"+
"$gfxlib.drawStar(16,48,8,16,5,0);\n"+
"$gfxlib.drawStar(48,48,8,16,6,0);\n"+
"var scratch = $gfxlib.scratchEndCanvas();\n"+
"$gfxlib.drawScratchPartScaled(scratch,0,0,32,32,0,32,2,2);\n"+
"$gfxlib.drawScratchPartScaled(scratch,32,0,64,32,64,48,1,1);\n"+
"$gfxlib.drawScratchPartScaled(scratch,0,32,32,64,96,56,0.5,0.5);\n",
desc: "You can even scale each region as it is drawn."
};
ScratchPattern = {
name: "Scratch Patterns",
code: "$gfxlib.scratchNewCanvas(32,32);\n"+
"$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,5,0);\n"+
"var pattern = $gfxlib.scratchToPattern('repeat');\n"+
"$gfxlib.setStyle(pattern,'black',1.0);\n"+
"$gfxlib.drawCircle(64,64,48);\n",
desc: "You can use a scratch canvas to create patterns that "+
"can be used as a fill style in other commands."
};
ScratchFromRegion = {
name: "Scratch From Region",
code: "$gfxlib.setStyle('none','#F00',1.0);\n"+
"$gfxlib.drawRect(16,16,32,32);\n"+
"$gfxlib.setStyle('#0F0','black',1.0);\n"+
"$gfxlib.drawStar(16,16,8,16,5,0);\n"+
"$gfxlib.drawStar(48,48,8,16,5,0);\n"+
"$gfxlib.scratchFromRegion(16,16,32,32);\n"+
"$gfxlib.drawStar(16,16,4,8,5,0);\n"+
"var scratch = $gfxlib.scratchEndCanvas();\n"+
"$gfxlib.drawScratch(scratch,80,16);\n"+
"$gfxlib.drawScratch(scratch,80,80);\n",
desc: "You can capture parts of things you have already drawn "+
"and repeat them or add to them."
};
ViewSimple = {
name: "Basic Viewport",
code: "//$gfxlib.viewSetCorners(0,0,64,64);\n"+
"$gfxlib.setStyle('none','red',1.0);\n"+
"$gfxlib.drawRect(0,0,64,64);\n"+
"$gfxlib.setStyle('blue','none',1.0);\n"+
"$gfxlib.drawRect(16,16,32,32);",
desc: "You can zoom or pan to different parts "+
"of the canvas using views. This canvas has the "+
"default view. Try uncommenting that line below."
};
ViewWrong = {
name: "Viewport Error",
code: "$gfxlib.setStyle('none','red',1.0);\n"+
"$gfxlib.drawRect(0,0,64,64);\n"+
"$gfxlib.setStyle('blue','none',1.0);\n"+
"$gfxlib.drawRect(16,16,32,32);\n"+
"$gfxlib.viewSetCorners(0,0,64,64);",
desc: "Don't set the after you draw stuff. Set the "+
"view first then draw everything."
};
ViewRotate = {
name: "Viewport Rotation",
code: "$gfxlib.viewSetCorners(0,0,64,64);\n"+
"$gfxlib.viewRotateTo(15);\n"+
"$gfxlib.setStyle('none','red',1.0);\n"+
"$gfxlib.drawRect(0,0,64,64);\n"+
"$gfxlib.setStyle('blue','none',1.0);\n"+
"$gfxlib.drawRect(16,16,32,32);",
desc: "You can also set the rotation of the viewport "+
"to a specific angle (in degrees). Positive angles "+
"make clockwise rotations."
};
ViewZoom = {
name: "Viewport Zooming",
code: "$gfxlib.viewSetCorners(0,0,64,64);\n"+
"$gfxlib.viewRotateTo(0);\n"+
"$gfxlib.viewZoomTo(1.5,1.5);\n"+
"$gfxlib.setStyle('none','red',1.0);\n"+
"$gfxlib.drawRect(0,0,64,64);\n"+
"$gfxlib.setStyle('blue','none',1.0);\n"+
"$gfxlib.drawRect(16,16,32,32);",
desc: "You can also set the rotation of the viewport "+
"to a specific angle (in degrees). Positive angles "+
"make clockwise rotations."
};
//http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-textalign
var examples = [
DrawRect,
SetStyle,
SetLine,
DrawRectCenter,
BasicCircle,
GradientLinear,
GradientRadial1,
GradientRadial2,
Checkerboard,
ClearAll,
DrawStar,
DrawPoly,
InvertAll,
InvertRegion,
Filters,
FilterColorSwap,
FilterHueShift,
Convolution,
FastConvolution,
DrawText,
MeasureText,
PathSimple,
PathMove,
PathRelative,
PathDirection,
PathDirectionRelative,
PathCurveTo,
PathCurveTurn,
PathBezierTo,
PathHasPoint,
ScratchSimple,
ScratchScaling,
ScratchRegion,
ScratchRegionScaling,
ScratchPattern,
ScratchFromRegion,
ViewSimple,
ViewWrong,
ViewRotate,
ViewZoom
];
function DrawModule(example,canvas){
$gfxlib.setCanvasNode(canvas);
eval(example.code);
};
function DrawPre(id){
var domCode = document.getElementById(id+'-edit');
var domPre = document.getElementById(id);
var code = domCode.value;
domPre.innerHTML = "";
domPre.appendChild(document.createTextNode(code));
domPre.style.display = 'block';
domCode.style.display = 'none';
$gfxlib.setCanvasNode(document.getElementById(id+'-canvas'));
$gfxlib.drawClearAll();
eval(code);
}
function HidePre(id){
document.getElementById(id).style.display = 'none';
document.getElementById(id+'-edit').style.display = 'block';
document.getElementById(id+'-edit').focus();
}
function gDrawPre(id){ return function(){DrawPre(id);}; }
function gHidePre(id){ return function(){HidePre(id);}; }
function CreateExampleDom(example){
// Create the div
var myContainer = document.createElement('div');
myContainer.className = 'preview-module';
var textContainer = document.createElement('div');
textContainer.className = 'text';
// Add the title
var myTitle = document.createElement('a');
myTitle.appendChild(document.createTextNode(example.name));
textContainer.appendChild(myTitle);
// Add the description
var myDesc = document.createElement('div');
myDesc.appendChild(document.createTextNode(example.desc));
myDesc.className = "desc";
textContainer.appendChild(myDesc);
// Add the code
var myCode = document.createElement('pre');
myCode.appendChild(document.createTextNode(example.code));
myCode.id = example.name;
textContainer.appendChild(myCode);
myCode.onclick = gHidePre(example.name);
var editArea = document.createElement('textarea');
editArea.style.display = 'none';
editArea.id = example.name + '-edit';
editArea.value = example.code;
editArea.onblur = gDrawPre(example.name);
textContainer.appendChild(editArea);
myContainer.appendChild(textContainer);
// Draw it
var myCanvas = document.createElement('canvas');
myCanvas.id = example.name+'-canvas';
myCanvas.width = 128;
myCanvas.height = 128;
DrawModule(example,myCanvas);
myContainer.appendChild(myCanvas);
document.body.appendChild(myContainer);
}
// ---------------------------------------------------------------- [ Main ] --
onload = function(){
//DrawRect.start();
//ViewSetByCorners1.start();
//NewCanvas.start();
//SetStyle.start();
//SetLine.start();
//DrawRectCenter.start();
//DrawCircle.start();
//GradientLinear.start();
//GradientRadial1.start();
//GradientRadial2.start();
//DrawCheckerboard.start();
//DrawClearAll.start();
//DrawModule(DrawRectData,document.getElementById("canvas-main"));
var nExamples = examples.length;
for(var i=0; i<nExamples; i+=1){
CreateExampleDom(examples[i]);
}
var start = {x: 3, y:4};
var end = {x:6, y:8};
function diff(a,b){
var ret = {};
for( key in a ){
if( key in b ){
ret[key] = a[key] - b[key];
}
}
return ret;
}
console.info(diff(end,start));
}
</script>
</body>
</html>