• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
逍遥汉
博客园    首页    新随笔    联系   管理    订阅  订阅

使用 canvas 重画文字重影问题

刷新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 = '';

 

时间如白驹过隙,忽然而已,且行且珍惜......
posted @ 2022-12-07 10:44  unfetteredman  阅读(317)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3