Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Canvas rotate, image does not fill the canvas

Aleksi

New Coder
I am implementing a photo editor, where the image can be zoomed in, rotated and cropped. I am having trouble rotating a rectangular image 90° to the right and to the left. When rotating, the image does not maintain its aspect ratio and does not fill the canvas.

The crop area is square shaped. Upon loading, the image looks like this:

1.png

When I rotate the image 90° to the right, it looks like this:

2.png

The image is now out of the cropped area bounds and the aspect ratio is off. I can't figure it out.
This is my code.

JavaScript:
function rotate (img) {
 
  var imageCropper    = document.querySelector ('.imageCropper');
 
  var cropArea        = imageCropper.querySelector ('.cropArea');
 
  var originalCanvas  = imageCropper.querySelector ('canvas.original');
  var resizedCanvas   = imageCropper.querySelector ('canvas.resized');
 
  var cropAreaRect    = cropArea.getBoundingClientRect();
 
  var resizedCtx      = resizedCanvas.getContext ('2d');
 
  rotationAngle       = img.getAttribute ('data-rotation-angle');


  if (Math.abs(rotationAngle) == 90 || Math.abs(rotationAngle == 270)) {
    
    resizedCanvas.width  = img.height;
    resizedCanvas.height = img.width;
    
  } else {
    
    resizedCanvas.width  = img.width;
    resizedCanvas.height = img.height;
  }
 
 
 
  if (img.matches('canvas')) { // canvas element as opposed to Image object
    
    // The source image is the resized canvas itself, and drawing it onto itself won't work.
    // Set the original canvas as source so that we can draw it on the resized canvas.
    
    img = originalCanvas;
  }

//######################################################### --- ROTATE
 
  resizedCtx.translate(resizedCanvas.width / 2, resizedCanvas.height / 2);
 
  resizedCtx.rotate(rotationAngle * Math.PI / 180);
 
  resizedCtx.translate(-resizedCanvas.width / 2, -resizedCanvas.height / 2);
    
  resizedCtx.drawImage (
    
    img,
    
    0, 0,
    img.width, img.height,
    
    0, 0,
    resizedCanvas.width, resizedCanvas.height
  ); 
}


if (node.matches ('.imageCropper button.rotateRight')) {
 
  node.addEventListener ('click', function () {
    
    var imageCropper  = node.closest ('.imageCropper');
    var cropArea      = imageCropper.querySelector ('.cropArea');
    var resizedCanvas = imageCropper.querySelector ('canvas.resized');
    
    // Get the rotation angle value in the data-rotation-angle attribute
    
    var rotationAngle = parseFloat(resizedCanvas.getAttribute ('data-rotation-angle'));
    
    // If a rotation angle value does not exist, it means the image is in the original position
    // and this will be the first rotation, so we set the rotationAngle as 0.
    
    if (! rotationAngle) {
      
      var rotationAngle = 0;
    }
    
    // We increment the rotation angle by 90 degrees, which will rotate the image in a clockwise direction
    
    rotationAngle += 90;
    
    // When the rotation angle reaches 270 and the next decrementation would take it to 360,
    // that means the image has done a full rotation and is back to its original position,
    // so we set it to 0.
    
    // We don't want values bigger than 360.
    
    if (rotationAngle > 270) {
      
      rotationAngle = 0;
    }
    
    // We set the data-rotation-angle with the new rotation angle value
    
    resizedCanvas.setAttribute('data-rotation-angle', rotationAngle);
    
    // Call the rotate() function
    
    rotate (resizedCanvas);
    
  }, false);
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom