-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Feature/GIF #43 #44
Open
EmilienLeroy
wants to merge
10
commits into
suchipi:master
Choose a base branch
from
EmilienLeroy:feature/gif-43
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/GIF #43 #44
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dd0a483
Create GIF component
EmilienLeroy 598aad4
Read gif using gifken
EmilienLeroy 89b5172
read a gif and add some options
EmilienLeroy b943af7
option to update the fps of the gif
EmilienLeroy 175eb64
Fixes trouble with compressed gifs
EmilienLeroy 4287271
Add currentFrameIndex, currentFrameComplexion and currentFrame proper…
EmilienLeroy 11caadf
Add doc and example for GIF component
EmilienLeroy aba743c
Update description of the GIF component
EmilienLeroy 79b3fcf
Adding Preloader to GIF component
EmilienLeroy 2639e62
Update Preloader import
EmilienLeroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { AnimationAPI, AnimationFrame } from './Animation'; | ||
import { useType } from '@hex-engine/core'; | ||
import gifken, { Gif } from 'gifken'; | ||
import Preloader from '../Preloader'; | ||
|
||
interface GIFInterface extends AnimationAPI<HTMLImageElement> { | ||
getGif(): Gif, | ||
drawCurrentFrame(context: CanvasRenderingContext2D, x?: number, y?: number): void; | ||
} | ||
|
||
/** | ||
* A component which enables you to play and manipulate gifs in Hex Engine. | ||
* @example | ||
* import someGifFile from "./your.gif"; | ||
* | ||
* export default function MyGif() { | ||
* useType(MyGif); | ||
* | ||
* const gif = useNewComponent(() => GIF({ | ||
* url: someGifFile, | ||
* width: 200, | ||
* height: 200, | ||
* fps: 20, | ||
* loop: true | ||
* })); | ||
* | ||
* gif.play() | ||
* | ||
* useDraw((context) => { | ||
* gif.drawCurrentFrame(context); | ||
* }); | ||
* } | ||
*/ | ||
export default function GIF(options: { | ||
url: string, | ||
width: number, | ||
height: number, | ||
fps?: number, | ||
loop?: boolean | ||
}): GIFInterface { | ||
useType(GIF); | ||
let gif: Gif = new Gif(); | ||
let frames: AnimationFrame<HTMLImageElement>[] = []; | ||
let play: boolean = false; | ||
let currentFrameIndex: number = 0; | ||
|
||
const loadPromise = load(options.url).then(async arrayBuffer => { | ||
gif = gifken.Gif.parse(arrayBuffer); | ||
frames = await getFrames({ | ||
gif, | ||
width: options.width, | ||
height: options.height, | ||
}) | ||
|
||
setInterval(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (frames.length - 1 > currentFrameIndex && play) { | ||
currentFrameIndex ++; | ||
} | ||
|
||
if(frames.length - 1 <= currentFrameIndex && options.loop && play) { | ||
currentFrameIndex = 0; | ||
} | ||
}, 1000 / (options.fps || 25)) | ||
}) | ||
|
||
Preloader.addTask(() => loadPromise); | ||
|
||
return { | ||
getGif() { | ||
return gif; | ||
}, | ||
drawCurrentFrame(context: CanvasRenderingContext2D, x: number = 0, y: number = 0) { | ||
if(frames.length !== 0) { | ||
context.drawImage(frames[currentFrameIndex].data, x, y); | ||
} | ||
}, | ||
frames: frames, | ||
loop: options.loop || false, | ||
get currentFrameIndex() { | ||
return currentFrameIndex; | ||
}, | ||
get currentFrame() { | ||
return frames[currentFrameIndex]; | ||
}, | ||
get currentFrameCompletion() { | ||
if(frames.length !== 0) { | ||
return currentFrameIndex / frames.length; | ||
} | ||
|
||
return 1; | ||
}, | ||
pause() { | ||
play = false; | ||
}, | ||
resume() { | ||
this.play(); | ||
}, | ||
play() { | ||
play = true; | ||
}, | ||
restart() { | ||
currentFrameIndex = 0; | ||
play = true; | ||
}, | ||
goToFrame(frameNumber: number) { | ||
currentFrameIndex = frameNumber; | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* Load a gif. | ||
* @param url - gif url | ||
*/ | ||
async function load(url: string) { | ||
return await fetch(url) | ||
.then((res) => res.arrayBuffer()); | ||
} | ||
|
||
/** | ||
* Get all frames of a gif. | ||
* For the moment, it must use a tmp canvas to get each frames due to a bug of gitken. | ||
* See this issues: https://github.com/aaharu/gifken/issues/22 | ||
*/ | ||
async function getFrames({ gif, width, height }: { | ||
gif: Gif, | ||
width?: number, | ||
height?: number, | ||
}): Promise<AnimationFrame<HTMLImageElement>[]> { | ||
const canvas: HTMLCanvasElement = document.createElement("canvas"); | ||
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D ; | ||
canvas.width = width || gif.width; | ||
canvas.height = height || gif.height; | ||
ctx.clearRect(0, 0, gif.width, gif.height); | ||
|
||
// get all frames as img | ||
const tmpImgs: HTMLImageElement[] = await Promise.all(gif.split(false).map((splited) => { | ||
return new Promise<HTMLImageElement>((resolve, reject) => { | ||
const img = new Image(width || gif.width, height || gif.height); | ||
img.onload = () => resolve(img); | ||
img.onerror = (e) => reject(e); | ||
img.src = gifken.GifPresenter.writeToDataUrl(splited.writeToArrayBuffer()); | ||
}) | ||
})); | ||
|
||
// draw frame to the tmp canvas and create the final frames. | ||
return tmpImgs.map((img: HTMLImageElement) => { | ||
ctx.drawImage(img, 0, 0); | ||
const newImg = new Image(); | ||
newImg.src = canvas.toDataURL("image/gif"); | ||
return new AnimationFrame<HTMLImageElement>(newImg, { | ||
duration: 0, | ||
}); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible for GIFs to have frames with different lengths; one frame can be long and the other can be short. Does gifken support this?
If you change the code to use the Animation component, you can specify the duration of each frame, and it will handle advancing to the next frame at the right time for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have find in the gifken documentation a property called delayCentiSeconds
I don't know if is exaclty what we want but i can test to set this property as duration when i will update the GIF component with the Animation component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, sounds good! You can use https://ezgif.com/maker to make a GIF that has frames with differing durations.