Witam, mam taki kod:

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



no i "wywolanie"

  1. $(window).load(function() {
  2. var options =
  3. {
  4. thumbBox: '.thumbBox',
  5. spinner: '.spinner',
  6. imgSrc: 'avatar.png'
  7. }
  8. var cropper;
  9. $('#file').on('change', function(){
  10. var reader = new FileReader();
  11. reader.onload = function(e) {
  12. options.imgSrc = e.target.result;
  13. cropper = $('.imageBox').cropbox(options);
  14. }
  15. reader.readAsDataURL(this.files[0]);
  16. this.files = [];
  17. })
  18. $('#btnCrop').on('click', function(){
  19. var img = cropper.getDataURL()
  20. $('.cropped').html('<img src="'+img+'">');
  21. })
  22. $('#btnZoomIn').on('click', function(){
  23. cropper.zoomIn();
  24. })
  25. $('#btnZoomOut').on('click', function(){
  26. cropper.zoomOut();
  27. })
  28.  
  29. $('#generate').on('click', function(){
  30. html2canvas([document.getElementById('save')], {
  31. onrendered: function (canvas) {
  32. document.getElementById('save').appendChild(canvas);
  33. var data = canvas.toDataURL('image/png');
  34. // AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server
  35.  
  36. var image
  37.  
  38.  
  39. = new Image();
  40. image.src = data;
  41. document.getElementById('image').appendChild(image);
  42. }
  43. });
  44.  
  45. });
  46. });


Problem jest następujący - obecnie wycina zdjęcie w kwadracie, nawet jak dam dla .thumbox border radius - w ogóle nie bierze tego po uwagę, a chcę zrobić, żeby wycinało w kształcie koła.

Ma ktoś jakiś pomysł?

Dzięki z góry!

REF@