CanvasPattern.setTransform()
Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
CanvasPattern.setTransform()
方法使用 DOMMatrix
对象作为图案的变换矩阵,并在此图案上调用它。
语法
setTransform(matrix)
参数
matrix
-
DOMMatrix
,被用作图案的变换矩阵。
示例
使用 setTransform
方法
这是一段简单的代码片段,使用 setTransform
方法创建一个来自 DOMMatrix
具有指定图案变化的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 = '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 |
---|
HTML Standard # dom-canvaspattern-settransform-dev |
浏览器兼容性
BCD tables only load in the browser
参见
- 接口定义,
CanvasPattern
DOMMatrix