4 <title>Canvas Hit Regions: path2d with transform test
</title>
5 <script src=
"../../resources/js-test.js"></script>
8 <canvas id=
"canvas" width=
"400" height=
"400"></canvas>
9 <script src=
"./resources/canvas-hit-region-event.js"></script>
12 var canvas
= document
.getElementById("canvas");
13 var context
= canvas
.getContext("2d");
16 function clickTests(message
, tests
)
23 for (var i
= 0; i
< tests
.length
; i
++)
26 actual
: clickCanvas(tests
[i
].x
, tests
[i
].y
),
27 expected
: tests
[i
].expected
36 function createHitRegion(transformMethod
, isRect
)
38 context
.removeHitRegion("hit");
41 context
.addHitRegion({
43 path
: drawSomething(isRect
)
48 function drawSomething(isRect
)
50 var path
= new Path2D();
53 path
.rect(0, 0, 50, 50);
59 path
.lineTo(-15, -10);
60 path
.lineTo(-50, -10);
72 // Rectangle with context.translate()
73 createHitRegion(function() {
74 context
.translate(20, 20);
76 clickTests("Rectangle with context.translate():", [
77 { x
: 1, y
: 1, expected
: null },
78 { x
: 31, y
: 21, expected
: "hit" },
79 { x
: 51, y
: 51, expected
: "hit" },
80 { x
: 10, y
: 5, expected
: null },
81 { x
: 61, y
: 61, expected
: "hit" }
84 // Rectangle with context.rotate()
85 createHitRegion(function() {
86 context
.rotate(Math
.PI
* 0.25); // 45 degrees
88 clickTests("Rectangle with context.rotate():", [
89 { x
: 20, y
: 5, expected
: null },
90 { x
: 0, y
: 25, expected
: "hit" },
91 { x
: 49, y
: 49, expected
: null },
92 { x
: 0, y
: 51, expected
: "hit" },
95 // Rectangle with context.scale()
96 createHitRegion(function() {
99 clickTests("Rectangle with context.scale():", [
100 { x
: 1, y
: 1, expected
: "hit" },
101 { x
: 49, y
: 49, expected
: "hit" },
102 { x
: 51, y
: 51, expected
: "hit" },
103 { x
: 99, y
: 99, expected
: "hit" },
106 // Non rectangle (star) with context.translate()
107 createHitRegion(function() {
108 context
.translate(50, 50);
110 clickTests("Non rectangle (star) with context.translate():", [
111 { x
: 26, y
: 23, expected
: null },
112 { x
: 82, y
: 65, expected
: null },
113 { x
: 51, y
: 21, expected
: "hit" },
114 { x
: 74, y
: 49, expected
: "hit" },
115 { x
: 49, y
: 88, expected
: null },
116 { x
: 13, y
: 65, expected
: null },
117 { x
: 66, y
: 76, expected
: "hit" },
118 { x
: 76, y
: 23, expected
: null },
119 { x
: 38, y
: 76, expected
: "hit" },
120 { x
: 28, y
: 47, expected
: "hit" },
123 // Non rectangle (star) with context.rotate()
124 createHitRegion(function() {
125 context
.translate(50, 50);
126 context
.rotate(Math
.PI
* 0.25);
128 clickTests("Non rectangle (star) with context.rotate():", [
129 { x
: 26, y
: 23, expected
: "hit" },
130 { x
: 82, y
: 65, expected
: null },
131 { x
: 51, y
: 21, expected
: null },
132 { x
: 74, y
: 49, expected
: null },
133 { x
: 49, y
: 88, expected
: null },
134 { x
: 13, y
: 65, expected
: null },
135 { x
: 66, y
: 76, expected
: null },
136 { x
: 76, y
: 23, expected
: "hit" },
137 { x
: 38, y
: 76, expected
: "hit" },
138 { x
: 28, y
: 47, expected
: null },
141 // Non rectangle (star) with context.scale()
142 createHitRegion(function() {
143 context
.translate(25, 25);
144 context
.scale(0.5, 0.5);
146 clickTests("Non rectangle (star) with context.scale():", [
147 { x
: 28, y
: 13, expected
: "hit" },
148 { x
: 38, y
: 24, expected
: "hit" },
149 { x
: 34, y
: 38, expected
: "hit" },
150 { x
: 13, y
: 12, expected
: null },
151 { x
: 36, y
: 12, expected
: null },
152 { x
: 40, y
: 33, expected
: null },
153 { x
: 9, y
: 31, expected
: null },
154 { x
: 18, y
: 41, expected
: "hit" },
155 { x
: 12, y
: 25, expected
: "hit" },
156 { x
: 25, y
: 42, expected
: null },
159 for (var i
= 0; i
< testSet
.length
; i
++) {
160 var test
= testSet
[i
];
162 if (test
.type
== "debug") {
165 else if (test
.type
== "shouldBe") {
166 window
.region
= test
.expected
;
167 if (test
.expected
== null)
168 shouldBe("region", String(test
.actual
));
170 shouldBe("region", "'" + test
.actual
+ "'");