1

Does anyone know how to add a point where the mouse position is clicked? Using the Typescript (or) JavaScript code.

I think for some how I know the solution but I don't know how to implement it. The thing that I know is how to get the X co-ordinates and Y co-ordinates at that particular point and the thing that I don't know is how to implement that.

    function getClickPosition(e) {
     var xPosition = e.clientX;
     var yPosition = e.clientY;
    }
curveball
  • 4,320
  • 15
  • 39
  • 49
Naveen Kumar
  • 33
  • 1
  • 5

1 Answers1

2

A quick approach is demonstrated by the snippet

function getClickPosition(e) {
  var p = {
    x: e.clientX,
    y: e.clientY
  }
  drawAt(p);
  return p
}

function drawAt(point) {
  var dotSize = 10; // in px
  var div = document.createElement('div');
  div.style.backgroundColor = "#000"
  div.style.width = dotSize + "px";
  div.style.height = dotSize + "px"
  div.style.position = "absolute"
  div.style.left = (point.x - dotSize / 2) + "px"
  div.style.top = (point.y - dotSize / 2) + "px"
  div.style.borderRadius = "50%"
  root.appendChild(div);
}

document.getElementById("root").addEventListener('click', function(e) {
  getClickPosition(e);
})
<div id="root" style="width: 400px; height: 300px; background-color: #ccc"></div>

however, you might wanna look into the HTML canvas

marsze
  • 15,079
  • 5
  • 45
  • 61
andylamax
  • 1,858
  • 1
  • 14
  • 31