开发者

Draw dot on image using jQuery

开发者 https://www.devze.com 2023-01-15 06:47 出处:网络
I have the following code to track where a user clicks on an image: <im开发者_如何转开发g src=\"images/test.jpg\" id=\"testimg\" />

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

 <im开发者_如何转开发g 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?


<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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号