Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating a html5 canvas

I want to know how I can translate an entire scene already drawn on an html5 canvas, for example 5 pixels down. I know the translate method just translates the canvas' coordinate system, but I want to know if there is a way to translate the entire scene that is already drawn onto the canvas.

like image 966
George Avatar asked Aug 02 '10 20:08

George


People also ask

What is CTX translation?

Syntax. The translate() method adds a translation transformation to the current matrix by moving the canvas and its origin x units horizontally and y units vertically on the grid.

How do you translate in HTML?

The translate attribute specifies whether the content of an element should be translated or not. Test: Use the Google translate box (at the top of the page) to change to another language, and look what happens to the word "ice cream" below: Here we use translate="no": ice cream.

What is canvas translate in JavaScript?

The translate() is a method of a 2D drawing context. The translate(x,y) method moves the canvas and its origin x units horizontally and y units vertically. In this syntax: x represents the distance that you want to move the origin of the canvas in the horizontal direction.

Is HTML5 Canvas still used?

The HTML5 canvas has the potential to become a staple of the web, enjoying ubiquitous browser and platform support in addition to widespread webpage support, as nearly 90% of websites have ported to HTML5.


1 Answers

You can apply the transforms and call drawImage passing in the canvas itself.

ctx.save();
ctx.translate(0, 5);
ctx.drawImage(canvas, 0, 0);
ctx.restore();

When doing that, the original contents will still be below. Depending on the effect you're trying to accomplish, setting the globalCompositeOperation may help you with that.

But it's likely you'll need to use drawImage to first copy to a second canvas, clear the current, apply the transform and draw from the copy.

like image 153
Kyle Jones Avatar answered Sep 22 '22 23:09

Kyle Jones