绘图已放大 canvas HTML/JavaScript

Drawing is zoomed in canvas HTML/JavaScript

目前我正在尝试在 canvas 上绘图...但问题是绘图被放大了。基本上 beginPath 没有跟随光标。当我在 css 中更改 canvas 宽度和高度时,就会发生这种情况。在此之前,大小是在 canvas 标签中指定的,没有任何像素值。但是在 css 中我有 vh% 值。有什么办法让它再次正常运行。这是我的 html 代码:

<canvas id="canvas" style="background-color: white; border:2px solid;"></canvas>

这是我的 css 代码:

#canvas {
    width: 100%;
    height: 90vh;
    cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>✍️</text></svg>") 0 20, auto;
}

最后但同样重要的是,这是我的 Js 代码(我是如何在 canvas 上写的):

const points = [];
const canvas = document.getElementById("canvas");
const mouse = { x: 0, y: 0, button: false }
const ctx = canvas.getContext("2d");
canvas.addEventListener("mousemove", mouseEvents);
canvas.addEventListener("mousedown", mouseEvents);
canvas.addEventListener("mouseup", mouseEvents);
var x = "black",
    y = 5 / 2;

function mouseEvents(e) {
    mouse.x = e.pageX - 1;
    mouse.y = e.pageY - 1;
    const lb = mouse.button;
    mouse.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
    if (mouse.button) {
        if (!lb) { points.length = 0 }
            points.push({ x: mouse.x, y: mouse.y });
            drawPoints();
        }
    }

function drawPoints() {
    ctx.strokeStyle = x;
    ctx.lineWidth = y;
    ctx.lineCap = "round";
    ctx.lineJoin = "round";
    ctx.beginPath();
    if (mode == "pen") {
        ctx.globalCompositeOperation = "source-over";
        for (const p of points) { ctx.lineTo(p.x, p.y); }
        ctx.stroke();
    } else {
        ctx.globalCompositeOperation = "destination-out";
        ctx.arc(mouse.x, mouse.y, 8, 0, Math.PI * 2, false);
        ctx.stroke();
        ctx.fill();
    }
}

function color(obj) {
    switch (obj.id) {
        case "green":
            x = "green";
            break;
        case "blue":
            x = "blue";
            break;
        case "red":
            x = "red";
            break;
        case "yellow":
            x = "yellow";
            break;
        case "orange":
            x = "orange";
            break;
        case "black":
            x = "black";
            break;
        case "white":
            x = "white";
            break;
        case "grey":
            x = "grey";
            break;
    }
    document.getElementById("current_color").style.background = x;
}

function erase() {
    var m = confirm("Want to clear");
    if (m) {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        document.getElementById("canvasimg").style.display = "none";
    }
}

canvas.addEventListener("touchstart", function (e) {
    mousePos = getTouchPos(canvas, e);
    var touch = e.touches[0];
    var mouseEvent = new MouseEvent("mousedown", {
        clientX: touch.clientX,
        clientY: touch.clientY
    });
    canvas.dispatchEvent(mouseEvent);
        }, false);
canvas.addEventListener("touchend", function (e) {
    var mouseEvent = new MouseEvent("mouseup", {});
    canvas.dispatchEvent(mouseEvent);
}, false);
canvas.addEventListener("touchmove", function (e) {
    var touch = e.touches[0];
    var mouseEvent = new MouseEvent("mousemove", {
        clientX: touch.clientX,
        clientY: touch.clientY
    });
    canvas.dispatchEvent(mouseEvent);
}, false);

// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
    var rect = canvasDom.getBoundingClientRect();
    return {
        x: touchEvent.touches[0].clientX - rect.left,
        y: touchEvent.touches[0].clientY - rect.top
    };
}

document.getElementById("canvas").onwheel = function (event) {
    event.preventDefault();
};

document.getElementById("canvas").onmousewheel = function (event) {
    event.preventDefault();
};

canvas.addEventListener("touchstart", function (event) { event.preventDefault() })
canvas.addEventListener("touchmove", function (event) { event.preventDefault() })
canvas.addEventListener("touchend", function (event) { event.preventDefault() })
canvas.addEventListener("touchcancel", function (event) { event.preventDefault() })

var mode = "pen";
$("#pen").click(function () {
    mode = "pen";
    $('canvas').css('cursor', "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>✍</text></svg>\") 0 20, auto");
});
$("#eraser").click(function () {
    mode = "eraser"
    $('canvas').css('cursor', "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:18px;'><text y='50%'></text></svg>\") 0 15, auto");
});

不要直接为您的 canvas 使用相对单位。将这些相对单位(% 和 vh)应用于 canvas 的容器,然后从 javascript 调整 canvas.

的大小

const container = document.getElementById("app");

(function () {
  window.addEventListener("resize", resizeCanvas, false);

  function resizeCanvas() {
    const canvas = document.getElementById("canvas");
    const context = canvas.getContext("2d");
    canvas.width = container.clientWidth;
    canvas.height = container.clientHeight;
    //DRAW ALL
    context.fillRect(0,0,canvas.width, canvas.height)
  }
  resizeCanvas();
})();
#app {
  width: 50vw;
  height: 50vh;
}
<div id="app">
   <canvas id="canvas" />
</div>