forked from svgdotjs/svg.draggable.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.draggable.min.js
2 lines (2 loc) · 1.8 KB
/
svg.draggable.min.js
1
2
// svg.draggable.js 0.12 - Copyright (c) 2013 Wout Fierens - Licensed under the MIT license
SVG.extend(SVG.Element,{draggable:function(e){var t,n,r,i=this,s=this.parent._parent(SVG.Nested)||this._parent(SVG.Doc);if(typeof this.fixed=="function")this.fixed();e=e||{};t=function(e){e=e||window.event;var t;if(i.beforedrag)i.beforedrag(e);t=i.bbox();if(i instanceof SVG.G){t.x=i.trans.x;t.y=i.trans.y}else if(i instanceof SVG.Nested){t={x:i.x(),y:i.y(),width:i.attr("width"),height:i.attr("height")}}i.startEvent=e;i.startPosition={x:t.x,y:t.y,width:t.width,height:t.height,zoom:s.viewbox().zoom,rotation:i.transform("rotation")*Math.PI/180};SVG.on(window,"mousemove",n);SVG.on(window,"mouseup",r);if(i.dragstart)i.dragstart({x:0,y:0,zoom:i.startPosition.zoom},e);e.preventDefault?e.preventDefault():e.returnValue=false};n=function(t){t=t||window.event;if(i.startEvent){var n,r,s=i.startPosition.rotation,o=i.startPosition.width,u=i.startPosition.height,a={x:t.pageX-i.startEvent.pageX,y:t.pageY-i.startEvent.pageY,zoom:i.startPosition.zoom};n=i.startPosition.x+(a.x*Math.cos(s)+a.y*Math.sin(s))/i.startPosition.zoom;r=i.startPosition.y+(a.y*Math.cos(s)+a.x*Math.sin(-s))/i.startPosition.zoom;if(i._offset){n-=i._offset.x;r-=i._offset.y}if(e.minX!=null&&n<e.minX)n=e.minX;else if(e.maxX!=null&&n>e.maxX-o)n=e.maxX-o;if(e.minY!=null&&r<e.minY)r=e.minY;else if(e.maxY!=null&&r>e.maxY-u)r=e.maxY-u;i.move(n,r);if(i.dragmove)i.dragmove(a,t)}};r=function(e){e=e||window.event;var t={x:e.pageX-i.startEvent.pageX,y:e.pageY-i.startEvent.pageY,zoom:i.startPosition.zoom};i.startEvent=null;i.startPosition=null;SVG.off(window,"mousemove",n);SVG.off(window,"mouseup",r);if(i.dragend)i.dragend(t,e)};i.on("mousedown",t);i.fixed=function(){i.off("mousedown",t);SVG.off(window,"mousemove",n);SVG.off(window,"mouseup",r);t=n=r=null;return i};return this}})