gpt4 book ai didi

javascript - Canvas 触觉 JS

转载 作者:行者123 更新时间:2023-12-03 00:20:14 28 4
gpt4 key购买 nike

我有一个项目需要建立一个自行车预订网站。我需要创建一个 Canvas 以允许人们签名。我创建了 Canvas ,它可以很好地使用鼠标,但我需要知道如何在移动设备上执行此操作。我尝试了几件事(使用 touchmove/touchstart),但我无法让它工作。这是我在 ES6 中的对象 Canvas (我不使用 jQuery)。我不允许使用任何库,我只想使用 JS(我会在某个时候学习 jQuery)。

class Canvas {

constructor(canvasId, clearBtnId) {
this.canvas = document.getElementById(canvasId);
this.idBtnClear = clearBtnId;
this.ctx = this.canvas.getContext("2d");
this.painting = false;
this.isEmpty = true;

this.canvas.addEventListener("mousedown", this.mouseDown.bind(this));
this.canvas.addEventListener("mouseup", this.mouseUp.bind(this));
this.canvas.addEventListener("mousemove", this.draw.bind(this));
this.canvas.addEventListener("mouseout", this.mouseUp.bind(this));

document.getElementById(this.idBtnClear).addEventListener("click", this.clearCanvas.bind(this));

}

mouseDown(e) {
this.painting = true;
}

mouseUp() {
this.painting = false;
this.ctx.beginPath();
}

draw(e) {
if (!this.painting) return;
this.ctx.lineWidth = 5;
this.ctx.lineCap = "round";
this.ctx.lineJoin = "round";

let topPos = e.pageY - this.canvas.offsetTop;
let leftPos = e.pageX - this.canvas.offsetLeft;

this.ctx.lineTo(leftPos, topPos);
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(leftPos, topPos);

this.isEmpty = false;
}

clearCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.isEmpty = true;
}

}

最佳答案

您可以使用这些事件

适用于触摸设备。

关于javascript - Canvas 触觉 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54348152/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com