code_junkie

Howto avoid image cutoff with <canvas> rotate?

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>test</title>
  4. <script type="text/javascript">
  5. function startup() {
  6. var canvas = document.getElementById('canvas');
  7. var ctx = canvas.getContext('2d');
  8. var img = new Image();
  9. img.src = 'player.gif';
  10. img.onload = function() {
  11. ctx.rotate(5 * Math.PI / 180);
  12. ctx.drawImage(img, 0, 0, 64, 120);
  13. }
  14. }
  15. </script>
  16. </head>
  17. <body onload='startup();'>
  18. <canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
  19. </body>
  20. </html>
  21.  
  22. ctx.translate(85, 85);
  23. ctx.rotate(5 * Math.PI / 180);
  24.  
  25. ctx.translate(85, 85);
  26. ctx.rotate(5 * Math.PI / 180); // for 5 degrees like the example from Vincent
  27.  
  28. ctx.translate(-85, -85);
  29.  
  30. ctx.save();
  31. ctx.translate(85,85);
  32. ctx.rotate(5 * Math.PI / 180);
  33. ctx.fillRect(10,10,10,10);
  34. ctx.restore();
  35.  
  36. Square = {
  37. x:10,
  38. y:10,
  39. width:10,
  40. height:10,
  41. angle:5
  42. }
  43. ctx.save();
  44. ctx.translate(Square.x,Square.y);
  45. ctx.rotate(Square.angle * Math.PI / 180);
  46. ctx.fillRect(-Square.width/2,-Square.height/2,Square.width,Square.height);
  47. ctx.restore();
Add Comment
Please, Sign In to add comment