Can't rotate image inside canvas


username
if (player1Ready) {
    ctx.save();  
    ctx.rotate(player1.rotation);  
    ctx.drawImage(player1Image, player1.x, player1.y,80,80);
    ctx.restore();  }

I'm trying to rotate a single image inside a canvas based on the angle the player is pressing left or right.

I can't understand how it works, can anyone help?

Mathiasfc

Capturing the left key <( 37 ) and right key >( 39 ) calls a function that updates the image rotation coefficient, which in this example is converted to degrees.

document.onkeydown = function (e) {
    if(e.keyCode == 39){
        angleInDegrees+=5;
        drawRotated(angleInDegrees);
    }else if(e.keyCode == 37){
        angleInDegrees-=5;
        drawRotated(angleInDegrees);
    }
}

function drawRotated(degrees){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.save();
    ctx.translate(canvas.width/2,canvas.height/2);
    ctx.rotate(degrees*Math.PI/180);
    ctx.drawImage(image,-image.width/2,-image.width/2);
    ctx.restore();
}

See a working example here : http://jsfiddle.net/mathiasfcx/6ZsCz/3088/

Related


Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Rotate an existing image on the canvas

Alex Saidani I am trying to rotate an image already drawn on an HTML canvas like this: var canvas = document.getElementById("editorCanvas"); var ctx = canvas.getContext("2d"); var canvasOffset = $("#editorCanvas").offset(); var offsetX = canvasOffset.left; va

Rotate an image in the canvas

Yaroslav Shabanov I am trying to create some rotated images. So I created an array of canvases and put in each picture. But for some reason they don't show up. please help me. var canvas = [], ctx = [], particles = []; for (var i = 0; i < 10; i++){ sym.$( sy

Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB

Can't rotate image in react

kill me I am working on React-Avatar editor. My spin buttons are not working as expected. Here is my code link : https://codesandbox.io/s/example-for-react-avatar-editor-ofoz4 oskenforth You are not using a rotation value. I've fixed it in this fork: https://c

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Rotate an image in the canvas

Yaroslav Shabanov I am trying to create some rotated images. So I created an array of canvases and put in each picture. But for some reason they don't show up. please help me. var canvas = [], ctx = [], particles = []; for (var i = 0; i < 10; i++){ sym.$( sy

Can't rotate image in react

kill me I am working on React-Avatar editor. My spin buttons are not working as expected. Here is my code link : https://codesandbox.io/s/example-for-react-avatar-editor-ofoz4 oskenforth You are not using a rotation value. I've fixed it in this fork: https://c

Rotate the image in the canvas using the mouse

Deepu SA In my code I am loading the image into the canvas. Then I need to resize, rotate and drag it. I managed to achieve dragging and resizing. How can I achieve rotation (along the center of the image) using the mouse on this code. my html page: <!doctype

Rotate the image in the canvas using the mouse

Deepu SA In my code I am loading the image into the canvas. Then I need to resize, rotate and drag it. I managed to achieve dragging and resizing. How can I achieve rotation (along the center of the image) using the mouse on this code. my html page: <!doctype

Rotate an existing image on the canvas

Alex Saidani I am trying to rotate an image already drawn on an HTML canvas like this: var canvas = document.getElementById("editorCanvas"); var ctx = canvas.getContext("2d"); var canvasOffset = $("#editorCanvas").offset(); var offsetX = canvasOffset.left; va

Rotate rectangular canvas image to fit

wbt11a I'm trying to rotate an image that fills a rectangle canvas element. Using the code below, I can rotate satisfactorily (in 90 degree increments). However, when converting large faces to small faces, I can't figure out how to fill the entire canvas. Inst

Code in WPF can't rotate image

Matt I have a situation where I want to rotate a control on a WPF form on a left mouse button down event. I've tried to adapt code I found elsewhere, but I haven't gotten there yet. Here is what I have: XAML: <Image Name="UpArrow"

Resize image, then rotate in canvas - javascript

pelvis I'm using an image with dimensions 1024 x 768 and the canvas is 300 x 300, so I want to resize the image to 300 x 300 first and then rotate it proportionally in the canvas. I am using the following code but it is resizing the image but rotating some ele

Can't rotate simple .jpg image

third Some parts of the image captured by my Motorola Moto phone (which is a problem in many models, e.g. Moto One, Moto Z2 Play, etc.) are rotated 90 degrees clockwise. Even though I've rotated hundreds of thousands of images using IrfanView over the past 15

Can't rotate rectangle in canvas

Rajib I am new to HTML5 canvas. I'm trying to build a game area using an HTML canvas, where someone can drive around a car inside the Canvas area, like if I press the left arrow key, the car will turn to the left, and if the right arrow key is pressed, the car

Rotate the image but not the canvas

Pujan Srivastava I am using a background image on the canvas. I just want to rotate the background image by 90 degrees, but the context should not be rotated. If I use CSS transitions then the whole canvas rotates. What can I do? var can = document.getElementB

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Rotate an image in the canvas

Yaroslav Shabanov I am trying to create some rotated images. So I created an array of canvases and put in each image. But for some reason they don't show up. please help me. var canvas = [], ctx = [], particles = []; for (var i = 0; i < 10; i++){ sym.$( sym.

Can't rotate image inside canvas

username if (player1Ready) { ctx.save(); ctx.rotate(player1.rotation); ctx.drawImage(player1Image, player1.x, player1.y,80,80); ctx.restore(); } I'm trying to rotate a single image inside a canvas based on the angle the player is pressing

Rotate the image in the canvas using the mouse

Deepu SA In my code I am loading the image into the canvas. Then I need to resize, rotate and drag it. I managed to achieve dragging and resizing. How can I achieve rotation (along the center of the image) using the mouse on this code. my html page: <!doctype

Rotate rectangular canvas image to fit

wbt11a I'm trying to rotate an image that fills a rectangle canvas element. Using the code below, I can rotate satisfactorily (in 90 degree increments). However, when converting large faces to small faces, I can't figure out how to fill the entire canvas. Inst

Code in WPF can't rotate image

Matt I have a situation where I want to rotate a control on a WPF form on a left mouse button down event. I've tried to adapt code I found elsewhere, but I haven't gotten there yet. Here is what I have: XAML: <Image Name="UpArrow"

Resize image, then rotate in canvas - javascript

pelvis I'm using an image with dimensions 1024 x 768 and the canvas is 300 x 300, so I want to resize the image to 300 x 300 first and then rotate it proportionally in the canvas. I am using the following code but it is resizing the image but rotating some ele

Rotate an existing image on the canvas

Alex Sadani I'm trying to rotate an image already drawn on an HTML canvas like this: var canvas = document.getElementById("editorCanvas"); var ctx = canvas.getContext("2d"); var canvasOffset = $("#editorCanvas").offset(); var offsetX = canvasOffset.left; var