graphic map and paintbrush

I've implemented an image map using shape circles, but now I'm looking for a way to draw visible circles or icons on the map.

Is there a solution for this issue?

Currently, I have used a canvas overlay on the image to draw on it

This method worked successfully

However, the problem arises when the image is within a container div with the overflow hidden property applied. This causes issues with zooming in and out as parts of the image are hidden.

With the canvas element, I faced challenges trying to maintain the overflow hidden behavior. When positioning the canvas absolutely, the overflow hidden property didn't work as expected. On the other hand, setting the position relative caused the image below the canvas to disappear.

A JavaScript function called drawCir has been created to draw circles based on coordinates provided.
The myInit function initializes the canvas and sets up the drawing context.
It calculates the position and size of the target image and places the canvas over the image.
The canvas is styled to be transparent to mouse events and positioned absolutely above the image.
The color and width properties for fill and stroke operations are also set.
Then, each area element within the image map is iterated through to draw circles on the canvas based on their coordinates.
#myCanvas
{
    pointer-events: none;
    position:absolute;

}

#con{
overflow: hidden;
height: 600px;
width: 100%;
}
#img{
width:100%;
height:100%;
position:relative;

}
<div id="con">
                    <canvas id="myCanvas"></canvas>
                    <img src="images/img.png" alt="" id="img" usemap="#img_map">
                    <map name="img_map"><area shape=circle>......</map>
       </div>
               //fucntion to draw  called in body tag

Answer №1

Here is how you can add a z-index to your canvas element:

function drawCircle(coordinates) {
    var mCoords = coordinates.split(',');
    var x, y, r;
    x = mCoords[0];
    y = mCoords[1];
    r = mCoords[2];
    hdc.beginPath();
    hdc.arc(x, y, r, 0, 2 * Math.PI);
    hdc.fill();
    hdc.stroke();
}


function initializeCanvas() {
    // get the target image
    var img = byId('mape');

    var x, y, w, h;

    // get its position and width+height
    x = img.offsetLeft;
    y = img.offsetTop;
    w = img.clientWidth;
    h = img.clientHeight;

    // move the canvas, so it's contained by the same parent as the image
    var imgParent = img.parentNode;
    var can = byId('myCanvas');
    // imgParent.appendChild(can);

    // place the canvas in front of the image
    can.style.zIndex = 1;

    // position it over the image
    can.style.left = x + 'px';
    can.style.top = y + 'px';

    // make same size as the image
    can.setAttribute('width', w + 'px');
    can.setAttribute('height', h + 'px');

    // get its context
    hdc = can.getContext('2d');

    // set the 'default' values for the colour/width of fill/stroke operations
    hdc.fillStyle = 'red';
    hdc.strokeStyle = 'red';
    hdc.lineWidth = 2;

    $("area").each(function() {

        var coordStr = $(this).attr('coords');
        drawCircle(coordStr);
    });


}
#myCanvas
{
    pointer-events: none;       /* make the canvas transparent to the mouse - needed since canvas is positioned in front of image */
    position:absolute;
    z-index: 2;
}

#con{
overflow: hidden;
height: 600px;
width: 100%;
}
#img{
width:100%;
height:100%;
position:relative;

}
<div id="con">
                    <canvas id="myCanvas"></canvas>
                    <img src="images/img.png" alt="" id="img" usemap="#img_map">
                    <map name="img_map"><area shape=circle>......</map>
       </div>
               //function to draw called in body tag

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the setTimeout function in JavaScript

As I understand it, the setTimeout function creates a new thread that waits for x milliseconds before executing the JavaScript function. setTimeout(functionName, timeInms); My question is: Is there a way to instruct it to run after all other JS on the pa ...

Utilizing dropdown list values within the context of $.getJSON: A practical guide

This is the script that I have written. $.getJSON("Employee.js", function (data) { var sample = data.one;alert(sample) }); Below you can see the contents of the Employee.js file: var sample={ "one":"Manager","two":"Sr.Eng","three":"Eng" } I am ...

JavaScript events for scrolling horizontally and vertically across multiple browsers

Is there a way to capture mousewheel and touchpad events, including on Mac, using JavaScript? Ideally, I want to get deltaX and deltaY values for both horizontal and vertical movements. I came across a website - - that appears to handle touchpad events ho ...

When sending an Axios POST request, a "Network Error" message may be received even when the status code is 200 and there is a valid response

Currently, I am utilizing Vue JS version 2.5 along with Axios: "vue": "^2.5.17", "vue-axios": "^2.1.4", "axios": "^0.18.0", The main issue I am facing involves making a POST call like so: const data = querystring.stringify({ 'email& ...

Using JavaScript to create customized checkboxes is a useful way to

I am looking to develop a JavaScript code that saves all the checkboxes selected by a user. When the user clicks on the finish button, the code should display what they have chosen (text within the label). Admittedly, I am unsure of how to proceed and wou ...

Explore the wonders of generating number permutations using JavaScript recursion!

After providing the input of 85 to this function, I noticed that it only returns 85. I am confused as to why it is not recursively calling itself again with 5 as the first number. console.log(PermutationStep(85)); function PermutationStep(num) { var ...

Combining shared table rows with embedded ruby code in Javascript - is it possible?

I'm a new Javascript learner and I'm attempting to create a function that will merge rows with the same value (year in this case) and add their numbers together. Although my code isn't functioning as expected, it also doesn't seem to be ...

The process of including a property in Vue.JS

Looking to include this property on my button: uk-toggle="target: #id" The desired outcome is: <button uk-toggle="target: #id" type="button">Click</button> I'm trying to achieve this with Vue.JS but I'm facing some difficulties. H ...

Struggles with jquery and fixing images

I am currently working on a project where users can click on a thumbnail image to display a larger version in the main image section. $('.alternative_images a').click(function(e){ var imgSrc = $(this).attr('href'); $('.mai ...

Is it necessary for the raycaster to be positioned within the render() function at all times?

Looking to capture the mouse double-click event's location and generate a 3D object in that spot within the scene. My understanding is that the raycaster, which is in the render() function, constantly updates the mouse location. I am interested in ha ...

Issue with Axios fetching data with parameter in Next.js not resolving

While working with Next.js, I encountered an issue where the input text value (email) is successfully displayed in the console, but when trying to use this value as a parameter, "{emails}" is being saved in the database instead of the dynamic email value. ...

Retrieve information from a controller and pass it to a Component in AngularJs

Having an issue with passing data from a controller to a component in AngularJs. The data is successfully passed to the template component, but it shows up as undefined in the controller! See below for my code snippets. The controller angular.module(&a ...

Performing a modulo operation within a v-for loop

I'm showcasing divs in a loop and I need to assign classes based on the loop index. My goal is to have index 0 and 1 with the class col-6, then indexes 2,3,4 with the class col-4, followed by index 5 and 6 having the class col-6, and so forth. This w ...

Validating object keys

I am dealing with an array of objects and I need to find a way to pass multiple keys in the function checkArray to validate these keys within each object. var test = [ { // Object details here... }, { // Another object details here... } ...

Error Checking in AngularJS Form Submission

According to my form.json file, I have a form that needs validation and a simulated submission. Firstly, I need to address this issue: fnPtr is not a function Next, I want to submit the form to a mocked API endpoint that will return true or false. Can I ...

Is JQuery the ultimate solution for creating a dynamic multi-language website?

Embarking on a new project that requires support for multiple languages. My plan is to create a jQuery/AJAX based application with all the code in jQuery, simply calling JSONs for data. What would be the most effective approach for implementing multi-lan ...

Discover the importance of Node.js integration with HTML

I am trying to display a Node-js value in an HTML file using the pug engine. In my app.js file: const express=require('express'); const app=express(); var bodyParser = require('body-parser'); app.set('views','views&apo ...

Encountering special symbols in the ID of a form element triggers an error message in jQuery validator, stating 'Unrecognized expression'

One of the challenges I am facing is that I have a form with elements that have ids containing special symbols. For example: The id="$FormData[1]$PersonData[1]$PhysicalPerson[1]$PersonName[1]$Affix[@type='qualification' and @position='prefi ...

Is there a way to dynamically set the active panel of a JQuery Accordion when making a call?

Currently, I am faced with a scenario where I need to implement a greybox popup window using jQuery Accordion from the main page via links. I am curious to know if it is doable to specify a default active panel for the accordion when calling it. Below is ...

Ways to substitute PHP functions using AJAX

I'm a beginner with AJAX. How do I replace the initial PHP function after an AJAX action is executed? It seems that the page does not refresh after the action takes place. Below is the code: Javascript function set_ddm(another_data) { var resul ...