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

enable per frame delay #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const encoder = new GIFEncoder(720, 480, 'neuquant', true, 20)
| Method | Parameter | Description | Notes |
| :------------------: | :--------------: | :-------------------------------------: | :--------------------------------------------------------: |
| `start` | n/a | Starts the encoder | n/a |
| `addFrame` | `Canvas Context` | Adds a frame to the GIF | n/a |
| `addFrame` | `Canvas Context`, _delay_ | Adds a frame to the GIF | `delay` (milliseconds) param is optional. Fallbacks to global delay if none given |
| `setDelay` | number | Number of milliseconds to display frame | Can be set once or per frame |
| `setFramesPerSecond` | number | Number of frames per second to display | Another way to set delay |
| `setQuality` | number 1-30 | Neuquant quality | 1 is best/slowest |
Expand Down
8 changes: 4 additions & 4 deletions src/GIFEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GIFEncoder extends EventEmitter {
this.readStreams = []
}

addFrame(input) {
addFrame(input, delay) {
if (input && input.getImageData) {
this.image = input.getImageData(0, 0, this.width, this.height).data
} else {
Expand All @@ -115,7 +115,7 @@ class GIFEncoder extends EventEmitter {
}
}

this.writeGraphicCtrlExt()
this.writeGraphicCtrlExt(delay)
this.writeImageDesc()
if (!this.firstFrame) {
this.writePalette()
Expand Down Expand Up @@ -322,7 +322,7 @@ class GIFEncoder extends EventEmitter {
this.out.writeByte(0)
}

writeGraphicCtrlExt() {
writeGraphicCtrlExt(delay) {
this.out.writeByte(0x21)
this.out.writeByte(0xf9)
this.out.writeByte(4)
Expand All @@ -343,7 +343,7 @@ class GIFEncoder extends EventEmitter {

this.out.writeByte(0 | disp | 0 | transp)

this.writeShort(this.delay)
this.writeShort(!isNaN(delay) ? delay : this.delay)
this.out.writeByte(this.transIndex)
this.out.writeByte(0)
}
Expand Down