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