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/calculateWebpLength.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/calculateWebpLength.js')
| -rw-r--r-- | webAO/utils/calculateWebpLength.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/webAO/utils/calculateWebpLength.js b/webAO/utils/calculateWebpLength.js new file mode 100644 index 0000000..1b422a0 --- /dev/null +++ b/webAO/utils/calculateWebpLength.js @@ -0,0 +1,22 @@ +const calculateWebpLength = (webpFile) => { + const d = new Uint8Array(webpFile); + // https://developers.google.com/speed/webp/docs/riff_container#animation + let duration = 0; + for (let i = 0; i < d.length; i++) { + // Find ANMF header (41 4E 4D 46) + if (d[i] === 0x41 + && d[i + 1] === 0x4E + && d[i + 2] === 0x4D + && d[i + 3] === 0x46) { + // Swap 5th and 6th bytes to get the delay per frame + const delay = (d[i + 21] << 8) | (d[i + 20] & 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; +}; + +export default calculateWebpLength; |
