I want to be able to paint or draw a circle/dot every time the user clicks inside a rectangle. It should be possible to add as many circles/dots as there are clicks and their position changes depending on the coordinates where the click was made. This is the code I’m using:
Circle:
<circle cy="50" cx="50" r="30" fill="#f"></circle>
Code:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<div onmousemove="showCoords(event)" onmouseout="clearCoor()"></div>
<p>Mouse over the rectangle above to get the horizontal and vertical coordinates of your mouse pointer.</p>
<p id="demo"></p>
<script>
function showCoords(event) {
var x = event.clientX;
var y = event.clientY;
var coor = "X coords: " + x + ", Y coords: " + y;
document.getElementById("demo").innerHTML = coor;
}
function clearCoor() {
document.getElementById("demo").innerHTML = "";
}
</script>
</body>
</html>
Code in action: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_event_mouse_clientxy2
What should I add in this code so it does what I want?