收藏
回答

canvas 2d 宽度高度修改后,画布自动清空?

        //设置canvas的宽度高度
        canvas.width = showInfo.width * dpr;
        canvas.height = showInfo.height * dpr;
        ctx.scale(dpr, dpr);


canvas 2d 宽度高度修改后,画布自动清空?

缩放canvas组件,修改canvas的宽度和高度,就遇到了当前问题。

  backMove(e) {
    var _this = this;
    var showInfo = this.data.showInfo;
    if (showInfo.isMove) {
      if (e.touches.length >= 2) {
        var touch1 = e.touches[0]; //获取第一个触点位置信息
        var touch2 = e.touches[1];


        var spanX = touch1.pageX - touch2.pageX;
        var spanY = touch1.pageY - touch2.pageY;
        showInfo.newSpan = Math.sqrt(spanX * spanX + spanY * spanY);
        var scale = showInfo.newSpan / showInfo.oldSpan;
        scale = Math.min(scale, 1.05);
        showInfo.zoom = scale;
        showInfo.width = showInfo.width * scale;
        showInfo.height = showInfo.height * scale;


        showInfo.width = Math.max(showInfo.width, showInfo.oldWidth);
        showInfo.height = Math.max(showInfo.height, showInfo.oldHeight);
        showInfo.width = Math.min(showInfo.width, showInfo.oldWidth * 2);
        showInfo.height = Math.min(showInfo.height, showInfo.oldHeight * 2);


        _this.setData({
          showInfo: showInfo
        });


        //设置canvas的宽度高度
        canvas.width = showInfo.width * dpr;
        canvas.height = showInfo.height * dpr;
        ctx.scale(dpr, dpr);


      }
    }
  },


小程序库,版本2.20.0

回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容