welCropperUtil.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // 获取选中区域的(x, y, w, h)
  2. const getCropRect = (cropperMovableItems) => {
  3. let maxX = 0, maxY = 0
  4. for (let key in cropperMovableItems) {
  5. let item = cropperMovableItems[key]
  6. maxX = Math.max(item.x, maxX)
  7. maxY = Math.max(item.y, maxY)
  8. }
  9. let minX = maxX, minY = maxY
  10. for (let key in cropperMovableItems) {
  11. let item = cropperMovableItems[key]
  12. minX = Math.min(item.x, minX)
  13. minY = Math.min(item.y, minY)
  14. }
  15. return {
  16. x: minX,
  17. y: minY,
  18. w: maxX - minX,
  19. h: maxY - minY
  20. }
  21. }
  22. // 获取适应屏幕的图片显示大小
  23. const getAdjustSize = (W, H, width, height) => {
  24. if (width > W) {
  25. height = W / width * height
  26. width = W
  27. }
  28. if (height > H) {
  29. width = H / height * width
  30. height = H
  31. }
  32. return {
  33. width: width,
  34. height: height
  35. }
  36. }
  37. // http://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/
  38. // To find orientation of ordered triplet (p, q, r).
  39. // The function returns following values
  40. // 0 --> p, q and r are colinear
  41. // 1 --> Clockwise
  42. // 2 --> Counterclockwise
  43. function orientation(p, q, r) {
  44. var val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  45. if (val == 0) return 0; // collinear
  46. return (val > 0) ? 1 : 2; // clock or counterclock wise
  47. }
  48. // Prints convex hull of a set of n points.
  49. function convexHull(points, n) {
  50. // There must be at least 3 points
  51. if (n < 3) return;
  52. // Initialize Result
  53. var hull = [];
  54. // Find the leftmost point
  55. var l = 0;
  56. for (var i = 1; i < n; i++) {
  57. if (points[i].x < points[l].x) {
  58. l = i;
  59. }
  60. }
  61. // Start from leftmost point, keep moving
  62. // counterclockwise until reach the start point
  63. // again. This loop runs O(h) times where h is
  64. // number of points in result or output.
  65. var p = l, q;
  66. do {
  67. // Add current point to result
  68. // Prevent duplicates object
  69. // if (hull.findIndex(i => i.x == points[p].x && i.y == points[p].y)==-1){
  70. hull.push(points[p]);
  71. // }
  72. // Search for a point 'q' such that
  73. // orientation(p, x, q) is counterclockwise
  74. // for all points 'x'. The idea is to keep
  75. // track of last visited most counterclock-
  76. // wise point in q. If any point 'i' is more
  77. // counterclock-wise than q, then update q.
  78. q = (p + 1) % n;
  79. for (var i = 0; i < n; i++) {
  80. // If i is more counterclockwise than
  81. // current q, then update q
  82. if (orientation(points[p], points[i], points[q]) == 2)
  83. q = i;
  84. }
  85. // Now q is the most counterclockwise with
  86. // respect to p. Set p as q for next iteration,
  87. // so that q is added to result 'hull'
  88. p = q;
  89. } while (p != l); // While we don't come to first
  90. // point
  91. // Print Result
  92. // for (var i in hull) {
  93. // var temp = hull[i]
  94. // console.log("(" + temp.x + ", " + temp.y + ")");
  95. // }
  96. return hull
  97. }
  98. function drawImageWithDegree(ctx, path, width, height, degree) {
  99. let isVertical = degree % 180 > 0
  100. let drawWidth = isVertical ? height : width
  101. let drawHeight = isVertical ? width : height
  102. let centerX = width / 2
  103. let cneterY = height / 2
  104. let drawCenterX = drawWidth / 2
  105. let drawCneterY = drawHeight / 2
  106. let d = Math.abs(width - height) / 2
  107. // ctx.translate(drawCenterX, drawCneterY)
  108. // ctx.rotate(degree * Math.PI / 180)
  109. // ctx.translate(-drawCenterX, -drawCneterY)
  110. ctx.translate(centerX, cneterY)
  111. ctx.rotate(degree * Math.PI / 180)
  112. ctx.translate(-centerX, -cneterY)
  113. // ctx.translate(-d, d)
  114. if (isVertical) {
  115. if (drawHeight > drawWidth) {
  116. ctx.drawImage(path, d, -d, drawWidth, drawHeight)
  117. }
  118. else {
  119. ctx.drawImage(path, -d, d, drawWidth, drawHeight)
  120. }
  121. }
  122. else {
  123. ctx.drawImage(path, 0, 0, drawWidth, drawHeight)
  124. }
  125. ctx.draw(false, function (e) {
  126. console.log('draw callback')
  127. })
  128. }
  129. // 查找topleft的点
  130. function findTopLeft(items) {
  131. let x = items.topleft.x, y = items.topleft.y
  132. for (let i in items) {
  133. let item = items[i]
  134. if (x > item.x) {
  135. x = item.x
  136. }
  137. if (y > item.y) {
  138. y = item.y
  139. }
  140. }
  141. return {
  142. x, y
  143. }
  144. }
  145. module.exports = {
  146. getCropRect,
  147. getAdjustSize,
  148. convexHull,
  149. drawImageWithDegree,
  150. findTopLeft
  151. }