A JavaScript Image Map is a technique used to define clickable areas on an image, where each area links to a different web page or performs a specific action when clicked. Image maps are created by defining shapes (rectangles, circles, polygons) and assigning them to specific URLs or actions.
To create an Image Map in JavaScript, you can follow these steps:
- Create an image that you want to use as your map.
- Use an image editing software to create the clickable areas on the image. The clickable areas can be rectangular, circular or polygonal shapes. Save the image as a .gif, .jpg, or .png file.
- In your HTML file, insert an <img> tag that references the image file you created in step 2.
- Create a <map> tag that contains <area> tags. Each <area> tag defines a clickable area on the image and specifies the URL or action that should be performed when the area is clicked.
Here is an example code snippet:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Image Map</title>
</head>
<body>
<h1>JavaScript Image Map Example</h1>
<img src="image.jpg" usemap="#myMap">
<map name="myMap">
<area shape="rect" coords="0,0,100,100" href="http://www.example.com">
<area shape="circle" coords="200,200,50" href="http://www.example.com">
<area shape="poly" coords="300,300,400,300,350,350" href="http://www.example.com">
</map>
</body>
</html>
In this example, the <img> tag references an image file called “image.jpg”, and the <map> tag contains three <area> tags that define three clickable areas on the image. The first area is a rectangle, the second is a circle, and the third is a polygon. Each <area> tag specifies a URL that should be loaded when the area is clicked.