forked from jillix/svg.draggy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.draggable.min.js
1 lines (1 loc) · 2.6 KB
/
svg.draggable.min.js
1
(function(){SVG.extend(SVG.Element,{draggable:function(e){function s(e){if(!e||typeof e.transform!="function")return{x:1,y:1};var t=e.parent,n=e.transform();r={};var r=s(t);return{x:n.scaleX*r.x,y:n.scaleY*r.y}}var t,n,r,i=this;return typeof this.fixed=="function"&&this.fixed(),e=e||{},t=function(e){var t=this.parent._parent(SVG.Nested)||this._parent(SVG.Doc);e=e||window.event,i.beforedrag&&i.beforedrag(e);var s=i.bbox();i instanceof SVG.G?(s.x=i.x(),s.y=i.y()):i instanceof SVG.Nested&&(s={x:i.x(),y:i.y(),width:i.width(),height:i.height()}),i.startEvent=e,i.startPosition={x:s.x,y:s.y,width:s.width,height:s.height,zoom:t.viewbox().zoom,rotation:i.transform("rotation")*Math.PI/180},SVG.on(window,"mousemove",n),SVG.on(window,"touchmove",n),SVG.on(window,"mouseup",r),SVG.on(window,"touchend",r),i.node.dispatchEvent(new CustomEvent("dragstart",{detail:{event:e,delta:{x:0,y:0}}})),e.preventDefault?e.preventDefault():e.returnValue=!1},n=function(t){t=t||window.event;if(i.startEvent){var n,r,o=i.startPosition.rotation,u=i.startPosition.width,a=i.startPosition.height,f={x:t.pageX-i.startEvent.pageX,y:t.pageY-i.startEvent.pageY};/^touchstart|touchmove$/.test(t.type)?(f.x=t.touches[0].pageX-i.startEvent.touches[0].pageX,f.y=t.touches[0].pageY-i.startEvent.touches[0].pageY):/^click|mousedown|mousemove$/.test(t.type)&&(f.x=t.pageX-i.startEvent.pageX,f.y=t.pageY-i.startEvent.pageY),f.scale=s(i),n=i.startPosition.x+(f.x*Math.cos(o)+f.y*Math.sin(o))/Math.pow(f.scale.x,2),r=i.startPosition.y+(f.y*Math.cos(o)+f.x*Math.sin(-o))/Math.pow(f.scale.y,2);if(typeof e=="function"){var l=e(n,r);typeof l=="object"?typeof l.x!="boolean"||l.x?i.x(typeof l.x=="number"?l.x:n):(typeof l.y!="boolean"||l.y)&&i.y(typeof l.y=="number"?l.y:r):typeof l=="boolean"&&l&&i.move(n,r)}else typeof e=="object"&&(e.minX!==null&&n<e.minX?n=e.minX:e.maxX!==null&&n>e.maxX-u&&(n=e.maxX-u),e.minY!==null&&r<e.minY?r=e.minY:e.maxY!==null&&r>e.maxY-a&&(r=e.maxY-a),i.move(n,r));i.node.dispatchEvent(new CustomEvent("dragmove",{detail:{delta:f,event: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,"touchmove",n),SVG.off(window,"mouseup",r),SVG.off(window,"touchend",r),i.node.dispatchEvent(new CustomEvent("dragend",{detail:{delta:{x:0,y:0},event:e}}))},i.on("mousedown",t),i.on("touchstart",t),i.fixed=function(){return i.off("mousedown",t),i.off("touchstart",t),SVG.off(window,"mousemove",n),SVG.off(window,"touchmove",n),SVG.off(window,"mouseup",r),SVG.off(window,"touchend",r),t=n=r=null,i},this}})}).call(this);