HTML5 Game - Scale and rotate a nonsquare object

Description

Scale and rotate a nonsquare object

Demo

ResultView the demo in separate window

<!DOCTYPE html>  
<html> 
<head> 
<style> 
#canvas { //  ww  w. j a va 2s .  c om
  border:1px solid #03F; 
  background:#CFC; 
} 
</style> 
</head> 
<body> 
<canvas id="canvas" width="640" height="480"></canvas> 
<script> 
  let context = document.getElementById('canvas').getContext('2d'); 
    
      //now draw a red rectangle 
      context.setTransform(1,0,0,1,0,0); 
      let angleInRadians = 90 * Math.PI / 180; 
      let x = 100; 
      let y = 100; 
      let width = 100; 
      let height = 50; 
      context.translate(x+.5*width, y+.5*height); 
      context.rotate(angleInRadians); 
      context.scale(2,2); 

      context.fillStyle = "red"; 
      context.fillRect(-.5*width,-.5*height , width, height); 

</script> 



</body> 
</html>

Related Topic