Canvas setTransform() 方法
示例
繪製一個矩形。重置變換矩陣。再次繪製矩形。重置變換矩陣。再次繪製矩形。
每次呼叫 setTransform() 時,都會構建一個新的矩陣。在下面的示例中,紅色矩形未顯示,因為它在藍色矩形下方。
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "yellow";
ctx.fillRect(0, 0, 250, 100)
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 250, 100);
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 250, 100);
自己動手試一試 »
描述
setTransform()
方法用於縮放、旋轉、移動和傾斜上下文。
畫布上的每個物件都有一個變換矩陣。
setTransform()
方法會將變換矩陣重置為單位矩陣,然後使用相同的引數執行 transform()。
注意
該變換會影響在呼叫 setTransform() 之後進行的繪製。
另請參閱
scale() 方法(縮放上下文)
rotate() 方法(旋轉上下文)
translate() 方法(重新對映 0,0 位置)
transform() 方法 (縮放、旋轉、移動、傾斜上下文)
語法
context.setTransform(a, b, c, d, e, f) |
引數值
引數 | 描述 | 試一試 |
---|---|---|
a | 水平縮放繪圖 | 試一試 » |
b | 水平傾斜繪圖 | 試一試 » |
c | 垂直傾斜繪圖 | 試一試 » |
d | 垂直縮放繪圖 | 試一試 » |
e | 水平移動繪圖 | 試一試 » |
f | 垂直移動繪圖 | 試一試 » |
返回值
無 |
瀏覽器支援
<canvas>
元素是 HTML5 標準(2014)。
setTransform()
在所有現代瀏覽器中都受支援。
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 9-11 |
❮ Canvas 參考