diff options
| author | stonedDiscord <Tukz@gmx.de> | 2022-03-09 07:34:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-09 07:34:56 +0100 |
| commit | 91c3769a78e59924a73db6c759844a9026e8da00 (patch) | |
| tree | 64ce4ac3e786b31a935be40438d5dc831121f055 /webAO/utils/calculateGifLength.js | |
| parent | 63a282481d033640a87f97b74f31136410c93717 (diff) | |
| parent | cb6a2ddb36d27abd12a6d0b9aa493194d4c242a2 (diff) | |
Merge pull request #100 from caleb-mabry/webp-support
WebAO Partial Image Fixes
Diffstat (limited to 'webAO/utils/calculateGifLength.js')
| -rw-r--r-- | webAO/utils/calculateGifLength.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/webAO/utils/calculateGifLength.js b/webAO/utils/calculateGifLength.js new file mode 100644 index 0000000..1df0ba9 --- /dev/null +++ b/webAO/utils/calculateGifLength.js @@ -0,0 +1,27 @@ +/** + * Adds up the frame delays to find out how long a GIF is + * I totally didn't steal this + * @param {data} gifFile the GIF data + */ +const calculateGifLength = (gifFile) => { + const d = new Uint8Array(gifFile); + // Thanks to http://justinsomnia.org/2006/10/gif-animation-duration-calculation/ + // And http://www.w3.org/Graphics/GIF/spec-gif89a.txt + let duration = 0; + for (let i = 0; i < d.length; i++) { + // Find a Graphic Control Extension hex(21F904__ ____ __00) + if (d[i] === 0x21 + && d[i + 1] === 0xF9 + && d[i + 2] === 0x04 + && d[i + 7] === 0x00) { + // Swap 5th and 6th bytes to get the delay per frame + const delay = (d[i + 5] << 8) | (d[i + 4] & 0xFF); + + // Should be aware browsers have a minimum frame delay + // e.g. 6ms for IE, 2ms modern browsers (50fps) + duration += delay < 2 ? 10 : delay; + } + } + return duration * 10; +}; +export default calculateGifLength; |
