W3Schools.com

HTML5 canvas translate Method

HTML5 track Tag Reference HTML5 canvas reference

Example

Draw a rectangle, move the canvas 70px down and to the right, and draw the rectangle again:

Yor browser does not support the canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillRect(10,10,100,50);
ctx.translate(70,70);
ctx.fillRect(10,10,100,50);

Try it yourself »

Definition and Usage

The translate method moves the canvas to the given position.

The canvas itself will not change position, but all drawings done after the translate() method is moved:

The translate method takes two parameters, the x and y coordinates of where to move the canvas.

JavaScript syntax: context.translate(x,y);


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The translate method is supported in all major browsers.


Parameter Values

Parameter Values Description Play it
x x-coordinate Moves the canvas horizontally to the given coordinate Play it »
y y-coordinate Moves the canvas vertically to the given coordinate Play it »

HTML5 track Tag Reference HTML5 canvas reference


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
Download XML Editor
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE