Skip to content

Commit

Permalink
fix: decode of disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Dec 25, 2024
1 parent a592e61 commit 35c8241
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 350 deletions.
115 changes: 115 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Playground</title>
<style>
canvas {
margin: 8px;
width: 200px;
}

img {
display: block;
margin: 8px;
width: 200px;
}
</style>
</head>

<button>ReEncode</button>

<img src="/test/assets/test10.gif" />

<body>
<script type="module" async>
import { Encoder, decode, decodeFrames } from './src'
/* eslint-disable no-console */
const el = document.querySelector('img')
const url = el.src

let width = 0
let height = 0
let frames = []
if (url.startsWith('data:image/svg+xml;')) {
width = el.naturalWidth
height = el.naturalHeight
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
ctx.drawImage(el, 0, 0)
const frame = ctx.getImageData(0, 0, width, height)
frames.push(frame)
} else {
const data = await fetch(url).then((res) => res.arrayBuffer())

console.log('raw size', data.byteLength / 1024)

console.time('decode')
const gif = decode(data)
console.timeEnd('decode')

console.log(gif)
console.time('decode frames')
frames = await decodeFrames(data, {
// workerUrl,
})
console.timeEnd('decode frames')
width = gif.width
height = gif.height
}

const encoder = new Encoder({
debug: true,
width,
height,
maxColors: 255,
// workerUrl,
})

for (let len = frames.length, i = 0; i < len; i++) {
const frame = frames[i]
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
canvas.style.width = '60px'
canvas.style.border = '1px solid grey'
canvas.getContext('2d').putImageData(
new ImageData(frame.data, frame.width, frame.height),
0, 0,
)
document.body.append(canvas)
}

document.querySelector('button').onclick = async () => {
console.time('encode')
for (let len = frames.length, i = 0; i < len; i++) {
const frame = frames[i]
await encoder.encode({
delay: frame.delay,
data: frame.data,
})
}
console.timeEnd('encode')

console.time('flush')
const blob = await encoder.flush('blob')
console.timeEnd('flush')

console.log('encode size', blob.size / 1024)

const img = new Image()
img.src = URL.createObjectURL(blob)
document.body.append(img)

blob.arrayBuffer().then(async buffer => {
console.log(decode(buffer))
await decodeFrames(buffer)
})

console.log(encoder)
}
</script>
</body>
</html>
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,17 @@
"modern-palette": "^2.0.0"
},
"devDependencies": {
"@antfu/eslint-config": "^3.7.1",
"@types/dom-webcodecs": "^0.1.7",
"@types/node": "^22.6.1",
"bumpp": "^9.8.1",
"@antfu/eslint-config": "^3.12.1",
"@types/node": "^22.10.2",
"bumpp": "^9.9.2",
"conventional-changelog-cli": "^5.0.0",
"eslint": "^9.14.0",
"lint-staged": "^15.2.10",
"eslint": "^9.17.0",
"lint-staged": "^15.2.11",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.7.2",
"unbuild": "^3.0.1",
"vite": "^5.4.10",
"vitest": "^2.1.4"
"vite": "^6.0.5",
"vitest": "^2.1.8"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
Loading

0 comments on commit 35c8241

Please sign in to comment.