Jump to content

Retrieving the coordinates of a dropped object.

Recommended Posts

<script type="text/javascript" src="jquery-2.0.3.min.js"></script><style>.canvas {    position:relative;    height:550px;    width:400px;    background:Yellow url("http://www.mapstop.co.uk/images/uploaded/lrg_wg2668.6a40d0d.jpg") no-repeat;}.canvas img {    position:absolute;}</style><div class="toolbar"><span>Drag...</span><img src="http://cdn4.iconfinder.com/data/icons/iconsland-weather/PNG/48x48/Sunny.png" alt="" title="" /><img src="http://cdn4.iconfinder.com/data/icons/iconsland-weather/PNG/48x48/Thermometer_Snowflake.png" alt="" title="" /><img src="http://cdn4.iconfinder.com/data/icons/iconsland-weather/PNG/48x48/Overcast.png" alt="" title="" /><img src="http://cdn4.iconfinder.com/data/icons/iconsland-weather/PNG/48x48/Night_Rain.png" alt="" title="" /></div><div class="canvas"></div><script type="text/javascript">$(document).ready(function () {    var $toolbar = $(".toolbar");    var $tools = $toolbar.find("img");    	//define drag and drop handlers    $toolbar.on("dragstart", "img", onDrag);    $(".canvas").on({        dragenter: false,        dragover: false,        drop: onDrop    });    //handle commencement of drag    function onDrag(e) {				console.log("onDrag");    }    //handle drop    function onDrop(e) {		console.log("OnDrop");		console.log(e.originalEvent.x);		console.log(e.originalEvent.y);		console.log(e);    }});</script>

What is clear to me is that if the drop event is fired then the toolbar image was dropped over the canvas. What is not clear to me at this time is what is the most cross browser way to retrieve the coordinates of the dropped image on the canvas so that I can place properly a clone of the dropped image.


Here is a link to my jsFiddle:


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...