这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
CanvasPattern
.setTransform()
方法使用 SVGMatrix
对象作为图案的变换矩阵,并在此图案上调用它。
语法
void pattern.setTransform(matrix);
参数
matrix
SVGMatrix
,被用作图案的变换矩阵。
示例
使用 setTransform
方法
这是一段简单的代码片段,使用 setTransform
方法创建一个来自 SVGMatrix
具有指定图案变化的CanvasPattern
。如例子所示,如果你把图案赋值给当前的 fillStyle
,当你使用 fillRect()
方法时,图案会被应用到 canvas 上绘制出效果。
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);
};
修改下面的代码并在线查看 canvas 的变化:
规范描述
Specification | Status | Comment |
---|---|---|
HTML Living Standard CanvasPattern.setTransform |
Living Standard |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
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) | ? | ? | ? |
参见
- 接口定义,
CanvasPattern
SVGMatrix