Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. this.angle=0; //added it in the constructor
  2.  
  3. this.rotated = false;
  4.  
  5. Caman.Plugin.register("rotate", function(degrees) {
  6. //....
  7. //....
  8. //....
  9. this.angle += degrees;
  10. this.rotated = true;
  11. return this.replaceCanvas(canvas);
  12. }
  13.  
  14. else if (this.rotated){
  15. canvas = document.createElement('canvas');//Canvas for initial state
  16. canvas.width = this.originalWidth; //give it the original width
  17. canvas.height = this.originalHeight; //and original height
  18. ctx = canvas.getContext('2d');
  19. imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  20. pixelData = imageData.data;//get the pixelData (length equal to those of initial canvas
  21. _ref = this.originalPixelData; //use it as a reference array
  22. for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
  23. pixel = _ref[i];
  24. pixelData[i] = pixel; //give pixelData the initial pixels
  25. }
  26. ctx.putImageData(imageData, 0, 0); //put it back on our canvas
  27. rotatedCanvas = document.createElement('canvas'); //canvas to rotate from initial
  28. rotatedCtx = rotatedCanvas.getContext('2d');
  29. rotatedCanvas.width = this.canvas.width;//Our canvas was already rotated so it has been replaced. Caman's canvas attribute is allready rotated, So use that width
  30. rotatedCanvas.height = this.canvas.height; //the same
  31. x = rotatedCanvas.width / 2; //for translating
  32. y = rotatedCanvas.width / 2; //same
  33. rotatedCtx.save();
  34. rotatedCtx.translate(x, y);
  35. rotatedCtx.rotate(this.angle * Math.PI / 180); //rotation based on the total angle
  36. rotatedCtx.drawImage(canvas, -canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height); //put the image back on canvas
  37. rotatedCtx.restore(); //restore it
  38. pixelData = rotatedCtx.getImageData(0, 0, rotatedCanvas.width, rotatedCanvas.height).data; //get the pixelData back
  39. width = rotatedCanvas.width; //used for returning the pixels in revert function
  40. }
  41.  
  42. Caman.prototype.reset = function() {
  43. //....
  44. //....
  45. this.angle = 0;
  46. this.rotated = false;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement