OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // Canvas API tests. Some of these are adapted from: | 5 // Canvas API tests. Some of these are adapted from: |
6 // | 6 // |
7 // http://www.html5canvastutorials.com/ | 7 // http://www.html5canvastutorials.com/ |
8 | 8 |
9 library openglui_canvas_tests; | 9 library openglui_canvas_tests; |
10 | 10 |
11 import 'gl.dart'; | 11 import 'gl.dart'; |
12 import 'dart:math' as Math; | 12 import 'dart:math' as Math; |
13 | 13 |
14 var ctx; | 14 var ctx; |
15 var width, height; | 15 var width, height; |
16 bool isDirty = true; | 16 bool isDirty = true; |
17 var canvas; | 17 var canvas; |
18 | 18 |
19 void resize(int w, int h) { | 19 void resize(int w, int h) { |
20 width = w; | 20 width = w; |
21 height = h; | 21 height = h; |
22 } | 22 } |
23 | 23 |
24 void setup(canvasp, int w, int h) { | 24 void setup(canvasp, int w, int h, int f) { |
25 if (canvasp == null) { | 25 if (canvasp == null) { |
26 log("Allocating canvas"); | 26 log("Allocating canvas"); |
27 canvas = new CanvasElement(width: w, height: h); | 27 canvas = new CanvasElement(width: w, height: h); |
28 document.body.nodes.add(canvas); | 28 document.body.nodes.add(canvas); |
29 } else { | 29 } else { |
30 log("Using parent canvas"); | 30 log("Using parent canvas"); |
31 canvas = canvasp; | 31 canvas = canvasp; |
32 } | 32 } |
33 canvas.onMouseDown.listen((e) { | 33 canvas.onMouseDown.listen((e) { |
34 ++testnum; | 34 ++testnum; |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 ctx.setLineDash([]); | 493 ctx.setLineDash([]); |
494 ctx.strokeStyle = "#0000FF"; | 494 ctx.strokeStyle = "#0000FF"; |
495 ctx.strokeStyle = "rgba(128,128,128, 0.5)"; | 495 ctx.strokeStyle = "rgba(128,128,128, 0.5)"; |
496 ctx.strokeRect(width / 5, height / 10, width / 2, height / 8); | 496 ctx.strokeRect(width / 5, height / 10, width / 2, height / 8); |
497 log("Width = $width"); | 497 log("Width = $width"); |
498 } | 498 } |
499 | 499 |
500 void loadImage() { | 500 void loadImage() { |
501 initTest("Image loading"); | 501 initTest("Image loading"); |
502 var imageObj = new ImageElement(); | 502 var imageObj = new ImageElement(); |
503 var sub; | 503 // Setting src before onLoad is a more interesting test. |
504 sub = imageObj.onLoad.listen((e) { | 504 imageObj.src = 'chrome.png'; |
505 ctx.drawImage(e.target, 69, 50); | 505 imageObj.onLoad.listen((e) { |
506 sub.cancel(); | 506 ctx.drawImage(e.target, 0, 0, width, height, 0, 0, width, height); |
507 }); | 507 }); |
508 imageObj.src = 'chrome.png'; | |
509 } | 508 } |
510 | 509 |
511 void clip() { | 510 void clip() { |
512 initTest("Clipping"); | 511 initTest("Clipping"); |
513 var x = width / 2; | 512 var x = width / 2; |
514 var y = height / 2; | 513 var y = height / 2; |
515 var radius = height / 4; | 514 var radius = height / 4; |
516 var offset = 2 * radius / 3; | 515 var offset = 2 * radius / 3; |
517 | 516 |
518 ctx.save(); | 517 ctx.save(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 | 648 |
650 ctx.beginPath(); | 649 ctx.beginPath(); |
651 ctx.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height); | 650 ctx.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height); |
652 ctx.fillStyle = '#8ED6FF'; | 651 ctx.fillStyle = '#8ED6FF'; |
653 ctx.fill(); | 652 ctx.fill(); |
654 ctx.lineWidth = myRectangle.borderWidth; | 653 ctx.lineWidth = myRectangle.borderWidth; |
655 ctx.strokeStyle = 'black'; | 654 ctx.strokeStyle = 'black'; |
656 ctx.stroke(); | 655 ctx.stroke(); |
657 } | 656 } |
658 | 657 |
| 658 void linearGradient() { |
| 659 initTest("Linear Gradient"); |
| 660 ctx.rect(0, 0, width, height); |
| 661 var grd = ctx.createLinearGradient(0, 0, width, height); |
| 662 // light blue |
| 663 grd.addColorStop(0, '#8ED6FF'); |
| 664 // dark blue |
| 665 grd.addColorStop(1, '#004CB3'); |
| 666 ctx.fillStyle = grd; |
| 667 ctx.fill(); |
| 668 } |
| 669 |
| 670 void radialGradient() { |
| 671 initTest("Radial Gradient"); |
| 672 ctx.rect(0, 0, width, height); |
| 673 var grd = ctx.createRadialGradient(238, 50, 10, 238, 50, 300); |
| 674 // light blue |
| 675 grd.addColorStop(0, '#8ED6FF'); |
| 676 // dark blue |
| 677 grd.addColorStop(1, '#004CB3'); |
| 678 ctx.fillStyle = grd; |
| 679 ctx.fill(); |
| 680 } |
| 681 |
659 int testnum = 0; // Set this to -1 to start with last test. | 682 int testnum = 0; // Set this to -1 to start with last test. |
660 | 683 |
| 684 double x, y, z; |
| 685 |
| 686 onAccelerometer(double xx, double yy, double zz) { |
| 687 x = xx; |
| 688 y = yy; |
| 689 z = zz; |
| 690 } |
| 691 |
661 void update(num when) { | 692 void update(num when) { |
662 window.requestAnimationFrame(update); | 693 window.requestAnimationFrame(update); |
663 if (testnum == 0) { | 694 if (testnum == 0) { |
664 anim(); | 695 anim(); |
665 return; | 696 return; |
666 } | 697 } |
667 if (!isDirty) return; | 698 if (!isDirty) return; |
668 switch(testnum) { | 699 switch(testnum) { |
669 case 1: | 700 case 1: |
670 helloWorld(); | 701 helloWorld(); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 case 20: | 757 case 20: |
727 shear(); | 758 shear(); |
728 break; | 759 break; |
729 case 21: | 760 case 21: |
730 composite(); | 761 composite(); |
731 break; | 762 break; |
732 case 22: | 763 case 22: |
733 smiley(); | 764 smiley(); |
734 break; | 765 break; |
735 case 23: | 766 case 23: |
| 767 linearGradient(); |
| 768 break; |
| 769 case 24: |
| 770 radialGradient(); |
| 771 break; |
| 772 case 25: |
| 773 break; // Skip for now; this is really slow. |
736 var rayTracer = new RayTracer(); | 774 var rayTracer = new RayTracer(); |
737 rayTracer.render(defaultScene(), ctx, width, height); | 775 rayTracer.render(defaultScene(), ctx, width, height); |
738 break; | 776 break; |
739 default: | 777 default: |
740 if (testnum < 0) { | 778 if (testnum < 0) { |
741 testnum = 23; | 779 testnum = 24; |
742 } else { | 780 } else { |
743 testnum = 0; | 781 testnum = 0; |
744 } | 782 } |
745 return; | 783 return; |
746 } | 784 } |
| 785 |
747 isDirty = false; | 786 isDirty = false; |
748 } | 787 } |
749 | 788 |
750 // Raytracer adapted from https://gist.github.com/mythz/3817303. | 789 // Raytracer adapted from https://gist.github.com/mythz/3817303. |
751 | 790 |
752 Scene defaultScene() => | 791 Scene defaultScene() => |
753 new Scene( | 792 new Scene( |
754 [new Plane(new Vector(0.0, 1.0, 0.0), 0.0, Surfaces.checkerboard), | 793 [new Plane(new Vector(0.0, 1.0, 0.0), 0.0, Surfaces.checkerboard), |
755 new Sphere(new Vector(0.0, 1.0, -0.25), 1.0, Surfaces.shiny), | 794 new Sphere(new Vector(0.0, 1.0, -0.25), 1.0, Surfaces.shiny), |
756 new Sphere(new Vector(-1.0, 0.5, 1.5), 0.5, Surfaces.shiny)], | 795 new Sphere(new Vector(-1.0, 0.5, 1.5), 0.5, Surfaces.shiny)], |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 for (int x = 0; x < screenWidth; x++) { | 1038 for (int x = 0; x < screenWidth; x++) { |
1000 var color = _traceRay(new Ray(scene.camera.pos, | 1039 var color = _traceRay(new Ray(scene.camera.pos, |
1001 getPoint(x, y, scene.camera) ), scene, 0); | 1040 getPoint(x, y, scene.camera) ), scene, 0); |
1002 ctx.fillStyle = Color.toDrawingRGB(color); | 1041 ctx.fillStyle = Color.toDrawingRGB(color); |
1003 ctx.fillRect(x, y, x + 1, y + 1); | 1042 ctx.fillRect(x, y, x + 1, y + 1); |
1004 } | 1043 } |
1005 } | 1044 } |
1006 } | 1045 } |
1007 } | 1046 } |
1008 | 1047 |
OLD | NEW |