jcanvas.handle.min.js
5.24 KB
/**
* @license jCanvas Handles v15.09.25
* Copyright 2015 Caleb Evans
* Released under the MIT license
*/
!function($){function e(e){var n=e._method;return n===$.fn.drawRect||n===$.fn.drawEllipse||n===$.fn.drawImage}function n(e){var n=e._method;return n===$.fn.drawLine||n===$.fn.drawQuadratic||n===$.fn.drawBezier}function t(e,n,t,r){var a=$.extend({cursors:{mouseover:"grab",mousedown:"grabbing",mouseup:"grab"}},n.handle,{layer:!0,draggable:!0,x:n[t],y:n[r],_parent:n,_xProp:t,_yProp:r,fromCenter:!0,dragstart:function(e){$(this).triggerLayerEvent(e._parent,"handlestart")},drag:function(e){var n=e._parent;n[e._xProp]=e.x-n.x,n[e._yProp]=e.y-n.y,o(n),$(this).triggerLayerEvent(n,"handlemove")},dragstop:function(e){$(this).triggerLayerEvent(e._parent,"handlestop")},dragcancel:function(e){$(this).triggerLayerEvent(e._parent,"handlecancel")}});e.draw(a),n._handles.push(e.getLayer(-1))}function r(e,n,t,r){var a,i;-1===t&&-1===r||1===t&&1===r?i="nwse-resize":0!==t||-1!==r&&1!==r?-1!==t&&1!==t||0!==r?(1===t&&-1===r||-1===t&&1===r)&&(i="nesw-resize"):i="ew-resize":i="ns-resize",a=$.extend({cursors:{mouseover:i}},n.handle,{layer:!0,draggable:!0,x:n.x+(t*n.width/2+(n.fromCenter?0:n.width/2)),y:n.y+(r*n.height/2+(n.fromCenter?0:n.height/2)),_parent:n,_px:t,_py:r,fromCenter:!0,dragstart:function(e){$(this).triggerLayerEvent(e._parent,"handlestart")},drag:function(e){var n=e._parent;n.width+e.dx*e._px<n.minWidth&&(n.width=n.minWidth,e.dx=0),n.height+e.dy*e._py<n.minHeight&&(n.height=n.minHeight,e.dy=0),n.resizeFromCenter?(n.width+=e.dx*e._px*2,n.height+=e.dy*e._py*2,n.constrainProportions&&(0===e._py?n.height=n.width/n.aspectRatio:n.width=n.height*n.aspectRatio)):(n.fromCenter?(n.width+=e.dx*e._px,n.height+=e.dy*e._py):(n.width+=e.dx*e._px,0!==e._px&&(n.x+=e.dx*(1-e._px&&(1-e._px)/Math.abs(1-e._px))),n.height+=e.dy*e._py,0!==e._py&&(n.y+=e.dy*(1-e._py&&(1-e._py)/Math.abs(1-e._py)))),n.constrainProportions&&(0===e._py?n.height=n.width/n.aspectRatio:(n.width=n.height*n.aspectRatio,e.dx=e.dy*n.aspectRatio*e._py*e._px)),n.fromCenter&&(0!==e._px&&(n.x+=e.dx/2),0!==e._py&&(n.y+=e.dy/2))),l(n),$(this).triggerLayerEvent(n,"handlemove")},dragstop:function(e){var n=e._parent;$(this).triggerLayerEvent(n,"handlestop")},dragcancel:function(e){var n=e._parent;$(this).triggerLayerEvent(n,"handlecancel")}}),e.draw(a),n._handles.push(e.getLayer(-1))}function a(e,n){var t=n.handlePlacement;null===n.aspectRatio&&0!==n.height&&(n.aspectRatio=n.width/n.height),("corners"===t||"both"===t)&&(r(e,n,-1,-1),r(e,n,1,-1),r(e,n,1,1),r(e,n,-1,1)),("sides"===t||"both"===t)&&(r(e,n,0,-1),r(e,n,1,0),r(e,n,0,1),r(e,n,-1,0)),n.guide&&d(e,n)}function i(e){var n=e._guide;n&&(n.x=e.x,n.y=e.y,n.width=e.width,n.height=e.height,n.fromCenter=e.fromCenter)}function d(e,n){var t,r;t=$.extend({},n.guide,{layer:!0,draggable:!1,type:"rectangle",handle:null}),e.addLayer(t),r=e.getLayer(-1),n._guide=r,e.moveLayer(r,-n._handles.length-1),i(n)}function h(e,n){var r,a,i,d;for(r in n)n.hasOwnProperty(r)&&(a=n[r],null!==r.match(/c?x(\d+)/gi)&&(i=r,d=r.replace("x","y"),t(e,n,i,d)));n.guide&&g(e,n)}function o(e){var n=e._handles,t=e._guides,r,a,i,d;if(e._method===$.fn.drawQuadratic){if(n&&(i=e._guide))for(a=0;a<n.length;a+=1)r=e._handles[a],i["x"+(a+1)]=r.x,i["y"+(a+1)]=r.y}else if(e._method===$.fn.drawBezier&&t)for(d=0;d<t.length;d+=1)if(i=t[d],n=i._handles,i&&n)for(a=0;a<n.length;a+=1)r=n[a],i["x"+(a+1)]=r.x,i["y"+(a+1)]=r.y}function g(e,n){var t=n._handles,r,a,i,d,h,g,l;if(l=$.extend({},n.guide,{layer:!0,draggable:!1,type:"line"}),n._method===$.fn.drawQuadratic)e.addLayer(l),n._guide=e.getLayer(-1),e.moveLayer(n._guide,-t.length-1);else if(n._method===$.fn.drawBezier)for(n._guides=[],h=0;h<t.length;h+=1)d=t[h],a=t[h+1],r=t[h-1],i=null,void 0!==a?0===d._xProp.indexOf("x")&&0===a._xProp.indexOf("cx")&&(i=a):void 0!==r&&0===r._xProp.indexOf("cx")&&0===d._xProp.indexOf("x")&&(i=r),null!==i&&(e.addLayer(l),g=e.getLayer(-1),g._handles=[d,i],n._guides.push(g),e.moveLayer(g,-t.length-1));o(n)}function l(e){var n,t;if(e._handles)for(t=0;t<e._handles.length;t+=1)n=e._handles[t],n.x=e.x+(e.width/2*n._px+(e.fromCenter?0:e.width/2)),n.y=e.y+(e.height/2*n._py+(e.fromCenter?0:e.height/2));i(e)}function s(e){var n=e._handles,t,r;if(n)for(r=0;r<n.length;r+=1)t=n[r],t.x=e[t._xProp]+e.x,t.y=e[t._yProp]+e.y;o(e)}function _(t){var r=$(t.canvas);void 0===t._handles&&(t._handles=[]),e(t)?a(r,t):n(t)&&h(r,t)}function p(e){var n=$(e.canvas),t,r;if(e._handles){for(r=0;r<e._handles.length;r+=1)t=e._handles[r],n.removeLayer(t);e._handles.length=0}}function y(e){var n;for(n in e)if(e.hasOwnProperty(n)&&n.match(/^(x|y)\d+$/))return!0;return!1}$.extend($.jCanvas.defaults,{handle:null,guide:null,aspectRatio:null,resizeFromCenter:!0,constrainProportions:!1,handlePlacement:"corners",minWidth:0,minHeight:0}),$.extend($.jCanvas.eventHooks,{add:function(e){e.handle&&_(e)},remove:function(e){var n,t,r;if(e._handles){for(n=$(this),r=0;r<e._handles.length;r+=1)t=e._handles[r],n.removeLayer(t);e._handles.length=0}},change:function(t,r){r.handle||y(r)?(p(t),_(t)):null===r.handle&&p(t),e(t)?(void 0!==r.width||void 0!==r.height||void 0!==r.x||void 0!==r.y)&&l(t):n(t)&&s(t)},animate:function(t,r){e(t)?("width"===r.prop||"height"===r.prop||"x"===r.prop||"y"===r.prop)&&l(t):n(t)&&null!==r.prop.match(/^c?(x|y)(\d+)/gi)&&s(t)},drag:function(t){e(t)?l(t):n(t)&&s(t)}})}(jQuery);