收藏
回答

Canvas无法正常使用,出现画线会断的问题?

canvas画布使用新版的,我是要控制画布的长宽:1:2,实际下来出现很多手机画线会断,横线还行,竖线一定会断。

<canvas class="canvas_content " type="2d" style='width:{{canvasWidth}}px; height:{{canvasHeight}}px' id="myCanvas" bindtouchstart="bindtouchstart" bindtouchmove="bindtouchmove" bindtouchend="bindtouchend" bindtouchcancel="bindtouchcancel"></canvas>

  ready() {
      
           

            wx.createSelectorQuery()
                .in(this)
                .select("#control") // canvas获取节点
                .fields({
                    node: true,
                    size: true,
                }) 
                .exec((res) => {
                 


                  
                    const deviceInFo = wx.getSystemInfoSync()
                


                


                  
                    const { windowWidth } = deviceInFo


                    const padding = this.rpx2px(15) * 2



                    let canvasHeight = deviceInFo.windowHeight - padding
                    let canvasWidth = canvasHeight * 2
                    if ((canvasHeight * 2 + res[0].width) > windowWidth) {
                       
                        let leftArea = deviceInFo?.safeArea?.left || this.rpx2px(15)
                        canvasWidth = deviceInFo.windowWidth - this.rpx2px(15) - leftArea - res[0].width
                       
                        canvasHeight = canvasWidth / 2
                    }


                    


                    this.setData({
                        deviceInFo, 
                        canvasWidth,
                        canvasHeight,
                    })


                    this.initCanvas("init")
                })

        },

 initCanvas(type) {
            let ctx = null
            let canvas = null
            let { historyImag, canvasWidth, canvasHeight, deviceInFo, initialCanvasImg } = this.data


          
            wx.createSelectorQuery()
                .in(this)
                .select("#myCanvas") // canvas获取节点
                .fields({
                    node: true,
                    size: true,
                }) 
                .exec((res) => {
                    
                    canvas = res[0].node
                    console.log(`画布初始化完成了没`, res)


                 
                    ctx = canvas.getContext("2d")
                 
                    const width = res[0].width
                    const height = res[0].height
                   
                    const dpr = wx.getWindowInfo().pixelRatio
                   
                    canvas.width = width * dpr
                    canvas.height = height * dpr
                  
                    ctx.fillStyle = "rgb(200, 0, 0)"
                  
                    ctx.lineWidth = 4
                   
                    if (historyImag.length !== 0 || (deviceInFo.platform !== "ios" && type !== "init")) {
                       
                        const image = canvas.createImage()
                     
                        image.onload = () => {
                          
                            ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight)
                        }
                      
                        image.src = historyImag[historyImag.length - 1] || initialCanvasImg
                    }


                    // 缩小/放大图像
                    ctx.scale(dpr, dpr)
                    this.setData({
                        canvas,
                        ctx,
                    })
                  
                    if (type === "init") {
                        wx.canvasToTempFilePath({
                            canvas,
                            png: "png",
                            success: (res) => {
                               
                                const tempFilePath = res.tempFilePath
                                this.setData({
                                    initialCanvasImg: tempFilePath,
                                })
                            },
                        })
                    }
                })

        },


 addPathDrop(event) {
            let { ctx, historyImag, canvas } = this.data
            let { type, changedTouches } = event
            let { x, y } = changedTouches[0]
            if (type === "touchstart"else {
               
                ctx.lineTo(x, y) 
                
                ctx.stroke()
             
                ctx.moveTo(x, y) 
            }
            
            if (type === "touchend" || type === "touchcancel") {
                


      
                    },
                })
            }
        }
最后一次编辑于  07-23
回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员--Demons
    社区技术运营专员--Demons
    07-23

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    07-23
    有用
    回复 4
    • 心锁辛城
      心锁辛城
      07-23
      https://developers.weixin.qq.com/s/4W4e5Rmo8g1x


      上述是代码片段,  现在在华为的pura 70一定会复现,就是签名框里面横线基本没问题  画竖线不行
      07-23
      回复
    • 八百里追风
      八百里追风
      07-24
      这个问题有解决进度吗?
      07-24
      回复
    • 鄒
      星期二 16:36
      我们上周也遇到了这个问题,出现在安卓机型上,有解决吗?
      星期二 16:36
      回复
    • 开发者
      开发者
      星期二 16:58
      很多手机都出现这问题了   吭哧吭哧升级,文档这个不维护了那个不维护了  倒逼我们使用你们最新的玩意  然后就是个这?
      星期二 16:58
      回复
登录 后发表内容