如何保存和回复canvas状态

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    function draw(){
        //获取js对象
        var c=document.getElementById("mycanvas");
        //设置大小
        c.height=800;
        c.width=800;
        //获取用户画图的2d的环境
        var ctx=c.getContext("2d");
        //画一个矩形
        //设置填充颜色
        ctx.fillStyle="#ff0000";
        //设置矩形边框颜色
        ctx.strokeSytl="#000000";
        //绘制
        ctx.fillRect(20,20,200,100);
        ctx.strokeRect(20,20,200,100);
        ctx.fill();
        ctx.stroke();
        //将上面的状态保存起来,可以使用restore提取出来
        ctx.save();
        
        
        //在画一个
        ctx.fillStyle="#ff00ff";
        ctx.strokeSyle="#0000ff";
        ctx.fillRect(200,200,100,50);
        ctx.strokeRect(200,200,100,50);
        ctx.fill();
        ctx.stroke();
        
        //恢复状态
        ctx.restore();
        //再话一个矩形
        ctx.fillRect(300,300,100,100);
        ctx.strokeRect(300,300,100,100);
        
    }
        
</script>
</head>
<body>
    <canvas id="mycanvas"></canvas>
    <input type="button" value="画图" οnclick="draw();"/>
</body>
</html>

 

 

 

转载于:https://www.cnblogs.com/gf36500/p/6874473.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值