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

249
Views
Unexpected canvas behavior when using stroke style

I'm trying to clear one line by drawing another one on top of it on HTML canvas. But the second line becomes semi transparent for some unknown to me reason.

index.html

<!DOCTYPE html>
<head>
    <script src="scripts.js" type="text/javascript"></script>
</head>
<html>

    <body onload = "onload()">
        <canvas id = "canvas" style="border: solid 1px; width: 200px; height: 200px;"></canvas>
    </body>
</html>

scripts.js

var canvas,ctx;
const onload = () =>{
    canvas = document.querySelector("#canvas")
    ctx = canvas.getContext('2d')
    drawLine("#ff0000")
    drawLine("#ffffff")
}

const drawLine = (color) =>{
    ctx.beginPath()
    ctx.strokeStyle = color
    ctx.moveTo(0,0)
    ctx.lineTo(200,200)
    ctx.stroke()
    ctx.closePath()
}

var canvas,ctx;
const onload = () =>{
    canvas = document.querySelector("#canvas")
    ctx = canvas.getContext('2d')
    drawLine("#ff0000")
    drawLine("#ffffff")
}

const drawLine = (color) =>{
    ctx.beginPath()
    ctx.strokeStyle = color
    ctx.moveTo(0,0)
    ctx.lineTo(200,200)
    ctx.stroke()
    ctx.closePath()
}
<!DOCTYPE html>
<head>
    <script src="scripts.js" type="text/javascript"></script>
</head>
<html>

    <body onload = "onload()">
        <canvas id = "canvas" style="border: solid 1px; width: 200px; height: 200px;"></canvas>
    </body>
</html>

about 4 years ago · Juan Pablo Isaza
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!