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.$( sym.$("Rectangle") ).append('<canvas id="partical' + i + '"></canvas>');
	canvas.push(document.getElementById("partical" + i));

	canvas[i].width = 550;
	canvas[i].height = 400;
	ctx[i] = canvas[i].getContext("2d");
}


for(var i = 0; i < 10; i++){
	particles.push({
		x: Math.random()*canvas[i].width, //x-coordinate
		y: Math.random()*canvas[i].height, //y-coordinate
		img: new Image()
	})

	particles[i].img.src = "images/Flake" + Math.floor(Math.random()*4+1) + ".png";
}

function draw(){
	for(var i = 1; i < 10; i++){
		ctx[i].clearRect(0, 0, canvas[i].width, canvas[i].height);
		var p = particles[i];
		ctx[i].drawImage(p.img,p.x,p.y,50,50); 
		ctx[i].fill();
	}
}

draw();

canvas

jsFiddle:https ://jsfiddle.net/CanvasCode/u0p9wtut/

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var Particle = function(xSet, ySet)
{
    this.XPos = xSet;
    this.YPos = ySet;
    this.Sprite = new Image();
        this.Sprite.src = "http://s.cdn.gaiaonline.com/images/thumbnails/85449184bbb9.png";
    this.Rotation = 0;
}

var particles = new Array();

for(var i = 0; i < 10; i++){
    particles.push(new Particle(Math.random()*c.width, Math.random()*c.height));
}

function draw()
{
  ctx.fillStyle = "#000";
  ctx.fillRect(0,0,c.width,c.height);

    for(var i = 0; i < particles.length; i++)
    {
      var particle = particles[i];
      particle.YPos += 1;

      // Draw image rotated
      ctx.save();

      ctx.save();
      ctx.translate(particle.XPos + particle.Sprite.width / 2, particle.YPos + particle.Sprite.height / 2);
            ctx.rotate( particle.Rotation );
            ctx.translate(-(particle.XPos + (particle.Sprite.width / 2)), -(particle.YPos));
      ctx.drawImage(particle.Sprite, particle.XPos, particle.YPos);
      ctx.restore();

      ctx.restore();

      if(particle.YPos > c.height)
        {
          particle.YPos = 0;
          particle.XPos = Math.random()*c.width;
        }

      particle.Rotation += 0.01;
    }
}

setInterval(draw, 3);

What you can do is use ctx.save, ctx.restoreand ctx.translate, you just save your canvas background first, so we can reload it later, and we convert to canvas, basically the translation is like setting where to draw the image offset, we set The given translation position is to the center of the sprite, and then we rotate around the image (translation means we will rotate the image around the center point of the image). Then we update the translation again, draw the rotated image, and restore the canvas context because we might want to draw something else. If we don't restore the context, the next image drawn will take translation and rotation into account.

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

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 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

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 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 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

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.

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

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 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

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 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

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 the image around the image center on the canvas

Mateusz Ceranka I want to animate image rotation. Right now, my image is rotating, but around a point. I want to animate without moving the image (around the center of the image). I hope you understand what I mean. Here is part of my code: ctx.save();

Rotate the image around the image center on the canvas

Mateusz Ceranka I want to animate image rotation. Right now, my image is rotating, but around a point. I want to animate without moving the image (around the center of the image). I hope you understand what I mean. Here is part of my code: ctx.save();

Rotate the image around the image center on the canvas

Mateusz Ceranka I want to animate image rotation. Right now, my image is rotating, but around a point. I want to animate without moving the image (around the center of the image). I hope you understand what I mean. Here is part of my code: ctx.save();

Rotate the image around the image center on the canvas

Mateusz Ceranka I want to animate image rotation. Right now, my image is rotating, but around a point. I want to animate without moving the image (around the center of the image). I hope you understand what I mean. Here is part of my code: ctx.save();

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 image before upload with hidden canvas

Toto NaBendo I am trying to rotate a picture before uploading it. I'm trying to create a canvas in the air, rotate it, and get data in it, but that doesn't seem to work, here's a fiddle: function rotate() { const img = document.getElementById("image"); c

Resize the image and rotate the canvas 90 degrees

dough There are a lot of topics here about rotating images with canvas on JS. I read most of them but couldn't find a solution for my problem. I am receiving images from any resolution (from upload component). I resized it to 1024x768 like: var canvas = docume

JavaScript - Rotate canvas image, cut out corners

Hendrian I am trying to rotate an image inside HTML canvas. The rotation does work, however, the corners of the image are clipped/clipped off with each rotation. This is wrong because the entire uploaded image should be intact after rotation. Some important de

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

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

Resize the image and rotate the canvas 90 degrees

dough There are a lot of topics here about rotating images with canvas on JS. I read most of them but couldn't find a solution for my problem. I am receiving images from any resolution (from upload component). I resized it to 1024x768 like: var canvas = docume

Rotate image before upload with hidden canvas

Toto NaBendo I am trying to rotate a picture before uploading it. I'm trying to create a canvas in the air, rotate it, and get data in it, but that doesn't seem to work, here's a fiddle: function rotate() { const img = document.getElementById("image"); c

Rotate each image to draw multiple images on canvas

username I am new to HTML5 programming and I would like to know how to rotate each image after adding them to the canvas. Should each of them be put into the canvas and then rotated? If so, how can I add multiple canvases to a single canvas context. Fiddle : h