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

168
Visualizações
how to add css ROTATEX() and ROTATEY() to image in canvas

i am trying to make a simple canvas game in jquery,

basically, i have an infinite loop that constantly draws the player and background:

loop = () => {
    $('#player').css("transform", "rotateX("+xr+"deg) rotateY("+yr+"deg)");
    ctx.fillStyle = 'gray';
    ctx.fillRect(0,0,400,400);
    ctx.drawImage(player, x, y, 50, 50);
    window.setTimeout(loop, 1);
}

And then i have a switch statement that alters the player's XR and YR when they push the A and D buttons:

$(document).ready(()=>{
    loop();
    $(document).bind('keydown',(e)=>{
        switch(e.keyCode){
          case 65:
              //moving left (altering the xr and yr values)
              break;
          case 87:
              //moving up (altering the y and x values based on SIN and COS and player's XR and YR)
              break;
          etc...
        }
    });
});

i can tell it's detecting the key pushes, because when i run CONSOLE.LOG() in the switch statement, it prints something to the console,

so clearly this is a problem with images drawn to a canvas not keeping their css styling.

if anybody can figure out how to keep an image's css when drawing it to a canvas, or alter an image draw in a canvas's css, that would be great.

and no, i didn't find any answers anywhere else. this question doesn't seem to have been asked yet.

EDIT: xr and yr are XROTATION and YROTATION variables.

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

0

apparently, images don't keep their css in canvases, and there is no way to add this. never mind, guess i'll try something else.

about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need CSS, you can do everything with the Canvas, you have translate and rotate methods. Here a very basic example:

const ctx = canvas.getContext("2d");
const { width: w, height: h } = canvas;

let x = (w - 50) >> 1, y = (h - 50) >> 1;
let angle = 0;

const Keys = {
  pressed: {},
  handleEvent({type, code}) {
    this.pressed[code] = type === "keydown"
  }
}

document.addEventListener("keydown", Keys);
document.addEventListener("keyup", Keys);

function update() {
  if (Keys.pressed["ArrowLeft"]) {
    --angle;
  }
  if (Keys.pressed["ArrowRight"]) {
    ++angle;
  }
}

function draw() {
  ctx.fillStyle = "black";
  ctx.fillRect(0, 0, w, h);
  ctx.translate(x + 25, y + 25);
  ctx.rotate(angle * Math.PI / 180 );
  ctx.drawImage(player, -25, -25, 50, 50);
  ctx.setTransform(1, 0, 0, 1, 0, 0);
}

let player = new Image();
player.src = "https://1.bp.blogspot.com/-ddkcnhuU07g/Vllq4DNohkI/AAAAAAAACHo/p8d9SZxOy-w/s1600/mship1.png";

requestAnimationFrame(function render() {
  update();
  draw();
  requestAnimationFrame(render);
})
<canvas id="canvas" width="320" height="240"></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