<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</title> <style type="text/css"> #canvas{border:1px solid #eee ; display:block; background-color: #B36666; margin: 20px auto; } </style></head><body><div><canvas id = "canvas" width = "1300px" height = "800px"></canvas></div> <script type = "text/javascript" >
window.onload=function(){
var context = document.getElementById(‘canvas‘).getContext(‘2d‘)
/* transform 变化矩阵
context.transform(1,0,0,1,0,0); 默认值
参数2:x坐标的倾斜度
context.transform(1,2,0,1,0,0)
参数3:y坐标的倾斜度
context.transform(1,0,2,1,0,0)
参数1:x坐标的放大倍数
context.transform(3,0,0,1,0,0) == context.scale(3,1);
参数4:y坐标的放大倍数
context.transform(1,0,0,2,0,0) == context.scale(1,2);
参数5:x坐标的偏移
context.transform(1,0,0,1,50,0) == context.translate(50,0);
参数6:y坐标的偏移
context.transform(1,0,0,1,50,40) == context.translate(50,40);
注意: transform 会叠加
解决方案: context.setTransform(1,0,0,1,10,10);
*/
context.strokeStyle=‘#eeeeee‘;
context.fillStyle=‘rgba(70,200,200,0.5)‘;
context.lineWith = 9;
context.save();
context.transform(1,2,2,1,0,100);
context.transform(1,0,0,1,0,0); //有叠加,附加在之前的变化
context.fillRect(30,30,100,150);
context.strokeRect(30,30,100,150);
context.restore();
context.save();
context.setTransform(1,0,0,1,0,0);//解决叠加方案:
context.fillRect(300,30,100,150);
context.strokeRect(300,30,100,150);
context.restore();
}
</script> </body> </html>