OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Canvas Hit Regions: clear test</title> |
| 5 <script src="../../resources/js-test.js"></script> |
| 6 </head> |
| 7 <body> |
| 8 <canvas id="canvas" width="400" height="400"> |
| 9 <button id="yellow"></button> |
| 10 </canvas> |
| 11 <script> |
| 12 |
| 13 var canvas = document.getElementById("canvas"); |
| 14 var context = canvas.getContext("2d"); |
| 15 |
| 16 function clickCanvas(x, y) |
| 17 { |
| 18 if (!window.eventSender) |
| 19 return "This test requires eventSender"; |
| 20 |
| 21 var result = null; |
| 22 function listener(event) |
| 23 { |
| 24 result = event.region; |
| 25 } |
| 26 |
| 27 var rect = canvas.getBoundingClientRect(); |
| 28 canvas.addEventListener("click", listener, false); |
| 29 eventSender.mouseMoveTo(rect.left + x, rect.top + y); |
| 30 eventSender.mouseDown(); |
| 31 eventSender.mouseUp(); |
| 32 canvas.removeEventListener("click", listener, false); |
| 33 |
| 34 return result; |
| 35 } |
| 36 |
| 37 shouldBe("internals.countHitRegions(context)", "0"); |
| 38 |
| 39 context.fillStyle = "red"; |
| 40 context.rect(0, 0, 100, 100); |
| 41 context.fill(); |
| 42 context.addHitRegion({ id : "red" }); |
| 43 shouldBe("internals.countHitRegions(context)", "1"); |
| 44 |
| 45 context.beginPath(); |
| 46 context.fillStyle = "yellow"; |
| 47 context.rect(100, 100, 50, 50); |
| 48 context.fill(); |
| 49 context.addHitRegion({ id : "yellow", control : document.getElementById("yello
w") }); |
| 50 shouldBe("internals.countHitRegions(context)", "2"); |
| 51 |
| 52 shouldBe("clickCanvas(60, 60)", "'red'"); |
| 53 shouldBe("clickCanvas(120, 120)", "'yellow'"); |
| 54 |
| 55 context.clearRect(50, 50, 50, 50); |
| 56 shouldBe("internals.countHitRegions(context)", "2"); |
| 57 shouldBe("clickCanvas(60, 60)", "null"); |
| 58 shouldBe("clickCanvas(120, 120)", "'yellow'"); |
| 59 |
| 60 context.clearRect(100, 100, 50, 50); |
| 61 shouldBe("internals.countHitRegions(context)", "1"); |
| 62 shouldBe("clickCanvas(120, 120)", "null"); |
| 63 |
| 64 context.beginPath(); |
| 65 context.fillStyle = "red"; |
| 66 context.rect(60, 60, 40, 40); |
| 67 context.fill(); |
| 68 shouldBe("clickCanvas(40, 40)", "'red'"); |
| 69 context.addHitRegion({ id : "red" }); |
| 70 shouldBe("internals.countHitRegions(context)", "1"); |
| 71 shouldBe("clickCanvas(62, 62)", "'red'"); |
| 72 shouldBe("clickCanvas(40, 40)", "null"); |
| 73 |
| 74 context.beginPath(); |
| 75 context.fillStyle = "yellow"; |
| 76 context.rect(0, 0, 50, 50); |
| 77 context.fill(); |
| 78 context.addHitRegion({ id : "yellow", control : document.getElementById("yello
w") }); |
| 79 shouldBe("internals.countHitRegions(context)", "2"); |
| 80 shouldBe("clickCanvas(40, 40)", "'yellow'"); |
| 81 |
| 82 context.beginPath(); |
| 83 context.fillStyle = "yellow"; |
| 84 context.rect(100, 0, 50, 50); |
| 85 context.fill(); |
| 86 context.addHitRegion({ control : document.getElementById("yellow") }); |
| 87 shouldBe("internals.countHitRegions(context)", "2"); |
| 88 shouldBe("clickCanvas(40, 40)", "null"); |
| 89 shouldBe("clickCanvas(101, 1)", "null"); |
| 90 |
| 91 context.beginPath(); |
| 92 context.fillStyle = "blue"; |
| 93 context.rect(100, 50, 20, 20); |
| 94 context.fill(); |
| 95 context.addHitRegion({ id : "blue" }); |
| 96 shouldBe("internals.countHitRegions(context)", "3"); |
| 97 shouldBe("clickCanvas(101, 51)", "'blue'"); |
| 98 |
| 99 context.removeHitRegion("blue"); |
| 100 shouldBe("internals.countHitRegions(context)", "2"); |
| 101 shouldBe("clickCanvas(101, 51)", "null"); |
| 102 |
| 103 context.clearHitRegions(); |
| 104 shouldBe("internals.countHitRegions(context)", "0"); |
| 105 shouldBe("clickCanvas(62, 62)", "null"); |
| 106 |
| 107 context.clearRect(0, 0, 400, 400); |
| 108 shouldBe("internals.countHitRegions(context)", "0"); |
| 109 |
| 110 debug(""); |
| 111 |
| 112 </script> |
| 113 </body> |
| 114 </html> |
OLD | NEW |