canvas中的状态保存与恢复

保存的方法save(),恢复的方法restore()

保存方法,是将状态保存在一个状态栈里,先进后出,后进先出

示例demo:

< script >
    var  canvas =  document . getElementById ( "canvas" );
    var  ctx = canvas . getContext ( "2d" );

    //1 、绘制一个绿色描边的矩形
    ctx . strokeStyle = "green" ;
    ctx . lineWidth = 3 ;
    ctx .setLineDash ([ 10 , 5 , 2 , 5 ]);
    ctx . strokeRect ( 50 , 50 , 300 , 200 );


    //2 、绘制一个红色描边的圆
    ctx . beginPath ();
    ctx . lineWidth = 5 ;
    ctx . strokeStyle = "red" ;
    ctx .setLineDash ([]);
    ctx . arc ( 600 , 150 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();

    //3 、绘制一个绿色描边的圆
    ctx . beginPath ();
    ctx .setLineDash ([ 10 , 5 , 2 , 5 ]);
    ctx . lineWidth = 3 ;
    ctx . strokeStyle = "green" ;
    ctx . arc ( 500 , 400 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();
</ script >

运用保存和恢复的写法:

< script >
    var  canvas =  document . getElementById ( "canvas" );
    var  ctx = canvas . getContext ( "2d" );
    //1 、绘制一个绿色描边的矩形
    ctx . strokeStyle = "green" ;
    ctx . lineWidth = 3 ;
    ctx .setLineDash ([ 10 , 5 , 2 , 5 ]);
    ctx . strokeRect ( 50 , 50 , 300 , 200 );

    ctx . save ();


    //2 、绘制一个红色描边的圆
    ctx . beginPath ();
    ctx . lineWidth = 5 ;
    ctx . strokeStyle = "red" ;
    ctx .setLineDash ([]);
    ctx . arc ( 600 , 150 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();


    //3 、绘制一个绿色描边的圆
    ctx . beginPath ();

    // 恢复
    ctx . restore ();

    ctx . arc ( 500 , 400 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();
</ script >

save保存的状态栈的原理:先进后出:

< script >
    var  canvas =  document . getElementById ( "canvas" );
    var  ctx = canvas . getContext ( "2d" );

    //1 、绘制一个绿色描边的矩形
    ctx . strokeStyle = "green" ;
    ctx . lineWidth = 3 ;
    ctx .setLineDash ([ 10 , 5 , 2 , 5 ]);
    ctx . strokeRect ( 50 , 50 , 300 , 200 );

    ctx . save ();


    //2 、绘制一个红色描边的圆
    ctx . beginPath ();
    ctx . lineWidth = 5 ;
    ctx . strokeStyle = "red" ;
    ctx .setLineDash ([]);
    ctx . arc ( 600 , 150 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();

    ctx . save ();

    //3 、绘制一个绿色描边的圆
    ctx . beginPath ();

    // 恢复
    ctx . restore (); // 恢复到第二个状态
    ctx . restore (); // 恢复到第一个状态

    ctx . arc ( 500 , 400 , 150 , 0 , 2 * Math . PI );
    ctx . stroke ();
</ script >

保存和恢复方法使用的示例

demo1
< script >
    var  canvas =  document . getElementById ( "canvas" );
    var  ctx = canvas . getContext ( "2d" );

    ctx . strokeRect ( 0 , 0 , 300 , 200 );

    ctx . save ();

    ctx . translate ( 200 , 200 );
    ctx . strokeRect ( 0 , 0 , 300 , 200 );

    ctx . restore ();

    ctx . clearRect ( 0 , 0 , canvas . width , canvas . height );
</ script >


demo2:

< script >
    var  canvas =  document . getElementById ( "canvas" );
    var  ctx = canvas . getContext ( "2d" );


    var  x= 0 ;

    var  timer= setInterval ( function (){

        ctx . clearRect ( 0 , 0 , canvas . width , canvas . height );

        ctx . save ();

        ctx . translate ( x , 0 );
        ctx . strokeRect ( 0 , 0 , 300 , 200 );

        ctx . restore ();

        x++ ;

    }, 10 );

</ script >

























  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值