setTransform() Method - Javascript Canvas Reference

Javascript examples for Canvas Reference:setTransform

Description

setTransform() method can scale, rotate, move, and skew the current context.

JavaScript syntax

context.setTransform(a, b, c, d, e, f);

Parameter Values

Parameter Description
a Scales the drawings horizontally
b Skews the drawings horizontally
c Skews the drawings vertically
d Scales the drawings vertically
e Moves the the drawings horizontally
f Moves the the drawings vertically

Example

Draw a rectangle, reset and create a new transformation matrix with setTransform()

JavaScript:

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.fillStyle = "yellow";
ctx.fillRect(0, 0, 250, 100)//w  w w  .ja v a2s  . c  o  m

ctx.setTransform(1,0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 250, 100);

ctx.setTransform(1,0.5, -0.5, 1, 30, 20);
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 250, 100);

</script>

</body>
</html>

Related Tutorials