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