I am trying to achieve this effect with jQuery UI – very much like the way you crop an image on Facebook:
http://blog.creonfx.com/examples/javascript/facebook-cropping-mootools.html
Here is a very simple test case in HTML (an img within a div):
<div>
<img src="fat_cat.jpg">
</div>
and here is the logic that seems fit for the purpose – however it is unfinished:
$("img").draggable({ drag: dragHandler });
function dragHandler(event, ui) {
var x = event.target.x - event.target.parentNode.offsetLeft;
var y = event.target.offsetTop;
if(x > 0) {
// How to constrain the movement here?
}
if(x < -(event.target.offsetWidth -
event.target.parentNode.offsetWidth)) {
}
if(y > 0) {
}
if(y < -(event.target.offsetHeight -
event.target.parentNode.offsetHeight)) {
}
$("p").text(x + ", " + y);
}
My first attempts were to modify the offsetLeft & offsetTop variables, in all their multiple access points, but nothing seems to be working for me.
Here is a jsFiddle with what is explained above: http://jsfiddle.net/g105b/FdkBK/
You can actually do this with an inner container whose width/height is calculated to only allow the image to travel a certain distance. Of course you also have to position the inner container appropriately.
Here is my go at it:
Markup:
Script: