首页 > 其他分享 >使用 canvas 重画文字重影问题

使用 canvas 重画文字重影问题

时间:2022-12-07 10:59:35浏览次数:34  
标签:canvas const ctx current canvasRef 重影 重画

刷新canvas,有时会出现上次的内容无法清除而造成重影,需要进行以下步骤的处
//在手机上,需加上这步,先隐藏再显示,类似刷新

      const oldWidth = canvasRef.current.width;
      const oldHeight = canvasRef.current.height;
      // 先隐藏canvas
      canvasRef.current.style.display = 'none';
      const ctx = canvasRef.current.getContext('2d');
      // 保存canvas设置,清楚画布区域
      ctx?.save();
      ctx?.setTransform(1, 0, 0, 1, 0, 0);
      ctx?.clearRect(0, 0, oldWidth, oldHeight);
      // 还原canvas设置
      ctx?.restore();
      canvasRef.current.style.display = '';

 

标签:canvas,const,ctx,current,canvasRef,重影,重画
From: https://www.cnblogs.com/UnfetteredMan/p/16962424.html

相关文章