Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
El cursor de dibujo HTML5 Canvas está fuera de su posición real

Estoy tratando de crear un dibujo de lienzo HTML, pero el cursor de dibujo está fuera de su posición real.

Aquí está todo el código JavaScript/CSS/HTML: HTML5 Canvas drawing

Creo que el problema está en el código JavaScript, pero no puedo solucionarlo.

Aquí está el código JavaScript:

 let canvas = document.getElementById("canvas") canvas.height = window.innerHeight canvas.width = window.innerWidth let ctx = canvas.getContext("2d") ctx.lineWidth = 5 let prevX = null let prevY = null let draw = false let clrs = document.querySelectorAll(".clr") clrs = Array.from(clrs) clrs.forEach(clr => { clr.addEventListener("click", () => { ctx.strokeStyle = clr.dataset.clr }) }) let clearBtn = document.querySelector(".clear") clearBtn.addEventListener("click", () => { ctx.clearRect(0, 0, canvas.width, canvas.height) }) window.addEventListener("mousedown", (e) => draw = true) window.addEventListener("mouseup", (e) => draw = false) window.addEventListener("mousemove", function(e){ if(prevX == null || prevY == null || !draw){ prevX = e.clientX prevY = e.clientY return } let mouseX = e.clientX let mouseY = e.clientY ctx.beginPath() ctx.moveTo(prevX, prevY) ctx.lineTo(mouseX, mouseY) ctx.stroke() prevX = e.clientX prevY = e.clientY })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Establezca la altura y el ancho en su canvas en el html en lugar de CSS.

Ha establecido el tamaño de su lienzo en 100x100 : <canvas id="canvas" width="100" height="100"></canvas> . Entonces, cuando establece el tamaño en CSS, efectivamente "estira" el lienzo a ese tamaño.

Cambiarlo a <canvas id="canvas" width="1600" height="700"></canvas> y eliminar el height y el width en su CSS hará el truco.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!