0
点赞
收藏
分享

微信扫一扫

canvas的绘图2


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<canvas id="canvas1" width="800" height="800"></canvas>
<script>
function draw1(){
var c=document.getElementById("canvas1");
var ctx=c.getContext("2d");

ctx.fillStyle='red';
ctx.fillRect(0,0,150,150);
ctx.save();
ctx.fillStyle='green';
ctx.fillRect(15,15,120,120);
ctx.save();
ctx.fillStyle='blue';
ctx.fillRect(30,30,90,90);

ctx.restore();
ctx.fillRect(45,45,60,60);
ctx.restore();
ctx.fillRect(60,60,30,30);
}
window.addEventListener("load",draw1(),true);
</script>
</body>
</html>在

在绘图代码上如果不加save属性会怎么样呢?则默认是红色的,保存一次颜色,需要加一次save.


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<canvas id="canvas2" width="800" height="800"></canvas>
<script>
function draw2(){
var c=document.getElementById("canvas2");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.font="40px 隶书";
ctx.fillText("Hello H5",0,100);
ctx.setTransform(1,-0.5,0,-1,0,2);

ctx.fillText("Hello H5", 0, -100)

}
window.addEventListener("load",draw2(),true);
</script>
</body>
</html>


定义和用法

画布上的每个对象都拥有一个当前的变换矩阵。

transform() 方法替换当前的变换矩阵。它以下面描述的矩阵来操作当前的变换矩阵:

a  c  e
b d f
0 0 1

换句话说,transform() 允许您缩放、旋转、移动并倾斜当前的环境。

注释:该变换只会影响 transform() 方法调用之后的绘图。

注释:transform() 方法的行为相对于由 rotate(), scale(), translate(), or transform() 完成的其他变换。例如:如果您已经将绘图设置为放到两倍,则 transform() 方法会把绘图放大两倍,您的绘图最终将放大四倍。



JavaScript 语法:

context.transform(a,b,c,d,e,f);

参数值

参数

描述

a

水平缩放绘图

b

水平倾斜绘图

c

垂直倾斜绘图

d

垂直缩放绘图

e

水平移动绘图

f

垂直移动绘图

该旋转方法,仍然遵循逆时针为负,顺时针为正的原则


canvas的绘图2_html

举报

相关推荐

0 条评论