views:

67

answers:

2

Hi there.

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?

Cheers. Jas.

+1  A: 

Method 1.

You wil have to make use of the canvas tag. https://developer.mozilla.org/en/Canvas_tutorial

Method 2.

<div style="width:1px; height : 1px; position: fixed; top: mouseY; left:mouseX></div>

Works only if the page is not scrollable

Kasturi
+2  A: 
<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.

hacksteak25
+1 This worked nicely, thanks.
Jason Evans