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

93
Views
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 answers
Answer question

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 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!