| 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 |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 ctx.lineDashOffset = 1; | 483 ctx.lineDashOffset = 1; |
| 484 ctx.strokeStyle = "#00FF00"; | 484 ctx.strokeStyle = "#00FF00"; |
| 485 ctx.strokeRect(width / 4, height / 5, width / 20, height / 8); | 485 ctx.strokeRect(width / 4, height / 5, width / 20, height / 8); |
| 486 ctx.setLineDash([]); | 486 ctx.setLineDash([]); |
| 487 ctx.strokeStyle = "#0000FF"; | 487 ctx.strokeStyle = "#0000FF"; |
| 488 ctx.strokeStyle = "rgba(128,128,128, 0.5)"; | 488 ctx.strokeStyle = "rgba(128,128,128, 0.5)"; |
| 489 ctx.strokeRect(width / 5, height / 10, width / 2, height / 8); | 489 ctx.strokeRect(width / 5, height / 10, width / 2, height / 8); |
| 490 log("Width = $width"); | 490 log("Width = $width"); |
| 491 } | 491 } |
| 492 | 492 |
| 493 // TODO(gram) - apparently a canvas can have only one op in its lifetime, | |
| 494 // so this test won't work until we can use off-screen canvases. | |
| 495 void compositeOp() { | |
| 496 initTest("Composition"); | |
| 497 var num = 0; | |
| 498 ctx.font = '10pt Verdana'; | |
| 499 var numPerRow = width ~/ 95; | |
| 500 log("Width = $width, numPerRow = $numPerRow\n"); | |
| 501 for (var mode in [ 'source-atop', 'source-in', | |
| 502 'source-out', 'source-over', | |
| 503 'destination-atop', 'destination-in', | |
| 504 'destination-out', 'destination-over', | |
| 505 'lighter', 'darker', | |
| 506 'xor', 'copy']) { | |
| 507 var col = num % numPerRow; | |
| 508 var row = num ~/ numPerRow; | |
| 509 ctx.globalCompositeOperation = mode; | |
| 510 ctx.translate(95 * col, 100 * row); | |
| 511 log("Doing $mode at row $row, col $col\n"); | |
| 512 ctx.beginPath(); | |
| 513 ctx.rect(0, 0, 55, 55); | |
| 514 ctx.fillStyle = 'blue'; | |
| 515 ctx.fill(); | |
| 516 ctx.beginPath(); | |
| 517 ctx.arc(50, 50, 35, 0, 2 * Math.PI, false); | |
| 518 ctx.fillStyle = 'red'; | |
| 519 ctx.fill(); | |
| 520 ctx.fillStyle = 'black'; | |
| 521 ctx.fillText(mode, 0, 100); | |
| 522 ctx.setTransform(1, 0, 0, 1, 0, 0); | |
| 523 ++num; | |
| 524 } | |
| 525 } | |
| 526 | |
| 527 void loadImage() { | 493 void loadImage() { |
| 528 initTest("Image loading"); | 494 initTest("Image loading"); |
| 529 var imageObj = new ImageElement(); | 495 var imageObj = new ImageElement(); |
| 530 imageObj.on.load.add((e) { | 496 imageObj.on.load.add((e) { |
| 531 ctx.drawImage(imageObj, 69, 50); | 497 ctx.drawImage(imageObj, 69, 50); |
| 532 }); | 498 }); |
| 533 imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.b
mp'; | 499 imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.b
mp'; |
| 534 } | 500 } |
| 535 | 501 |
| 536 void clip() { | 502 void clip() { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 // translate context to center of canvas | 557 // translate context to center of canvas |
| 592 ctx.translate(width / 2, height / 2); | 558 ctx.translate(width / 2, height / 2); |
| 593 | 559 |
| 594 // apply custom transform | 560 // apply custom transform |
| 595 ctx.transform(1, sy, sx, 1, 0, 0); | 561 ctx.transform(1, sy, sx, 1, 0, 0); |
| 596 | 562 |
| 597 ctx.fillStyle = 'blue'; | 563 ctx.fillStyle = 'blue'; |
| 598 ctx.fillRect(-rectWidth / 2, rectHeight / -2, rectWidth, rectHeight); | 564 ctx.fillRect(-rectWidth / 2, rectHeight / -2, rectWidth, rectHeight); |
| 599 } | 565 } |
| 600 | 566 |
| 601 int numtests = 21; | 567 void composite() { |
| 602 int testnum = numtests - 1; // Start with latest. | 568 initTest("Composition"); |
| 569 var num = 0; |
| 570 var numPerRow = width ~/ 150; |
| 571 var tempCanvas = new CanvasElement(width: width, height:height); |
| 572 var tempContext = tempCanvas.getContext("2d"); |
| 573 log("Width = $width, numPerRow = $numPerRow\n"); |
| 574 for (var mode in [ 'source-atop', 'source-in', |
| 575 'source-out', 'source-over', |
| 576 'destination-atop', 'destination-in', |
| 577 'destination-out', 'destination-over', |
| 578 'lighter', 'darker', |
| 579 'xor', 'copy']) { |
| 580 tempContext.save(); |
| 581 tempContext.clearRect(0, 0, width, height); |
| 582 tempContext.beginPath(); |
| 583 tempContext.rect(0, 0, 55, 55); |
| 584 tempContext.fillStyle = 'blue'; |
| 585 tempContext.fill(); |
| 586 |
| 587 tempContext.globalCompositeOperation = mode; |
| 588 tempContext.beginPath(); |
| 589 tempContext.arc(50, 50, 35, 0, 2 * Math.PI, false); |
| 590 tempContext.fillStyle = 'red'; |
| 591 tempContext.fill(); |
| 592 tempContext.restore(); |
| 593 tempContext.font = '10pt Verdana'; |
| 594 tempContext.fillStyle = 'black'; |
| 595 tempContext.fillText(mode, 0, 100); |
| 596 if (num > 0) { |
| 597 if ((num % numPerRow) == 0) { |
| 598 ctx.translate(-150 * (numPerRow-1), 150); |
| 599 } else { |
| 600 ctx.translate(150, 0); |
| 601 } |
| 602 } |
| 603 ctx.drawImage(tempCanvas, 0, 0); |
| 604 ++num; |
| 605 } |
| 606 } |
| 607 |
| 608 int testnum = -1; // Start with latest. |
| 603 | 609 |
| 604 void update() { | 610 void update() { |
| 605 if (!isDirty) return; | 611 if (!isDirty) return; |
| 606 switch(testnum) { | 612 switch(testnum) { |
| 607 case 0: | 613 case 0: |
| 608 helloWorld(); | 614 helloWorld(); |
| 609 break; | 615 break; |
| 610 case 1: | 616 case 1: |
| 611 smiley(); | 617 smiley(); |
| 612 break; | 618 break; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 break; | 666 break; |
| 661 case 18: | 667 case 18: |
| 662 loadImage(); | 668 loadImage(); |
| 663 break; | 669 break; |
| 664 case 19: | 670 case 19: |
| 665 clip(); | 671 clip(); |
| 666 break; | 672 break; |
| 667 case 20: | 673 case 20: |
| 668 shear(); | 674 shear(); |
| 669 break; | 675 break; |
| 676 case 21: |
| 677 composite(); |
| 678 break; |
| 670 default: | 679 default: |
| 671 testnum = 0; | 680 if (testnum < 0) { |
| 681 testnum = 21; |
| 682 } else { |
| 683 testnum = 0; |
| 684 } |
| 672 update(); | 685 update(); |
| 673 break; | 686 break; |
| 674 } | 687 } |
| 675 isDirty = false; | 688 isDirty = false; |
| 676 } | 689 } |
| 677 | 690 |
| 678 /* | 691 /* |
| 679 TODO(gram): below are the current entry points for input events for the | 692 TODO(gram): below are the current entry points for input events for the |
| 680 native code version. We need to integrate these somehow with the WebGL | 693 native code version. We need to integrate these somehow with the WebGL |
| 681 version: | 694 version: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 702 isDirty = true; | 715 isDirty = true; |
| 703 log("Marking dirty"); | 716 log("Marking dirty"); |
| 704 } | 717 } |
| 705 | 718 |
| 706 onKeyUp(num when, int flags, int keycode, int metastate, int repeat) {} | 719 onKeyUp(num when, int flags, int keycode, int metastate, int repeat) {} |
| 707 | 720 |
| 708 onKeyMultiple(num when, int flags, int keycode, int metastate, int repeat) { | 721 onKeyMultiple(num when, int flags, int keycode, int metastate, int repeat) { |
| 709 } | 722 } |
| 710 | 723 |
| 711 | 724 |
| OLD | NEW |