Does (HTML5) Canvas have the equivalent of an image map?
Asked Answered
T

5

16

Does the Canvas element have the equivalent of img's map, so you can define clickable areas on the canvas element?

There is brief mention of a map halfway down the page here: http://www.w3.org/TR/html5/the-canvas-element.html, but I can't find anything else about it.

Theriot answered 28/5, 2010 at 20:11 Comment(0)
P
15

Maps on Canvas are currently an open issue with HTML5. Issue #105 to be precise. See http://www.w3.org/html/wg/tracker/issues/105

Photoreconnaissance answered 28/5, 2010 at 23:20 Comment(1)
Update: The issue was closed in this working group decision.Baziotes
M
17

Even if there isn't, wouldn't an image map on a transparent image positioned over a canvas get you what you need?

Macassar answered 28/5, 2010 at 20:13 Comment(0)
P
15

Maps on Canvas are currently an open issue with HTML5. Issue #105 to be precise. See http://www.w3.org/html/wg/tracker/issues/105

Photoreconnaissance answered 28/5, 2010 at 23:20 Comment(1)
Update: The issue was closed in this working group decision.Baziotes
L
6

As a solution to your problem: I would attach a click event to the canvas and in the mouse-event I would examine the mouse-coords, then a simple list of areas combined with polygons you could do some sort of collision test on. Rectangles would be a start, but if you truely need special areas, a more advanced collision test would have to be made.

A quick solution I have used in Flash for pixel precision maps, is to make a second hidden overlay bitmap, that you use for lookup when someone click on the visible image.

Its same technic used in old adventure games. Each "area/object/link" has its own pixel color. And then you just keep the list of objects with the matching color.

Once you have the pixelcolor under the mousecoord, then you can do a very quick lookup in the table.. and bingo...

First you clear the "click image" with zero's (black) and thats equal to "no link", then you draw every area with a special color and store this color in the list.

Ask if you need more help. I hope this was a usefull answer.

Leff answered 1/7, 2010 at 14:8 Comment(0)
U
2

Have you considered using svg instead of canvas? With svg the graphic itself can contain all the information you need for identifying active regions, and you can attach event handlers just like you would on html elements.

Some examples of how you can detect mouseevents on different parts of the geometry of svg shapes:

http://dev.w3.org/SVG/profiles/1.1F2/test/harness/htmlObject/interact-pevents-08-f.html

http://dev.w3.org/SVG/profiles/1.1F2/test/harness/htmlObject/interact-pevents-09-f.html

Ussery answered 29/5, 2010 at 10:20 Comment(0)
H
2

Take a look at Cake. It's a scene graph plug-in for canvas. Might be a little too complicated for what you're looking for, but you can add events to Cake wrapped canvas objects.

Hal answered 16/8, 2010 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.