Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw dot on image using jQuery

I have the following code to track where a user clicks on an image:

 <img src="images/test.jpg" id="testimg" />

    <script type="text/javascript">
        $("#testimg").click(function (ev) {
            mouseX = ev.pageX;
            mouseY = ev.pageY
            alert(mouseX + ' ' + mouseY);
        })

    </script>

What I would like to do is, when the user clicks on the image, I want to draw a dot at the X,Y coordinates of the click.

Can someone give me some advice on how this can be done?

like image 952
Jason Evans Avatar asked Sep 09 '10 18:09

Jason Evans


1 Answers

<script type="text/javascript">
        $("#testimg").click(function (ev) {
        mouseX = ev.pageX;
        mouseY = ev.pageY
        //alert(mouseX + ' ' + mouseY);
        var color = '#000000';
        var size = '1px';
        $("body").append(
            $('<div></div>')
                .css('position', 'absolute')
                .css('top', mouseY + 'px')
                .css('left', mouseX + 'px')
                .css('width', size)
                .css('height', size)
                .css('background-color', color)
        );
    })
</script>

This will draw a black 1x1 pixel div.

like image 184
hacksteak25 Avatar answered Oct 06 '22 00:10

hacksteak25