1

this function is called every 30ms

function draw(){
    context.clearRect(0, 0,canvas.width, canvas.height);

    context.drawImage(displayme, plot.position.x, plot.position.y, plot.width, plot.height);    
}

and some mouse events modify the displayme.src and plot position

but when the new src is being loaded, all the other javascript gets blocked and cant move the (previously loaded) image

i've also tried using a different Image object and assigning it to "displayme" but it didn't work

any help would be most welcome

1 Answer 1

3

AFAIK all browsers run javascript in a single thread. You can, using setTimeout() make it so this function returns immediately, but at the time that the function actually runs the browser won't be running any other javascript on the page. This simulates multithreading, but it shouldn't be mistaken for the real thing.

function draw(){
    // allows immediate return and "schedules" the anonymous function next
    setTimeout( function() {
        context.clearRect(0, 0,canvas.width, canvas.height);
        context.drawImage(displayme, plot.position.x, plot.position.y,
                                     plot.width, plot.height);
    },0);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

awesome it worked, thanks I still had to do the "placeholder" object var displayme = new Image(); var img = new Image(); img.onload = function(){ displayme.src = img.src; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.