cropbox.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. (function (factory) {
  3. if (typeof define === 'function' && define.amd) {
  4. define(['jquery'], factory);
  5. } else {
  6. factory(jQuery);
  7. }
  8. }(function ($) {
  9. var cropbox = function(options, el){
  10. var el = el || $(options.imageBox),
  11. obj =
  12. {
  13. state : {},
  14. ratio : 1,
  15. options : options,
  16. imageBox : el,
  17. thumbBox : el.find(options.thumbBox),
  18. spinner : el.find(options.spinner),
  19. image : new Image(),
  20. getDataURL: function ()
  21. {
  22. var width = this.thumbBox.width(),
  23. height = this.thumbBox.height(),
  24. canvas = document.createElement("canvas"),
  25. dim = el.css('background-position').split(' '),
  26. size = el.css('background-size').split(' '),
  27. dx = parseInt(dim[0]) - el.width()/2 + width/2,
  28. dy = parseInt(dim[1]) - el.height()/2 + height/2,
  29. dw = parseInt(size[0]),
  30. dh = parseInt(size[1]),
  31. sh = parseInt(this.image.height),
  32. sw = parseInt(this.image.width);
  33. canvas.width = width;
  34. canvas.height = height;
  35. var context = canvas.getContext("2d");
  36. context.drawImage(this.image, 0, 0, sw, sh, dx, dy, dw, dh);
  37. var imageData = canvas.toDataURL('image/png');
  38. return imageData;
  39. },
  40. getBlob: function()
  41. {
  42. var imageData = this.getDataURL();
  43. var b64 = imageData.replace('data:image/png;base64,','');
  44. var binary = atob(b64);
  45. var array = [];
  46. for (var i = 0; i < binary.length; i++) {
  47. array.push(binary.charCodeAt(i));
  48. }
  49. return new Blob([new Uint8Array(array)], {type: 'image/png'});
  50. },
  51. zoomIn: function ()
  52. {
  53. this.ratio*=1.1;
  54. setBackground();
  55. },
  56. zoomOut: function ()
  57. {
  58. this.ratio*=0.9;
  59. setBackground();
  60. }
  61. },
  62. setBackground = function()
  63. {
  64. var w = parseInt(obj.image.width)*obj.ratio;
  65. var h = parseInt(obj.image.height)*obj.ratio;
  66. var pw = (el.width() - w) / 2;
  67. var ph = (el.height() - h) / 2;
  68. el.css({
  69. 'background-image': 'url(' + obj.image.src + ')',
  70. 'background-size': w +'px ' + h + 'px',
  71. 'background-position': pw + 'px ' + ph + 'px',
  72. 'background-repeat': 'no-repeat'});
  73. },
  74. imgMouseDown = function(e)
  75. {
  76. e.stopImmediatePropagation();
  77. obj.state.dragable = true;
  78. obj.state.mouseX = e.clientX;
  79. obj.state.mouseY = e.clientY;
  80. },
  81. imgMouseMove = function(e)
  82. {
  83. e.stopImmediatePropagation();
  84. if (obj.state.dragable)
  85. {
  86. var x = e.clientX - obj.state.mouseX;
  87. var y = e.clientY - obj.state.mouseY;
  88. var bg = el.css('background-position').split(' ');
  89. var bgX = x + parseInt(bg[0]);
  90. var bgY = y + parseInt(bg[1]);
  91. el.css('background-position', bgX +'px ' + bgY + 'px');
  92. obj.state.mouseX = e.clientX;
  93. obj.state.mouseY = e.clientY;
  94. }
  95. },
  96. imgMouseUp = function(e)
  97. {
  98. e.stopImmediatePropagation();
  99. obj.state.dragable = false;
  100. },
  101. zoomImage = function(e)
  102. {
  103. e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0 ? obj.ratio*=1.1 : obj.ratio*=0.9;
  104. setBackground();
  105. }
  106. obj.spinner.show();
  107. obj.image.onload = function() {
  108. obj.spinner.hide();
  109. setBackground();
  110. el.bind('mousedown', imgMouseDown);
  111. el.bind('mousemove', imgMouseMove);
  112. $(window).bind('mouseup', imgMouseUp);
  113. el.bind('mousewheel DOMMouseScroll', zoomImage);
  114. };
  115. obj.image.crossOrigin = 'Anonymous';
  116. obj.image.src = options.imgSrc;
  117. el.on('remove', function(){$(window).unbind('mouseup', imgMouseUp)});
  118. return obj;
  119. };
  120. jQuery.fn.cropbox = function(options){
  121. return new cropbox(options, this);
  122. };
  123. }));