HTML canvas setTransform() 方法
實例
繪製一個矩形,通過setTransform() 重置並創建新的變換矩陣,再次繪製矩形,重置並創建新的變換矩陣,然後再次繪製矩形。 請注意,每當您調用setTransform() 時,它都會重置前一個變換矩陣然後構建新的矩陣,因此在下面的例子中,不會顯示紅色矩形,因為它在藍色矩形下面:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.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);
var ctx=c.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);
嘗試一下»
瀏覽器支持
Internet Explorer 9、Firefox、Opera、Chrome 和Safari 支持setTransform() 方法。
注意: Internet Explorer 8及之前的版本不支持<canvas>元素。
定義和用法
畫布上的每個對像都擁有一個當前的變換矩陣。
setTransform()方法把當前的變換矩陣重置為單位矩陣,然後以相同的參數運行transform() 。
換句話說,setTransform() 允許您縮放、旋轉、移動並傾斜當前的環境。
注意:該變換只會影響setTransform()方法調用之後的繪圖。
JavaScript 語法: | context.setTransform( a,b,c,d,e,f ); |
---|
參數值
参数 | 描述 |
---|---|
a | 水平缩放绘图。 |
b | 水平倾斜绘图。 |
c | 垂直倾斜绘图。 |
d | 垂直缩放绘图。 |
e | 水平移动绘图。 |
f | 垂直移动绘图。 |
HTML canvas參考手冊