首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >画布触觉JS

画布触觉JS
EN

Stack Overflow用户
提问于 2019-01-24 21:48:01
回答 1查看 99关注 0票数 0

我有一个项目,我需要建立一个自行车预订网站。我需要创建一个画布,以允许人们签署的东西。我创建了我的画布,它与鼠标工作得很好,但我需要知道如何在移动设备上做它。我尝试了几种方法(使用touchmove/touchstart),但是我不能让它工作。这是我在ES6中的对象画布(我不使用jQuery)。我不被允许使用任何库,我只想使用JS (我会在某个时候学习jQuery )。

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

发布于 2019-01-24 21:51:40

您可以使用事件

用于触控设备。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54348152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档