簡體   English   中英

將鼠標懸停在 JavaScript Canvas 中的按鈕上的問題

[英]Issue with hovering over a button in JavaScript Canvas

```
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Paint, Inc.</title>
  </head>

  <body>
    <div id="sideNav">
      <a href="#"></a>
    </div>
    <canvas id="canvas"></canvas>

    <script src="canvas.js"></script>
    <button id="clear" title="Clear">X</button>
    <section id="leftBumper"></section>
    
    <section id="colorChoice">
        <input id="color" type="color" value="#000000" />
        <label id="colorLabel" for="color">Color</label>
    </section>

    <section id="strokeWeightChoice">
        <input id="strokeWeight" type="range" min="1" max="51" step="5" value="1" list="tickmarks">
        <label for="strokeWeight">Thickness</label>
        <datalist id="tickmarks">
            <option value="1"></option>
            <option value="6"></option>
            <option value="11"></option>
            <option value="16"></option>
            <option value="21"></option>
            <option value="26"></option>
            <option value="31"></option>
            <option value="36"></option>
            <option value="41"></option>
            <option value="46"></option>
            <option value="51"></option>
        </datalist>
    </section>
  </body>
</html>
```
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

#canvas {
    border: 0.0001px solid white;
}

html {
    overflow: hidden;
}

#clear {
    position: absolute;
    top: 0;
    left: 0;
    bottom: -10px;
    width: 30px;
    background: rgba(70, 70, 70, 0.32);
    border: rgba(70, 70, 70, 0.32);
    border-width: 5px;
    font-weight: bold;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 28px;
    color: red;
}

#clear:hover {
    transition: 0.4s;
    background: rgba(20, 20, 20, 0.4);
    cursor: pointer;
    font-size: 32px;
}

#clear:focus {
    outline: 0;
}

#colorChoice {
    position: absolute;
    bottom: 0.5rem;
    right: 50%;
    transform: translateX(50%);
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
    font-weight: bold;
}

#colorChoice:hover {
    cursor: pointer;
}

#strokeWeightChoice {
    position: absolute;
    bottom: 3rem;
    right: 50%;
    transform: translateX(50%);
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
    font-weight: bold;
}

#strokeWeight {
    width: 200px;
}
```
window.addEventListener("load", () => {
    const canvas = document.querySelector("#canvas");
    const ctx = canvas.getContext("2d");
    const color = document.querySelector("#color");
    const strokeWeight = document.querySelector("#strokeWeight");
    //variables
    const clearButton = document.querySelector("#clear");
  
    let painting = false;
  
    function startPosition(e) {
      painting = true;
      draw(e);
    }
    function finishedPosition() {
      painting = false;
      ctx.beginPath();
    }
  
    function draw(e) {
      if (!painting) return;
      ctx.lineCap = "round";
      ctx.lineTo(e.clientX, e.clientY);
      ctx.stroke();
      ctx.beginPath();
      ctx.moveTo(e.clientX, e.clientY);
    }
  
    function changeColor(e) {
      const color = e.target.value;
      ctx.strokeStyle = color;
    }

    function changeStrokeWeight(e) {
        const strokeWeight = e.target.value;
        ctx.lineWidth = strokeWeight;
    }

  
    //Event listeners
    canvas.addEventListener("mousedown", startPosition);
    canvas.addEventListener("mouseup", finishedPosition);
    canvas.addEventListener("mousemove", draw);
  
    color.addEventListener("input", changeColor);
    strokeWeight.addEventListener("input", changeStrokeWeight);
  
    //Buttons
    clearButton.addEventListener("click", clearCanvas);
  
    function clearCanvas() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
});
  
window.addEventListener("resize", resizeCanvas);
function resizeCanvas() {
    //Resizing
    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;
}
resizeCanvas();

在我的 canvas 上,每當我將鼠標懸停在按鈕上時進行繪制,然后將我的 cursor 移回 canvas 都會導致我在不按下的情況下進行繪制,直到我再次按下。 有人可以告訴我如何解決這個故障。 這個故障發生在我的清除按鈕、厚度 slider 和我的換色器上。 另外我如何添加一些東西來停止在清除按鈕上繪圖。

讓我知道對 javascript 的以下更改是否對您有用 -

當鼠標移動事件調用 draw() 時,它不會再檢查繪畫是否設置為 true,而是會檢查鼠標事件的狀態,以查看當您關閉 go 時它們是否以這種方式按住左鍵canvas 和 hover 在其中一個按鈕上,然后回到 canvas 上,它將檢查您是否按住左鍵並繪制是否為真

如果有幫助,請在此處撥弄

您可以在此處查看有關哪些屬性的更多信息

    window.addEventListener("load", () => {
    const canvas = document.querySelector("#canvas");
    const ctx = canvas.getContext("2d");
    const color = document.querySelector("#color");
    const strokeWeight = document.querySelector("#strokeWeight");
    //variables
    const clearButton = document.querySelector("#clear");
  
    let painting = false;
  
    function startPosition(e) {
      painting = true;
      draw(e);
    }
    function finishedPosition() {
      painting = false;
      ctx.beginPath();
    }
  
    function draw(e) {
      //if (!painting) return;
      if(e.which == 1){
        ctx.lineCap = "round";
        ctx.lineTo(e.clientX, e.clientY);
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(e.clientX, e.clientY);
      }else{
      finishedPosition();
      }
      
    }
  
    function changeColor(e) {
      const color = e.target.value;
      ctx.strokeStyle = color;
    }

    function changeStrokeWeight(e) {
        const strokeWeight = e.target.value;
        ctx.lineWidth = strokeWeight;
    }

  
    //Event listeners
    canvas.addEventListener("mousedown", startPosition);
    canvas.addEventListener("mouseup", finishedPosition);
    canvas.addEventListener("mousemove", draw);
  
    color.addEventListener("input", changeColor);
    strokeWeight.addEventListener("input", changeStrokeWeight);
  
    //Buttons
    clearButton.addEventListener("click", clearCanvas);
  
    function clearCanvas() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
});
  
window.addEventListener("resize", resizeCanvas);
function resizeCanvas() {
    //Resizing
    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;
}
resizeCanvas();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM