CanvasPattern.setTransform()

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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
HTML Living Standard
The definition of 'CanvasPattern.setTransform' in that specification.
Living Standard Ā 

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setTransform
ExperimentalNon-standard
Chrome Full support 68Edge No support NoFirefox Full support 33IE No support NoOpera Full support 9Safari Full support 3.1WebView Android Full support 68Chrome Android Full support 68Firefox Android Full support 33Opera Android Full support 10.1Safari iOS Full support 11Samsung Internet Android No support No

Legend

Full support Ā 
Full support
No support Ā 
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.

See also