Mozilla's getting a new look. What do you think? https://mzl.la/brandsurvey

CanvasPattern.setTransform()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.

The CanvasPattern.setTransform() method uses an SVGMatrix object as the pattern's transformation matrix and invokes it on the pattern.

Syntax

void pattern.setTransform(matrix);

Parameters

matrix
An SVGMatrix to use as the pattern's transformation matrix.

Examples

Using the setTransform method

This is just a simple code snippet which uses the setTransform method to create a CanvasPattern with the specified pattern transformation from an SVGMatrix. The pattern gets applied if you set it as the current fillStyle and gets drawn onto the canvas when using the fillRect() method, for example.

HTML

<canvas id="canvas"></canvas>
<svg id="svg1"></svg>

JavaScript

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

var svg1 = document.getElementById("svg1");
var matrix = svg1.createSVGMatrix();

var img = new Image();
img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';

img.onload = function() {
  var pattern = ctx.createPattern(img, 'repeat');
  pattern.setTransform(matrix.rotate(-45).scale(1.5));
  ctx.fillStyle = pattern;
  ctx.fillRect(0,0,400,400);
};

Edit the code below and see your changes update live in the canvas:

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'CanvasPattern.setTransform' in that specification.
Living Standard  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? 33 (33) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? 33.0 (33) ? ? ?

See also

Document Tags and Contributors

 Contributors to this page: fscholz
 Last updated by: fscholz,