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

added programmatic dissection of cumulatively rendered gifs #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pnkfluffy
Copy link

Removed cumulative option. I deleted the stuff that had to do with your cumulative rendering and just added the code from TrevorSundberg. Tested it on a few cumulative and non-cumulative gifs and it worked with no issue.

Copy link
Owner

@benwiley4000 benwiley4000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I didn't fully understand what this algorithm entailed.. it strikes me as expensive since it involves repeated draws and subsequent reads from a canvas 2d context.. and it also will only work in a web browser. gifFrames is designed to work in Node.js as well. A few main thoughts:

  1. I think we can do this without using a canvas. We can probably just modify the code that you deleted which updates the frame object, and we already have access to framesInfo there so we can check the disposal method, etc. before we send anything to savePixels.
  2. I see that we can remove the cumulative option, but we should make sure not to impose a significant performance penalty on users who have GIFs without fancy transparency stuff that causes problems. By using the frame object instead of a canvas I think we can do that.
  3. Check this comment which specifies the logic for what to do depending on the disposal method specified. I think it's basically the same as what is implemented here, but it could be helpful for reimplementing without canvas.
  4. I think the new algorithm assumes all frames are collected. What we ought to do is collect all frames up to the highest frame index, and return those that are requested.

@@ -114,6 +132,9 @@ function gifFrames (options, callback) {
resolve(frameData);
});

// programatically fixes
promise.then((frameData) => {return (renderCumulativeFrames(frameData))});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for consistency I'd like to stick to ES5, so no arrow functions.

@@ -114,6 +132,9 @@ function gifFrames (options, callback) {
resolve(frameData);
});

// programatically fixes
promise.then((frameData) => {return (renderCumulativeFrames(frameData))});

return promise;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would need to return the result of promise.then above, not the initial promise value. I think the only reason your code works is because that callback gets queued first, and it mutates frameData. But if renderCumulativeFrames returned a new frameData object, the code wouldn't work. So I think return promise.then(...) would be better.

current.width = firstFrameCanvas.width;
current.height = firstFrameCanvas.height;

for (const frame of frameData) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can use ES5 here (e.g. forEach) that would be better. thanks

previousContext.drawImage(current, 0, 0);

// Draw the current frame to the cumulative buffer.
const canvas = frame.getImage();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var please (here and elsewhere)... thanks!

currentContext.clearRect(0, 0, current.width, current.height);
currentContext.drawImage(previous, 0, 0);
}
frame.getImage = () => canvas;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function() { } please... thanks!

const {disposal} = frameInfo;
// If the disposal method is clear to the background color, then clear the canvas.
if (disposal === 2) {
currentContext.clearRect(frameInfo.x, frameInfo.y, frameInfo.width, frameInfo.height);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this on the heart GIF from the other issue thread (https://media0.giphy.com/media/3HHvQSvdCMYD2bY1X2/giphy.gif)? Here I noticed that the reason for that issue was because the background color was red. I would think we would need to be reading the background color from the framesInfo.

@pnkfluffy
Copy link
Author

I apologize for being so clumsy with syntax standards, this was my first pull request I've made on an unfamiliar repository. I recently got some work that will keep me busy for these next few days, but I'll try to look over all of it and try to submit a more optimized and conforming pull request within this week!

@benwiley4000
Copy link
Owner

No worries about the formatting! I definitely appreciate the PR, and my norm is to leave comments. Doesn't mean anything bad 😄.

BTW I've been thinking about my earlier comment:

  1. I think the new algorithm assumes all frames are collected. What we ought to do is collect all frames up to the highest frame index, and return those that are requested.

Actually what could be more efficient and skip unneeded computation, is to collect the frames beginning by iterating in reverse order. I actually have something in my head so I might suggest a code update to the PR later today, if you wouldn't mind.

@pnkfluffy
Copy link
Author

pnkfluffy commented Apr 20, 2020 via email

@benwiley4000
Copy link
Owner

@pnkfluffy cool! The concept for your app seems cool from what I can see, but I wasn't able to try because the signup confirmation email got flagged as spam by Gmail and it won't let me click the link. Let me know if you get that sorted out.. thanks for sharing!

@benwiley4000
Copy link
Owner

benwiley4000 commented Apr 21, 2020

@pnkfluffy just kidding, I was able to get it to work in the desktop web version of gmail by bypassing the spam filter, but I got stuck in the app on this screen (there's no prompt to access the camera, and I don't see any JS console errors):

Screen Shot 2020-04-21 at 5 54 51 PM

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

Successfully merging this pull request may close these issues.

None yet

2 participants