Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated Gif comes out black w/ Web Workers #24

Closed
jodyheavener opened this issue Mar 29, 2015 · 6 comments
Closed

Animated Gif comes out black w/ Web Workers #24

jodyheavener opened this issue Mar 29, 2015 · 6 comments

Comments

@jodyheavener
Copy link

I'm trying to use Web Workers to handle the Gif creation, and while I could get animated Gif generation working without using the worker, once I moved everything over to the worker I can only get a black image with the specified dimensions as the output.

In my main JS:

this.gifWorker = new Worker("scripts/worker.js");

... context setup ...

toggleSnapshotRecord() {
  this.isRecording = !this.isRecording;
  $(document.body).toggleClass("is-recording");

  let interval = (this.captureSecondsDelay - 1) * 1000;
  let snapCount = 0;

  if (this.isRecording) {
    this.snapshotInterval = setInterval(() => {
      // Capture context image data at a specified interval
      // until the desired frame count is met
      this.snapshotFrames.push(this.canvasContext.getImageData(0, 0, this.outputWidth, this.outputHeight));
      snapCount = snapCount + 1;
      if (snapCount === this.captureFramesNumber)
        return this.toggleSnapshotRecord();
    }, interval);
  } else {
    clearInterval(this.snapshotInterval);

    // Send the frames and the canvas dimensions
    // to our web worker
    this.gifWorker.postMessage({
      size: [this.outputWidth, this.outputHeight],
      frames: this.snapshotFrames
    });

    this.gifWorker.onmessage = function(event) {
      console.log(event.data);
    }
  };
};

In my Web Worker:

self.addEventListener("message", function(event) {
  var data = event.data;
  var encoder = new GIFEncoder();

  encoder.setRepeat(0);
  encoder.setDelay(300);
  encoder.setSize(data.size[0], data.size[1]);
  encoder.start();
  encoder.addFrame(data.frames, true);
  encoder.finish();

  self.postMessage("data:image/gif;base64," + encode64(encoder.stream().getData()));
}, false);

What would I be missing here? Any help is much appreciated!

@jodyheavener
Copy link
Author

Also tried posting to the web worker on each interval:

Main JS

toggleSnapshotRecord() {
  this.isRecording = !this.isRecording;
  $(document.body).toggleClass("is-recording");

  let interval = (this.captureSecondsDelay - 1) * 1000;
  let snapCount = 0;

  if (this.isRecording) {
    this.snapshotInterval = setInterval(() => {
      let messageData = {
        size: [this.outputWidth, this.outputHeight],
        frame: this.canvasContext.getImageData(0, 0, this.outputWidth, this.outputHeight)
      };

      if (snapCount === 0)
        messageData["start"] = true;

      if (snapCount === this.captureFramesNumber - 1)
        messageData["finish"] = true;

      this.gifWorker.postMessage(messageData);

      snapCount = snapCount + 1;
      if (snapCount === this.captureFramesNumber)
        return this.toggleSnapshotRecord();
    }, interval);
  } else {
    clearInterval(this.snapshotInterval);

    this.gifWorker.onmessage = function(event) {
      console.log(event.data);
    };
  };
};

Web Worker

var encoder = new GIFEncoder();
    encoder.setRepeat(0);
    encoder.setDelay(300);

self.addEventListener("message", function(event) {
  var data = event.data;

  if (data.start) {
    encoder.start();
  } else {
    encoder.setProperties(true, true);
  }

  encoder.setSize(data.size[0], data.size[1]);
  encoder.addFrame(data.frame, true);

  if (data.finish) {
    encoder.finish();
    self.postMessage("data:image/gif;base64," + encode64(encoder.stream().getData()));
  }
}, false);

@Joncom
Copy link
Collaborator

Joncom commented Mar 29, 2015

Does the black output GIF at least appear to have the correct number of frames?

@jodyheavener
Copy link
Author

Negative. It looks to be just the one frame (upon inspection in Apple Preview).

@jodyheavener
Copy link
Author

Got it. In passing data.frame to encoder.addFrame() I was passing an ImageData object to it, when I should have been passing the ImageData's data – all in all I just had to change it to encoder.addFrame(data.frame.data, true).

@jodyheavener
Copy link
Author

download

@claybudin
Copy link

Why are you calling:

encoder.setProperties(true, true);

? It seems like those are internal parameters managed by the encoder and don't need to be changed.

Also it looks like the setSize() call can be moved into the if (data.start) { ... } area.

Clay Budin
NYC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants