OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Canvas Hit Regions: transform test</title> |
| 5 <script src="../../resources/js-test.js"></script> |
| 6 </head> |
| 7 <body> |
| 8 <canvas id="canvas" width="400" height="400"></canvas> |
| 9 <script> |
| 10 |
| 11 var canvas = document.getElementById("canvas"); |
| 12 var context = canvas.getContext("2d"); |
| 13 var testSet = []; |
| 14 |
| 15 function clickCanvas(x, y) |
| 16 { |
| 17 if (!window.eventSender) |
| 18 return "This test requires eventSender"; |
| 19 |
| 20 var result = null; |
| 21 function listener(event) |
| 22 { |
| 23 canvas.removeEventListener("click", listener, false); |
| 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 |
| 33 return result; |
| 34 } |
| 35 |
| 36 function clickTests(message, tests) |
| 37 { |
| 38 testSet.push({ |
| 39 type : "debug", |
| 40 message : message |
| 41 }); |
| 42 |
| 43 for (var i = 0; i < tests.length; i++) |
| 44 testSet.push({ |
| 45 type : "shouldBe", |
| 46 actual : clickCanvas(tests[i].x, tests[i].y), |
| 47 expected : tests[i].expected |
| 48 }); |
| 49 |
| 50 testSet.push({ |
| 51 type : "debug", |
| 52 message : "" |
| 53 }); |
| 54 } |
| 55 |
| 56 function createHitRegion(pathMethod) |
| 57 { |
| 58 context.removeHitRegion("hit"); |
| 59 context.beginPath(); |
| 60 context.save(); |
| 61 pathMethod(); |
| 62 context.restore(); |
| 63 context.addHitRegion({ |
| 64 id : "hit" |
| 65 }); |
| 66 } |
| 67 |
| 68 function drawStar() |
| 69 { |
| 70 context.beginPath(); |
| 71 context.moveTo(0, -50); |
| 72 context.lineTo(-15, -10); |
| 73 context.lineTo(-50, -10); |
| 74 context.lineTo(-15, 10); |
| 75 context.lineTo(-35, 50); |
| 76 context.lineTo(0, 20); |
| 77 context.lineTo(35, 50); |
| 78 context.lineTo(15, 10); |
| 79 context.lineTo(50, -10); |
| 80 context.lineTo(15, -10); |
| 81 context.lineTo(0, -50); |
| 82 } |
| 83 |
| 84 // Rectangle with context.translate() |
| 85 createHitRegion(function() { |
| 86 context.translate(20, 20); |
| 87 context.rect(0, 0, 50, 50); |
| 88 }); |
| 89 clickTests("Rectangle with context.translate():", [ |
| 90 { x : 1, y : 1, expected : null }, |
| 91 { x : 31, y : 21, expected : "hit" }, |
| 92 { x : 51, y : 51, expected : "hit" }, |
| 93 { x : 10, y : 5, expected : null }, |
| 94 { x : 61, y : 61, expected : "hit" } |
| 95 ]); |
| 96 |
| 97 // Rectangle with context.rotate() |
| 98 createHitRegion(function() { |
| 99 context.rotate(Math.PI * 0.25); // 45 degrees |
| 100 context.rect(0, 0, 50, 50); |
| 101 }); |
| 102 clickTests("Rectangle with context.rotate():", [ |
| 103 { x : 20, y : 5, expected : null }, |
| 104 { x : 0, y : 25, expected : "hit" }, |
| 105 { x : 49, y : 49, expected : null }, |
| 106 { x : 0, y : 51, expected : "hit" }, |
| 107 ]); |
| 108 |
| 109 // Rectangle with context.scale() |
| 110 createHitRegion(function() { |
| 111 context.scale(2, 2); |
| 112 context.rect(0, 0, 50, 50); |
| 113 }); |
| 114 clickTests("Rectangle with context.scale():", [ |
| 115 { x : 1, y : 1, expected : "hit" }, |
| 116 { x : 49, y : 49, expected : "hit" }, |
| 117 { x : 51, y : 51, expected : "hit" }, |
| 118 { x : 99, y : 99, expected : "hit" }, |
| 119 ]); |
| 120 |
| 121 // Non rectangle (star) with context.translate() |
| 122 createHitRegion(function() { |
| 123 context.translate(50, 50); |
| 124 drawStar(); |
| 125 }); |
| 126 clickTests("Non rectangle (star) with context.translate():", [ |
| 127 { x : 26, y : 23, expected : null }, |
| 128 { x : 82, y : 65, expected : null }, |
| 129 { x : 51, y : 21, expected : "hit" }, |
| 130 { x : 74, y : 49, expected : "hit" }, |
| 131 { x : 49, y : 88, expected : null }, |
| 132 { x : 13, y : 65, expected : null }, |
| 133 { x : 66, y : 76, expected : "hit" }, |
| 134 { x : 76, y : 23, expected : null }, |
| 135 { x : 38, y : 76, expected : "hit" }, |
| 136 { x : 28, y : 47, expected : "hit" }, |
| 137 ]); |
| 138 |
| 139 // Non rectangle (star) with context.rotate() |
| 140 createHitRegion(function() { |
| 141 context.translate(50, 50); |
| 142 context.rotate(Math.PI * 0.25); |
| 143 drawStar(); |
| 144 }); |
| 145 clickTests("Non rectangle (star) with context.rotate():", [ |
| 146 { x : 26, y : 23, expected : "hit" }, |
| 147 { x : 82, y : 65, expected : null }, |
| 148 { x : 51, y : 21, expected : null }, |
| 149 { x : 74, y : 49, expected : null }, |
| 150 { x : 49, y : 88, expected : null }, |
| 151 { x : 13, y : 65, expected : null }, |
| 152 { x : 66, y : 76, expected : null }, |
| 153 { x : 76, y : 23, expected : "hit" }, |
| 154 { x : 38, y : 76, expected : "hit" }, |
| 155 { x : 28, y : 47, expected : null }, |
| 156 ]); |
| 157 |
| 158 // Non rectangle (star) with context.scale() |
| 159 createHitRegion(function() { |
| 160 context.translate(25, 25); |
| 161 context.scale(0.5, 0.5); |
| 162 drawStar(); |
| 163 }); |
| 164 clickTests("Non rectangle (star) with context.scale():", [ |
| 165 { x : 28, y : 13, expected : "hit" }, |
| 166 { x : 38, y : 24, expected : "hit" }, |
| 167 { x : 34, y : 38, expected : "hit" }, |
| 168 { x : 13, y : 12, expected : null }, |
| 169 { x : 36, y : 12, expected : null }, |
| 170 { x : 40, y : 33, expected : null }, |
| 171 { x : 9, y : 31, expected : null }, |
| 172 { x : 18, y : 41, expected : "hit" }, |
| 173 { x : 12, y : 25, expected : "hit" }, |
| 174 { x : 25, y : 42, expected : null }, |
| 175 ]); |
| 176 |
| 177 for (var i = 0; i < testSet.length; i++) { |
| 178 var test = testSet[i]; |
| 179 |
| 180 if (test.type == "debug") { |
| 181 debug(test.message); |
| 182 } |
| 183 else if (test.type == "shouldBe") { |
| 184 window.region = test.expected; |
| 185 if (test.expected == null) |
| 186 shouldBe("region", String(test.actual)); |
| 187 else |
| 188 shouldBe("region", "'" + test.actual + "'"); |
| 189 } |
| 190 } |
| 191 |
| 192 </script> |
| 193 </body> |
| 194 </html> |
OLD | NEW |