Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:43+00:00 2026-06-15T12:53:43+00:00

I’m having a video copying pixels to a canvas and applying a real time

  • 0

I’m having a video copying pixels to a canvas and applying a real time bitmap filter using a web worker.

When using the web worker I can’t see the resulting change on the canvas (should be black ‘n white).

This is my code:
http://www.dev.createdbychristian.com/filter-test/

I can solve this by using an intermediate canvas, but I don’t really want to do that.

My code is loosely based on this example, same as mine just not using a web worker for the image filter processing.
http://techslides.com/demos/html5-video-canvas.html

What is wrong with my code?

NB: Requires a modern browser with support for Web Workers and HTML5 Video / canvas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:53:43+00:00Added an answer on June 15, 2026 at 12:53 pm

    This happens because draw() doesn’t wait for the return value of your WebWorker and uses requestAnimationFrame. If you want continuously filtered images you have to call draw after the filter has been applied:

    filterWorker.addEventListener('message', function(e) {
        ctx.putImageData(e.data, 0, 0);
        requestAnimationFrame(draw);
    }, false);
    

    However, if the filter takes to long it is very likely that draw will fill the canvas with colored data to early to notice the change (I was able to see the black ‘n white version with FF17 in your current version).

    What is wrong with my code?

    You’re using a facility for parallel computation in a scenario where the order of operations is important: you want to apply the filter on each image before it is shown. Let me quote MDN:

    About thread safety

    The Worker interface spawns real OS-level threads, and mindful programmers may be concerned that concurrency can cause “interesting” effects in your code if you aren’t careful.

    However, since web workers have carefully controlled communication points with other threads, it’s actually very hard to cause concurrency problems. There’s no access to non-threadsafe components or the DOM. And you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code.

    And as it is with real OS-level threads no assumptions in the order of execution can be made unless you use some blocking mechanism, but this isn’t included in JavaScript. So you’ll need to draw and manipulate the image in the same thread.

    Further explanation

    Sequential case

    Have a look at the following picture:

    Sequential filtering

    This is what basically happens in the techdemo. draw() copies the video and immediately filters the image:

    function draw(v,c,w,h,filter) {
        if(v.paused || v.ended) return false;
        c.drawImage(v,0,0,w,h);
        if (typeof filter === 'undefined') {
            // variable is undefined
        } else {
            var idata = c.getImageData(0,0,w,h);
            newdata = filterdata(idata,filter);
            c.putImageData(newdata,0,0);
        }
        setTimeout(draw,20,v,c,w,h,filter);
    }
    

    As you can see there is no gap between the the call of drawImage the following filter. The resulting picture is shown for approximately 20 milliseconds. Even if the filter would take 1ms the user likely won’t notice this.

    Parallel case

    Now it gets a little bit more complicated. Blackened text boxes are methods which modify the canvas directly, white boxes indicate a triggered event or registered timeout, and black boxes indicate the execution of the timeout or handling of the event:

    Parallel case

    Now, what happens? draw is still called approximately every 20ms and copies the video. Then it posts a message to the worker. However, it isn’t stated when this message is going to be sent. It could be placed in the worker’s event loop immediately (as shown in the picture), or in the event loop of "main" and dispatched later (which is more likely).

    This process takes very, very long in comparison to the sequential method. Keep in mind that you also need to send the processed data back and that both filter and filterWorker‘s event listener may be triggered at a later time than you originally expected. But even if you actually process the data and display it, a following call to draw will destroy all your hard work.

    Solution

    Either don’t use a web worker, or use a hidden canvas as source for the worker.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.