skip to Main Content

I have a function that gets me the coordinates of the position of an image when you click on it. What I want to achieve is that when I click on the image draw a point in that same position as shown in the image:

enter image description here

this is my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img src="https://t1.up.ltmcdn.com/es/images/2/6/6/img_organos_internos_del_cuerpo_humano_lista_completa_1662_600.jpg" height="500" width="500" alt="dragon">


<script>

$(document).ready(function() {
    $("img").on("click", function(event) {
        var x = event.pageX - this.offsetLeft;
        var y = event.pageY - this.offsetTop;
        alert("X Coordinate: " + x + " Y Coordinate: " + y);
    });
});

</script>

2

Answers


  1. Just append an absolutely positioned element to the coordinates:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <img src="https://t1.up.ltmcdn.com/es/images/2/6/6/img_organos_internos_del_cuerpo_humano_lista_completa_1662_600.jpg" height="500" width="500" alt="dragon">
    
    
    <script>
    
    $(document).ready(function() {
        const size = 4;
        $("img").on("click", function(event) {
            var x = event.pageX - this.offsetLeft;
            var y = event.pageY - this.offsetTop;
            $(this).parent().append(`<div style="width: ${size}px; height: ${size}px; background: black; position: absolute; top: ${y + size}px; left: ${x + size}px; border-radius: ${size}px"/>`); 
        });
    });
    
    </script>
    Login or Signup to reply.
  2. I would use raw JavaScript. I don’t have time to fully explain right now, but may later. Good Luck!

    //<![CDATA[
    /* js/external.js */
    let get, post, doc, htm, bod, nav, M, I, mobile, S, Q, aC, rC, tC; // for use on other loads
    addEventListener('load', ()=>{
    get = (url, success, context)=>{
      const x = new XMLHttpRequest;
      const c = context || x;
      x.open('GET', url);
      x.onload = ()=>{
        if(success)success.call(c, JSON.parse(x.responseText));
      }
      x.send();
    }
    post = function(url, send, success, context){
      const x = new XMLHttpRequest;
      const c = context || x;
      x.open('POST', url);
      x.onload = ()=>{
        if(success)success.call(c, JSON.parse(x.responseText));
      }
      if(typeof send === 'object' && send && !(send instanceof Array)){
        if(send instanceof FormData){
          x.send(send);
        }
        else{
          const fd = new FormData;
          for(let k in send){
            fd.append(k, JSON.stringify(send[k]));
          }
          x.send(fd);
        }
      }
      else{
        throw new Error('send argument must be an Object');
      }
      return x;
    }
    doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);
    mobile = nav.userAgent.match(/Mobi/i) ? true : false;
    S = (selector, within)=>{
      var w = within || doc;
      return w.querySelector(selector);
    }
    Q = (selector, within)=>{
      var w = within || doc;
      return w.querySelectorAll(selector);
    }
    aC = function(){
      const a = [].slice.call(arguments), n = a.shift();
      n.classList.add(...a);
      return aC;
    }
    rC = function(){
      const a = [].slice.call(arguments), n = a.shift();
      n.classList.remove(...a);
      return rC;
    }
    tC = function(){
      const a = [].slice.call(arguments), n = a.shift();
      n.classList.toggle(...a);
      return tC;
    }
    // magic under here
    const stage = I('stage'), human = I('human'), dot = M('div'), dotStyle = dot.style;
    dot.id = 'dot';
    let move = false;
    function moveDot(e){
      if(move === true){
        let left = e.clientX, top = e.clientY;
        if(!stage.contains(dot))stage.appendChild(dot);
        dotStyle.left = left+'px'; dotStyle.top = top+'px';
      }
    }
    if(mobile){
      human.ontouchstart = (e)=>{
        move = true; moveDot(e);
      }
      human.ontouchmove = moveDot;
      human.ontouchend = ()=>{
        move = false;
      }
    }
    else{
      human.onmousedown = e=>{
        move = true; e.preventDefault(); moveDot(e);
        
      }
      human.onmousemove = moveDot;
      human.onmouseup = ()=>{
        move = false;
      }
    }
    }); // end load
    /* css/external.css */
    *{
      box-sizing:border-box;
    }
    html,body{
      width:100%; height:100%; padding:0; margin:0; background:#ccc;
    }
    .main,#stage{
      width:100%; height:100%;
    }
    #stage{
      position:relative; height:100%;
    }
    #stage>*{
      position:absolute;
    }
    #human{
      height:100%; margin:auto; top:0; right:0; bottom;0; left:0;
    }
    #dot{
      border:1px solid #009; border-radius:50%;
    }
    <!DOCTYPE html>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
      <head>
        <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />
        <title>Title Here</title>
        <link type='text/css' rel='stylesheet' href='css/external.css' />
        <script src='js/external.js'></script>
      </head>
    <body>
      <div class='main'>
        <div id='stage'>
          <img id='human' src='https://t1.up.ltmcdn.com/es/images/2/6/6/img_organos_internos_del_cuerpo_humano_lista_completa_1662_600.jpg' alt='dragon' />
        </div>
      </div>
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search