Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

touchmove(e) { if (!this.imageSrc) return; // 取出x, y的值 let { x, y } =…

  1. User

    touchmove(e) {

    			if (!this.imageSrc) return;
    			// 取出x, y的值
    			let {
    				x,
    				y
    			} = e.changedTouches[0]
    			// 保存本次绘制的数据
    			this.penData.push({
    				type: 'line',
    				startX: this.moveX || x,
    				startY: this.moveY || y,
    				endX: x,
    				endY: y,
    				lineWidth: this.context.lineWidth,
    				strokeStyle: this.context.strokeStyle,
    			});
    			// 如果上次绘图的位置与当前相差太小,则不再绘制
    			if (this.moveX && this.moveY && Math.abs(x - this.moveX) < 2 && Math.abs(y - this.moveY) < 2) {
    				return;
    			}
    			
    			// 绘制线条起点	 
    			this.context.beginPath();
    			this.context.moveTo(this.moveX || x, this.moveY || y);
    			this.context.lineTo(x, y);
    			this.moveX = x
    			this.moveY = y
    			this.context.stroke()
    			// ture,保留之前的内容
    			this.context.draw(true)
    
    		}, 这段代码无法在微信小程序内画出白线
    
  2. Assistant

    可能是因为使用了默认的黑色线条颜色,而在微信小程序内默认背景是白色,所以无法看到绘制的线条。解决方法是在onLoad中设置画布背景颜色为白色,或者在绘制时设置线条颜色为白色。可以尝试修改以下代码:

    // 在onLoad中设置画布背景颜色为白色
    wx.createSelectorQuery().select('#canvas').fields({
      node: true,
      size: true,
    }).exec((res) => {
      const canvas = res[0].node;
      const context = canvas.getContext('2d');
      context.fillStyle = '#fff';
      context.fillRect(0, 0, res[0].width, res[0].height);
      this.context = context;
    });
    
    // 或者在绘制时设置线条颜色为白色
    this.context.strokeStyle = '#fff';
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.