Guest User

Untitled

a guest
Jan 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. //funçao para fazer um crop na imagem
  2. $(document).ready(function() {
  3. var condition = 1;
  4. var points = [];//holds the mousedown points
  5. var canvas = document.getElementById('myCanvas');
  6. this.isOldIE = (window.G_vmlCanvasManager);
  7. $(function() {
  8. // if (document.domain == 'localhost') {
  9.  
  10. if (this.isOldIE) {
  11. G_vmlCanvasManager.initElement(myCanvas);
  12. }
  13. var ctx = canvas.getContext('2d');
  14. var imageObj = new Image();
  15. function init() {
  16. canvas.addEventListener('mousedown', mouseDown, false);
  17. canvas.addEventListener('mouseup', mouseUp, false);
  18. canvas.addEventListener('mousemove', mouseMove, false);
  19. }
  20. // Draw image onto the canvas
  21. imageObj.onload = function() {
  22. ctx.drawImage(imageObj, 0, 0);
  23. };
  24. imageObj.src = "img.png";
  25.  
  26. // Switch the blending mode
  27. ctx.globalCompositeOperation = 'destination-over';
  28.  
  29. //mousemove event
  30. $('#myCanvas').mousedown(function(e) {
  31. if (condition == 1) {
  32.  
  33. ctx.beginPath();
  34.  
  35. $('#posx').html(e.offsetX);
  36. $('#posy').html(e.offsetY);
  37. }
  38. });
  39. //mousedown event
  40. $('#myCanvas').mousemove(function(e) {
  41. if (condition == 1) {
  42.  
  43. if (e.which == 1) {
  44. var pointer = $('<span class="spot">').css({
  45. 'position': 'absolute',
  46. 'background-color': '#000000',
  47. 'width': '5px',
  48. 'height': '5px',
  49. 'top': e.pageY,
  50. 'left': e.pageX
  51.  
  52.  
  53. });
  54. //store the points on mousedown
  55. points.push(e.pageX, e.pageY);
  56.  
  57. //console.log(points);
  58.  
  59. ctx.globalCompositeOperation = 'destination-out';
  60. var oldposx = $('#oldposx').html();
  61. var oldposy = $('#oldposy').html();
  62. var posx = $('#posx').html();
  63. var posy = $('#posy').html();
  64. ctx.beginPath();
  65. ctx.moveTo(oldposx, oldposy);
  66. if (oldposx != '') {
  67. ctx.lineTo(posx, posy);
  68.  
  69. ctx.stroke();
  70. }
  71. $('#oldposx').html(e.offsetX);
  72. $('#oldposy').html(e.offsetY);
  73. }
  74. $(document.body).append(pointer);
  75. $('#posx').html(e.offsetX);
  76. $('#posy').html(e.offsetY);
  77. }//condition
  78. });
  79.  
  80. $('#crop').click(function() {
  81. condition = 0;
  82.  
  83. // var pattern = ctx.createPattern(imageObj, "repeat");
  84. //ctx.fillStyle = pattern;
  85. $('.spot').each(function() {
  86. $(this).remove();
  87.  
  88. })
  89. //clear canvas
  90.  
  91. //var context = canvas.getContext("2d");
  92. //Delimita o tamanho da imagem isso já retira a base responsiva da imagem
  93. ctx.clearRect(0, 0, 600, 600);
  94. ctx.beginPath();
  95. ctx.width = 350;
  96. ctx.height = 350;
  97. ctx.globalCompositeOperation = 'destination-over';
  98. //draw the polygon
  99. setTimeout(function() {
  100.  
  101.  
  102. //console.log(points);
  103. var offset = $('#myCanvas').offset();
  104. //console.log(offset.left,offset.top);
  105.  
  106.  
  107. for (var i = 0; i < points.length; i += 2) {
  108. var x = parseInt(jQuery.trim(points[i]));
  109. var y = parseInt(jQuery.trim(points[i + 1]));
  110.  
  111.  
  112. if (i == 0) {
  113. ctx.moveTo(x - offset.left, y - offset.top);
  114. } else {
  115. ctx.lineTo(x - offset.left, y - offset.top);
  116. }
  117. //console.log(points[i],points[i+1])
  118. }
  119.  
  120. if (this.isOldIE) {
  121.  
  122. ctx.fillStyle = '';
  123. ctx.fill();
  124. var fill = $('fill', myCanvas).get(0);
  125. fill.color = '';
  126. fill.src = element.src;
  127. fill.type = 'tile';
  128. fill.alignShape = false;
  129. }
  130. else {
  131. var pattern = ctx.createPattern(imageObj, "repeat");
  132. ctx.fillStyle = pattern;
  133. ctx.fill();
  134.  
  135. var dataurl = canvas.toDataURL("image/png");
  136.  
  137.  
  138. //upload to server (if needed)
  139. var xhr = new XMLHttpRequest();
  140. // //
  141. xhr.open('POST', 'upload.php', false);
  142. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  143. var files = dataurl;
  144. var data = new FormData();
  145. var myprod = $("#pid").val();
  146. data = 'image=' + files;
  147. xhr.send(data);
  148. if (xhr.status === 200) {
  149. console.log(xhr.responseText);
  150. $('#myimg').html('<img src="upload/' + xhr.responseText + '.png"/>');
  151. }
  152.  
  153.  
  154.  
  155. }
  156. }, 20);
  157.  
  158. });
  159.  
  160. // }
  161. });
  162.  
  163. });
Add Comment
Please, Sign In to add comment