HTML canvas transform() 方法
實例
繪製一個矩形,通過transform() 添加一個新的變換矩陣,再次繪製矩形,添加一個新的變換矩陣,然後再次繪製矩形。 請注意,每當您調用transform() 時,它都會在前一個變換矩陣上構建:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="yellow";
ctx.fillRect(0,0,250,100)
ctx.transform(1,0.5,-0.5,1,30,10);
ctx.fillStyle="red";
ctx.fillRect(0,0,250,100);
ctx.transform(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.transform(1,0.5,-0.5,1,30,10);
ctx.fillStyle="red";
ctx.fillRect(0,0,250,100);
ctx.transform(1,0.5,-0.5,1,30,10);
ctx.fillStyle="blue";
ctx.fillRect(0,0,250,100);
嘗試一下»
瀏覽器支持
Internet Explorer 9、Firefox、Opera、Chrome 和Safari 支持transform() 方法。
注意: Internet Explorer 8及之前的版本不支持<canvas>元素。
定義和用法
畫布上的每個對像都擁有一個當前的變換矩陣。
transform() 方法替換當前的變換矩陣。 它以下面描述的矩陣來操作當前的變換矩陣:
a | c | e |
b | d | f |
0 | 0 | 1 |
換句話說,transform() 允許您縮放、旋轉、移動並傾斜當前的環境。
注意:該變換只會影響transform()方法調用之後的繪圖。
注意: transform()方法的行為相對於由rotate()、scale()、translate()或transform()完成的其他變換。例如:如果您已經將繪圖設置為放到兩倍,則transform() 方法會把繪圖放大兩倍,您的繪圖最終將放大四倍。
提示:請查看setTransform()方法,它不會相對於其他變換來發生行為。
JavaScript 語法: | context.transform( a,b,c,d,e,f ); |
---|
參數值
参数 | 描述 |
---|---|
a | 水平缩放绘图。 |
b | 水平倾斜绘图。 |
c | 垂直倾斜绘图。 |
d | 垂直缩放绘图。 |
e | 水平移动绘图。 |
f | 垂直移动绘图。 |
HTML canvas參考手冊