Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How can I rotate the canvas image?

I tried to load the image by url on the canvas. And I change the canvas size to image size. andf I tried to rotate the image but if I executed it, some part of the image is cropped. Please find the errors in my code.

if (submitBtn) {
  submitBtn.addEventListener('click', handleSubmitBtnClick);
}

function handleSubmitBtnClick() {
  let imgURLValue = inputImageURL.value;
  backgroundImage.src = `${imgURLValue}`;
  backgroundImage.crossOrigin = 'Anonymous';
  backgroundImage.onload = function () {
    canvas.width = backgroundImage.width;
    canvas.height = backgroundImage.height;
    ctx.drawImage(
      backgroundImage,
      0,
      0,
      backgroundImage.width,
      backgroundImage.height
    );
  };
}
if (rotateBtn) {
  rotateBtn.addEventListener('click', handleRotateBtn);
}

function handleRotateBtn() {
  degree += 90;
  if (degree >= 360) {
    degree = 0;
  }

  if (degree === 0 || degree === 180) {
    canvas.width = backgroundImage.width;
    canvas.height = backgroundImage.height;
  } else {
    canvas.width = backgroundImage.height;
    canvas.height = backgroundImage.width;
  }

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.save();

  ctx.translate(backgroundImage.width / 2, backgroundImage.height / 2);
  ctx.rotate((degree * Math.PI) / 180);

  ctx.drawImage(
    backgroundImage,
    0,
    0,
    backgroundImage.width,
    backgroundImage.height,
    -backgroundImage.width / 2,
    -backgroundImage.height / 2,
    backgroundImage.width,
    backgroundImage.height
  );

  ctx.restore();
}

[enter image description here][1]

If I rotate the image, the image is loaded like this.. [1]: https://i.stack.imgur.com/drNaG.png

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The origin of your rotation should be the center of the canvas, so that it uses the updated width and height values.
By using the original center of the image your drawing will be offset.

const rotateBtn = document.querySelector("button");
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const backgroundImage = new Image();
backgroundImage.src = "https://picsum.photos/150/300";
backgroundImage.decode().then(() => {
  rotateBtn.disabled = false;
  handleRotateBtn();
});
let degree = -90;


if (rotateBtn) {
  rotateBtn.addEventListener('click', handleRotateBtn);
}

function handleRotateBtn() {
  degree += 90;
  if (degree >= 360) {
    degree = 0;
  }

  if (degree === 0 || degree === 180) {
    canvas.width  = backgroundImage.width;
    canvas.height = backgroundImage.height;
  } else {
    canvas.width  = backgroundImage.height;
    canvas.height = backgroundImage.width;
  }

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.save();

  // use the updated center as origin
  ctx.translate(canvas.width / 2, canvas.height / 2);
  ctx.rotate((degree * Math.PI) / 180);
  
  // I simplified this call but it does the same as yours
  ctx.drawImage(
    backgroundImage,
    -backgroundImage.width / 2,
    -backgroundImage.height / 2
  );
  ctx.restore();
}
canvas { background: ivory }
<button disabled>rotate</button><br>
<canvas></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda