Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
Erase line drawn in canvas element with vanilla javascript?

I'm trying to implement an eraser functionality in my canvas, I'm able to draw with this function:

canvas = document.createElement("canvas")
context = canvas.getContext("2d")

function reposition(event) {
coords.x = event.clientX - canvas.getBoundingClientRect().left;
coords.y = event.clientY - canvas.getBoundingClientRect().top;
context.arc
}

function draw(event) {
context.beginPath();
context.lineWidth = "30";
context.lineCap = "square";
context.strokeStyle = "#999999"
context.moveTo(coords.x, coords.y);
reposition(event);
context.lineTo(coords.x, coords.y);
context.stroke();
}

Can I alter this in a way where instead of drawing a coloured stroke, it will erase the canvas to make the line drawn transparent?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So the thing that worked here was adding GlobalCompositeOperation = 'destination-out'; to my function

Final function:

function draw(event) {
context.beginPath();
GlobalCompositeOperation = 'destination-out';'
context.lineWidth = "30";
context.lineCap = "square";
context.strokeStyle = "#999999"
context.moveTo(coords.x, coords.y);
reposition(event);
context.lineTo(coords.x, coords.y);
context.stroke();
}```
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda