translate() Method - Javascript Canvas Reference

Javascript examples for Canvas Reference:translate

Description

The translate() method move the (0,0) position on the canvas.

JavaScript syntax

context.translate(x, y);

Parameter Values

Parameter Description
x The value to add to horizontal (x) coordinates
y The value to add to vertical (y) coordinates

Example

Draw a rectangle in position (10,10), set new (0,0) position to (70,70).

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillRect(10, 10, 100, 50);//from w  ww.  j  ava 2s.c om
ctx.translate(70, 70);
ctx.fillRect(10, 10, 100, 50);

</script>

</body>
</html>

Related Tutorials