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


void pattern.setTransform(matrix);


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


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.


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


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 = '';

img.onload = function() {
  var pattern = ctx.createPattern(img, 'repeat');
  ctx.fillStyle = pattern;
  ctx.fillRect(0, 0, 400, 400);

Note that newer browser versions started to support DOMMatrix as an input to setTransform(), so for example you could replace the SVGMatrix in the above example with the following:

const matrix = new DOMMatrix([1, .2, .8, 1, 0, 0]);

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


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

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setTransformChrome Full support 68Edge Full support 79Firefox Full support 33IE No support NoOpera Full support 9Safari Full support 11.1WebView Android Full support 68Chrome Android Full support 68Firefox Android Full support 33Opera Android Full support 10.1Safari iOS Full support 11Samsung Internet Android Full support 10.0
DOMMatrix parameter supportedChrome Full support YesEdge Full support YesFirefox Full support 79IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 79Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes


Full support  
Full support
No support  
No support

See also