Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!