crop-area-square.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. crop.factory('cropAreaSquare', ['cropArea', function(CropArea) {
  3. var CropAreaSquare = function() {
  4. CropArea.apply(this, arguments);
  5. this._resizeCtrlBaseRadius = 10;
  6. this._resizeCtrlNormalRatio = 0.75;
  7. this._resizeCtrlHoverRatio = 1;
  8. this._iconMoveNormalRatio = 0.9;
  9. this._iconMoveHoverRatio = 1.2;
  10. this._resizeCtrlNormalRadius = this._resizeCtrlBaseRadius*this._resizeCtrlNormalRatio;
  11. this._resizeCtrlHoverRadius = this._resizeCtrlBaseRadius*this._resizeCtrlHoverRatio;
  12. this._posDragStartX=0;
  13. this._posDragStartY=0;
  14. this._posResizeStartX=0;
  15. this._posResizeStartY=0;
  16. this._posResizeStartSize=0;
  17. this._resizeCtrlIsHover = -1;
  18. this._areaIsHover = false;
  19. this._resizeCtrlIsDragging = -1;
  20. this._areaIsDragging = false;
  21. };
  22. CropAreaSquare.prototype = new CropArea();
  23. CropAreaSquare.prototype._calcSquareCorners=function() {
  24. var hSize=this._size/2;
  25. return [
  26. [this._x-hSize, this._y-hSize],
  27. [this._x+hSize, this._y-hSize],
  28. [this._x-hSize, this._y+hSize],
  29. [this._x+hSize, this._y+hSize]
  30. ];
  31. };
  32. CropAreaSquare.prototype._calcSquareDimensions=function() {
  33. var hSize=this._size/2;
  34. return {
  35. left: this._x-hSize,
  36. top: this._y-hSize,
  37. right: this._x+hSize,
  38. bottom: this._y+hSize
  39. };
  40. };
  41. CropAreaSquare.prototype._isCoordWithinArea=function(coord) {
  42. var squareDimensions=this._calcSquareDimensions();
  43. return (coord[0]>=squareDimensions.left&&coord[0]<=squareDimensions.right&&coord[1]>=squareDimensions.top&&coord[1]<=squareDimensions.bottom);
  44. };
  45. CropAreaSquare.prototype._isCoordWithinResizeCtrl=function(coord) {
  46. var resizeIconsCenterCoords=this._calcSquareCorners();
  47. var res=-1;
  48. for(var i=0,len=resizeIconsCenterCoords.length;i<len;i++) {
  49. var resizeIconCenterCoords=resizeIconsCenterCoords[i];
  50. if(coord[0] > resizeIconCenterCoords[0] - this._resizeCtrlHoverRadius && coord[0] < resizeIconCenterCoords[0] + this._resizeCtrlHoverRadius &&
  51. coord[1] > resizeIconCenterCoords[1] - this._resizeCtrlHoverRadius && coord[1] < resizeIconCenterCoords[1] + this._resizeCtrlHoverRadius) {
  52. res=i;
  53. break;
  54. }
  55. }
  56. return res;
  57. };
  58. CropAreaSquare.prototype._drawArea=function(ctx,centerCoords,size){
  59. var hSize=size/2;
  60. ctx.rect(centerCoords[0]-hSize,centerCoords[1]-hSize,size,size);
  61. };
  62. CropAreaSquare.prototype.draw=function() {
  63. CropArea.prototype.draw.apply(this, arguments);
  64. // draw move icon
  65. this._cropCanvas.drawIconMove([this._x,this._y], this._areaIsHover?this._iconMoveHoverRatio:this._iconMoveNormalRatio);
  66. // draw resize cubes
  67. var resizeIconsCenterCoords=this._calcSquareCorners();
  68. for(var i=0,len=resizeIconsCenterCoords.length;i<len;i++) {
  69. var resizeIconCenterCoords=resizeIconsCenterCoords[i];
  70. this._cropCanvas.drawIconResizeCircle(resizeIconCenterCoords, this._resizeCtrlBaseRadius, this._resizeCtrlIsHover===i?this._resizeCtrlHoverRatio:this._resizeCtrlNormalRatio);
  71. }
  72. };
  73. CropAreaSquare.prototype.processMouseMove=function(mouseCurX, mouseCurY) {
  74. var cursor='default';
  75. var res=false;
  76. this._resizeCtrlIsHover = -1;
  77. this._areaIsHover = false;
  78. if (this._areaIsDragging) {
  79. this._x = mouseCurX - this._posDragStartX;
  80. this._y = mouseCurY - this._posDragStartY;
  81. this._areaIsHover = true;
  82. cursor='move';
  83. res=true;
  84. this._events.trigger('area-move');
  85. } else if (this._resizeCtrlIsDragging>-1) {
  86. var xMulti, yMulti;
  87. switch(this._resizeCtrlIsDragging) {
  88. case 0: // Top Left
  89. xMulti=-1;
  90. yMulti=-1;
  91. cursor = 'nwse-resize';
  92. break;
  93. case 1: // Top Right
  94. xMulti=1;
  95. yMulti=-1;
  96. cursor = 'nesw-resize';
  97. break;
  98. case 2: // Bottom Left
  99. xMulti=-1;
  100. yMulti=1;
  101. cursor = 'nesw-resize';
  102. break;
  103. case 3: // Bottom Right
  104. xMulti=1;
  105. yMulti=1;
  106. cursor = 'nwse-resize';
  107. break;
  108. }
  109. var iFX = (mouseCurX - this._posResizeStartX)*xMulti;
  110. var iFY = (mouseCurY - this._posResizeStartY)*yMulti;
  111. var iFR;
  112. if(iFX>iFY) {
  113. iFR = this._posResizeStartSize + iFY;
  114. } else {
  115. iFR = this._posResizeStartSize + iFX;
  116. }
  117. var wasSize=this._size;
  118. this._size = Math.max(this._minSize, iFR);
  119. var posModifier=(this._size-wasSize)/2;
  120. this._x+=posModifier*xMulti;
  121. this._y+=posModifier*yMulti;
  122. this._resizeCtrlIsHover = this._resizeCtrlIsDragging;
  123. res=true;
  124. this._events.trigger('area-resize');
  125. } else {
  126. var hoveredResizeBox=this._isCoordWithinResizeCtrl([mouseCurX,mouseCurY]);
  127. if (hoveredResizeBox>-1) {
  128. switch(hoveredResizeBox) {
  129. case 0:
  130. cursor = 'nwse-resize';
  131. break;
  132. case 1:
  133. cursor = 'nesw-resize';
  134. break;
  135. case 2:
  136. cursor = 'nesw-resize';
  137. break;
  138. case 3:
  139. cursor = 'nwse-resize';
  140. break;
  141. }
  142. this._areaIsHover = false;
  143. this._resizeCtrlIsHover = hoveredResizeBox;
  144. res=true;
  145. } else if(this._isCoordWithinArea([mouseCurX,mouseCurY])) {
  146. cursor = 'move';
  147. this._areaIsHover = true;
  148. res=true;
  149. }
  150. }
  151. this._dontDragOutside();
  152. angular.element(this._ctx.canvas).css({'cursor': cursor});
  153. return res;
  154. };
  155. CropAreaSquare.prototype.processMouseDown=function(mouseDownX, mouseDownY) {
  156. var isWithinResizeCtrl=this._isCoordWithinResizeCtrl([mouseDownX,mouseDownY]);
  157. if (isWithinResizeCtrl>-1) {
  158. this._areaIsDragging = false;
  159. this._areaIsHover = false;
  160. this._resizeCtrlIsDragging = isWithinResizeCtrl;
  161. this._resizeCtrlIsHover = isWithinResizeCtrl;
  162. this._posResizeStartX=mouseDownX;
  163. this._posResizeStartY=mouseDownY;
  164. this._posResizeStartSize = this._size;
  165. this._events.trigger('area-resize-start');
  166. } else if (this._isCoordWithinArea([mouseDownX,mouseDownY])) {
  167. this._areaIsDragging = true;
  168. this._areaIsHover = true;
  169. this._resizeCtrlIsDragging = -1;
  170. this._resizeCtrlIsHover = -1;
  171. this._posDragStartX = mouseDownX - this._x;
  172. this._posDragStartY = mouseDownY - this._y;
  173. this._events.trigger('area-move-start');
  174. }
  175. };
  176. CropAreaSquare.prototype.processMouseUp=function(/*mouseUpX, mouseUpY*/) {
  177. if(this._areaIsDragging) {
  178. this._areaIsDragging = false;
  179. this._events.trigger('area-move-end');
  180. }
  181. if(this._resizeCtrlIsDragging>-1) {
  182. this._resizeCtrlIsDragging = -1;
  183. this._events.trigger('area-resize-end');
  184. }
  185. this._areaIsHover = false;
  186. this._resizeCtrlIsHover = -1;
  187. this._posDragStartX = 0;
  188. this._posDragStartY = 0;
  189. };
  190. return CropAreaSquare;
  191. }]);