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

153
Views
Recurring image function in canvas fails after one call

I'm trying to build an interactive blackjack table with chips and play buttons. Instead of drawing the chips individually, I wanted to draw a circle, put the image of the chip inside, and crop the rest of the picture.

function getMousePos(canvas, event) {
    let button = canvas.getBoundingClientRect();
    return {
        x: event.clientX - button.left,
        y: event.clientY - button.top
    };
}

function isInside (pos, button) {
    return pos.x > button.x && pos.x < button.x+button.width && pos.y < button.y+button.height && pos.y > button.y
}


function draw() {
    const canvas = document.getElementById('bjGame');
    const width = canvas.width = window.innerWidth-40;
    const height = canvas.height = window.innerHeight-40;
    if (canvas.getContext) {
        const ctx = canvas.getContext('2d');
        let chip1 = new Image();
        let chip5 = new Image();

        //Background
        ctx.fillStyle = "rgba(29,129,36,0.71)";
        ctx.fillRect(0, 0, width, height);

        //This works-
        createChip(ctx, chip1, 300, 300, "images/chip1.png");

        //But once I add this line of code it completely breaks
        createChip(ctx, chip5, 100, 100, "images/chip5.png")


    }
}

function createChip(ctx, chip, x, y, imgSrc) {
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.arc(x, y, 40, 0, 2*Math.PI);
    ctx.clip();

    chip.addEventListener('load', ()=> {
        ctx.drawImage(chip, 30, 11, 100, 100, x-40, y-40, 80, 80);
    }, false);

    chip.src = imgSrc;
}

I've been trying to avoid making the same lines of code for the 8 different required chips but the function has a one-time-use for some reason.

jsfiddle

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!