4 <title>Canvas Hit Regions: path2d test
</title>
5 <script src=
"../../resources/js-test.js"></script>
8 <canvas id=
"canvas" width=
"400" height=
"400">
9 <button id=
"face"></button>
10 <button id=
"eyes"></button>
13 <script src=
"./resources/canvas-hit-region-event.js"></script>
16 var canvas
= document
.getElementById("canvas");
17 var context
= canvas
.getContext("2d");
19 var face
= new Path2D();
20 context
.fillStyle
= "pink";
21 face
.arc(200, 175, 150, 0, Math
.PI
* 2, true);
23 context
.addHitRegion({ id
: "face", control
: document
.getElementById("face"), path
: face
});
25 var nose
= new Path2D();
26 context
.fillStyle
= "black";
27 context
.globalAlpha
= .5;
28 nose
.moveTo(200, 165);
29 nose
.lineTo(240, 205);
30 nose
.lineTo(160, 205);
33 context
.addHitRegion({ id
: "nose", path
: nose
});
35 var mouth
= new Path2D();
36 context
.fillStyle
= "red";
37 mouth
.rect(125, 240, 150, 20);
39 context
.addHitRegion({ id
: "mouth", path
: mouth
});
41 var eyes
= new Path2D();
42 context
.globalAlpha
= 1;
43 context
.fillStyle
= "blue";
44 eyes
.arc(150, 125, 25, 0, Math
.PI
* 2, true);
45 eyes
.arc(250, 125, 25, 0, Math
.PI
* 2, true);
47 context
.addHitRegion({ id
: "eye", control
: document
.getElementById("eyes"), path
: eyes
});
49 debug("Hit detection and mouse event tests");
50 shouldBe("clickCanvas(100, 100)", "'face'");
51 shouldBe("clickCanvas(200, 200)", "'nose'");
52 shouldBe("clickCanvas(127, 242)", "'mouth'");
53 shouldBe("clickCanvas(150, 125)", "'eye'");
54 shouldBe("clickCanvas(250, 125)", "'eye'");
55 shouldBe("clickCanvas(200, 125)", "'face'");
56 shouldBe("clickCanvas(20, 10)", "null");