crop-canvas.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. crop.factory('cropCanvas', [function() {
  3. // Shape = Array of [x,y]; [0, 0] - center
  4. var shapeArrowNW=[[-0.5,-2],[-3,-4.5],[-0.5,-7],[-7,-7],[-7,-0.5],[-4.5,-3],[-2,-0.5]];
  5. var shapeArrowNE=[[0.5,-2],[3,-4.5],[0.5,-7],[7,-7],[7,-0.5],[4.5,-3],[2,-0.5]];
  6. var shapeArrowSW=[[-0.5,2],[-3,4.5],[-0.5,7],[-7,7],[-7,0.5],[-4.5,3],[-2,0.5]];
  7. var shapeArrowSE=[[0.5,2],[3,4.5],[0.5,7],[7,7],[7,0.5],[4.5,3],[2,0.5]];
  8. var shapeArrowN=[[-1.5,-2.5],[-1.5,-6],[-5,-6],[0,-11],[5,-6],[1.5,-6],[1.5,-2.5]];
  9. var shapeArrowW=[[-2.5,-1.5],[-6,-1.5],[-6,-5],[-11,0],[-6,5],[-6,1.5],[-2.5,1.5]];
  10. var shapeArrowS=[[-1.5,2.5],[-1.5,6],[-5,6],[0,11],[5,6],[1.5,6],[1.5,2.5]];
  11. var shapeArrowE=[[2.5,-1.5],[6,-1.5],[6,-5],[11,0],[6,5],[6,1.5],[2.5,1.5]];
  12. // Colors
  13. var colors={
  14. areaOutline: '#fff',
  15. resizeBoxStroke: '#fff',
  16. resizeBoxFill: '#444',
  17. resizeBoxArrowFill: '#fff',
  18. resizeCircleStroke: '#fff',
  19. resizeCircleFill: '#444',
  20. moveIconFill: '#fff'
  21. };
  22. return function(ctx){
  23. /* Base functions */
  24. // Calculate Point
  25. var calcPoint=function(point,offset,scale) {
  26. return [scale*point[0]+offset[0], scale*point[1]+offset[1]];
  27. };
  28. // Draw Filled Polygon
  29. var drawFilledPolygon=function(shape,fillStyle,centerCoords,scale) {
  30. ctx.save();
  31. ctx.fillStyle = fillStyle;
  32. ctx.beginPath();
  33. var pc, pc0=calcPoint(shape[0],centerCoords,scale);
  34. ctx.moveTo(pc0[0],pc0[1]);
  35. for(var p in shape) {
  36. if (p > 0) {
  37. pc=calcPoint(shape[p],centerCoords,scale);
  38. ctx.lineTo(pc[0],pc[1]);
  39. }
  40. }
  41. ctx.lineTo(pc0[0],pc0[1]);
  42. ctx.fill();
  43. ctx.closePath();
  44. ctx.restore();
  45. };
  46. /* Icons */
  47. this.drawIconMove=function(centerCoords, scale) {
  48. drawFilledPolygon(shapeArrowN, colors.moveIconFill, centerCoords, scale);
  49. drawFilledPolygon(shapeArrowW, colors.moveIconFill, centerCoords, scale);
  50. drawFilledPolygon(shapeArrowS, colors.moveIconFill, centerCoords, scale);
  51. drawFilledPolygon(shapeArrowE, colors.moveIconFill, centerCoords, scale);
  52. };
  53. this.drawIconResizeCircle=function(centerCoords, circleRadius, scale) {
  54. var scaledCircleRadius=circleRadius*scale;
  55. ctx.save();
  56. ctx.strokeStyle = colors.resizeCircleStroke;
  57. ctx.lineWidth = 2;
  58. ctx.fillStyle = colors.resizeCircleFill;
  59. ctx.beginPath();
  60. ctx.arc(centerCoords[0],centerCoords[1],scaledCircleRadius,0,2*Math.PI);
  61. ctx.fill();
  62. ctx.stroke();
  63. ctx.closePath();
  64. ctx.restore();
  65. };
  66. this.drawIconResizeBoxBase=function(centerCoords, boxSize, scale) {
  67. var scaledBoxSize=boxSize*scale;
  68. ctx.save();
  69. ctx.strokeStyle = colors.resizeBoxStroke;
  70. ctx.lineWidth = 2;
  71. ctx.fillStyle = colors.resizeBoxFill;
  72. ctx.fillRect(centerCoords[0] - scaledBoxSize/2, centerCoords[1] - scaledBoxSize/2, scaledBoxSize, scaledBoxSize);
  73. ctx.strokeRect(centerCoords[0] - scaledBoxSize/2, centerCoords[1] - scaledBoxSize/2, scaledBoxSize, scaledBoxSize);
  74. ctx.restore();
  75. };
  76. this.drawIconResizeBoxNESW=function(centerCoords, boxSize, scale) {
  77. this.drawIconResizeBoxBase(centerCoords, boxSize, scale);
  78. drawFilledPolygon(shapeArrowNE, colors.resizeBoxArrowFill, centerCoords, scale);
  79. drawFilledPolygon(shapeArrowSW, colors.resizeBoxArrowFill, centerCoords, scale);
  80. };
  81. this.drawIconResizeBoxNWSE=function(centerCoords, boxSize, scale) {
  82. this.drawIconResizeBoxBase(centerCoords, boxSize, scale);
  83. drawFilledPolygon(shapeArrowNW, colors.resizeBoxArrowFill, centerCoords, scale);
  84. drawFilledPolygon(shapeArrowSE, colors.resizeBoxArrowFill, centerCoords, scale);
  85. };
  86. /* Crop Area */
  87. this.drawCropArea=function(image, centerCoords, size, fnDrawClipPath) {
  88. var xRatio=image.width/ctx.canvas.width,
  89. yRatio=image.height/ctx.canvas.height,
  90. xLeft=centerCoords[0]-size/2,
  91. yTop=centerCoords[1]-size/2;
  92. ctx.save();
  93. ctx.strokeStyle = colors.areaOutline;
  94. ctx.lineWidth = 2;
  95. ctx.beginPath();
  96. fnDrawClipPath(ctx, centerCoords, size);
  97. ctx.stroke();
  98. ctx.clip();
  99. // draw part of original image
  100. if (size > 0) {
  101. ctx.drawImage(image, xLeft*xRatio, yTop*yRatio, size*xRatio, size*yRatio, xLeft, yTop, size, size);
  102. }
  103. ctx.beginPath();
  104. fnDrawClipPath(ctx, centerCoords, size);
  105. ctx.stroke();
  106. ctx.clip();
  107. ctx.restore();
  108. };
  109. };
  110. }]);