WIP
This commit is contained in:
@@ -265,6 +265,10 @@ class Board {
|
|||||||
return this.canvas.toDataURL();
|
return this.canvas.toDataURL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toImageData() {
|
||||||
|
return this.ctx.getImageData(0, 0, this.width, this.height).data;
|
||||||
|
}
|
||||||
|
|
||||||
toImgElement() {
|
toImgElement() {
|
||||||
const dataUrl = this.toImgUrl();
|
const dataUrl = this.toImgUrl();
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,14 @@ import GIFLib from "gif.js";
|
|||||||
|
|
||||||
class GIF {
|
class GIF {
|
||||||
private gif: GIFLib;
|
private gif: GIFLib;
|
||||||
|
private frameTime: number;
|
||||||
|
|
||||||
constructor(width: number, height: number, loop: boolean) {
|
constructor(
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
loop: boolean,
|
||||||
|
frameTime: number = 1000
|
||||||
|
) {
|
||||||
this.gif = new GIFLib({
|
this.gif = new GIFLib({
|
||||||
workers: 2,
|
workers: 2,
|
||||||
quality: 10,
|
quality: 10,
|
||||||
@@ -11,6 +17,7 @@ class GIF {
|
|||||||
height,
|
height,
|
||||||
repeat: loop ? 0 : -1,
|
repeat: loop ? 0 : -1,
|
||||||
});
|
});
|
||||||
|
this.frameTime = frameTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
add(
|
add(
|
||||||
@@ -19,9 +26,10 @@ class GIF {
|
|||||||
| CanvasRenderingContext2D
|
| CanvasRenderingContext2D
|
||||||
| WebGLRenderingContext
|
| WebGLRenderingContext
|
||||||
| ImageData,
|
| ImageData,
|
||||||
delay: number
|
|
||||||
|
frames: number
|
||||||
) {
|
) {
|
||||||
this.gif.addFrame(frame, { delay });
|
this.gif.addFrame(frame, { delay: frames * this.frameTime });
|
||||||
}
|
}
|
||||||
|
|
||||||
render(): Promise<File> {
|
render(): Promise<File> {
|
||||||
|
|||||||
@@ -3,35 +3,54 @@ import * as HME from "h264-mp4-encoder";
|
|||||||
class MP4 {
|
class MP4 {
|
||||||
private hme: Promise<HME.H264MP4Encoder>;
|
private hme: Promise<HME.H264MP4Encoder>;
|
||||||
private encoder: HME.H264MP4Encoder | null = null;
|
private encoder: HME.H264MP4Encoder | null = null;
|
||||||
|
private frameTime: number;
|
||||||
|
|
||||||
constructor(width: number, height: number) {
|
constructor(
|
||||||
|
private width: number,
|
||||||
|
private height: number,
|
||||||
|
frameTime: number = 1000
|
||||||
|
) {
|
||||||
this.hme = HME.createH264MP4Encoder();
|
this.hme = HME.createH264MP4Encoder();
|
||||||
this.setup(width, height);
|
this.frameTime = frameTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setup(width: number, height: number) {
|
async setup(width: number, height: number) {
|
||||||
this.encoder = await this.hme;
|
this.encoder = await this.hme;
|
||||||
this.encoder.width = width;
|
this.encoder.width = width;
|
||||||
this.encoder.height = height;
|
this.encoder.height = height;
|
||||||
|
this.encoder.frameRate = 1000 / this.frameTime;
|
||||||
|
this.encoder.quantizationParameter = 10;
|
||||||
this.encoder.initialize();
|
this.encoder.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
// add(frame: CanvasImageSource | string, delay: number) {
|
async add(data: Uint8ClampedArray, frames: number) {
|
||||||
// // this.encoder?.addFrameRgba()
|
if (this.encoder === null) {
|
||||||
// }
|
await this.setup(this.width, this.height);
|
||||||
|
}
|
||||||
|
|
||||||
// async render(): Promise<File> {
|
while (frames--) {
|
||||||
// const blob = await this.video.complete();
|
this.encoder?.addFrameRgba(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// const timestamp = Date.now();
|
async render(): Promise<File> {
|
||||||
|
this.encoder?.finalize();
|
||||||
|
|
||||||
// const file = new File([blob], `board_${timestamp}.webm`, {
|
const uint8Array = this.encoder?.FS.readFile(
|
||||||
// type: "video/webm",
|
this.encoder.outputFilename
|
||||||
// lastModified: timestamp,
|
) as Uint8Array;
|
||||||
// });
|
|
||||||
|
|
||||||
// return file;
|
const timestamp = Date.now();
|
||||||
// }
|
|
||||||
|
const file = new File([uint8Array], `board_${timestamp}.mp4`, {
|
||||||
|
type: "video/mp4",
|
||||||
|
lastModified: timestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.encoder?.delete();
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MP4;
|
export default MP4;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import WebMWriter from "webm-writer";
|
|||||||
class WebM {
|
class WebM {
|
||||||
private video: WebMWriter;
|
private video: WebMWriter;
|
||||||
|
|
||||||
constructor() {
|
constructor(private frameTime: number = 1000) {
|
||||||
this.video = new WebMWriter({
|
this.video = new WebMWriter({
|
||||||
quality: 0.8,
|
quality: 0.8,
|
||||||
fileWriter: null,
|
fileWriter: null,
|
||||||
@@ -14,8 +14,8 @@ class WebM {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
add(frame: CanvasImageSource | string, delay: number) {
|
add(frame: CanvasImageSource | string, frames: number) {
|
||||||
this.video.addFrame(frame, delay);
|
this.video.addFrame(frame, frames * this.frameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
async render(): Promise<File> {
|
async render(): Promise<File> {
|
||||||
|
|||||||
@@ -3,32 +3,43 @@ import Board from "../board/Board";
|
|||||||
import Game from "../game/Game";
|
import Game from "../game/Game";
|
||||||
import GIF from "./GIF";
|
import GIF from "./GIF";
|
||||||
import WebM from "./WebM";
|
import WebM from "./WebM";
|
||||||
// import MP4 from "./MP4";
|
import MP4 from "./MP4";
|
||||||
|
|
||||||
const MOVE_TIME = 1000;
|
const getData = (board: Board, encoder: GIF | WebM | MP4) => {
|
||||||
|
return encoder instanceof GIF
|
||||||
|
? board.toImgElement()
|
||||||
|
: encoder instanceof MP4
|
||||||
|
? board.toImageData()
|
||||||
|
: board.canvas;
|
||||||
|
};
|
||||||
|
|
||||||
const createAnimation = async (
|
const createAnimation = async (
|
||||||
pgn: string,
|
pgn: string,
|
||||||
style: Style,
|
style: Style,
|
||||||
size: number = 720,
|
size: number = 720,
|
||||||
format: "GIF" | "WebM"
|
format: "GIF" | "WebM" | "MP4"
|
||||||
) => {
|
) => {
|
||||||
const game = new Game().loadPGN(pgn);
|
const game = new Game().loadPGN(pgn);
|
||||||
const board = new Board(8).setStyle(style).setSize(size).hideBorder();
|
const board = new Board(8).setStyle(style).setSize(size).showBorder();
|
||||||
const animation =
|
const encoder =
|
||||||
format === "GIF" ? new GIF(board.width, board.height, true) : new WebM();
|
format === "GIF"
|
||||||
|
? new GIF(board.width, board.height, true)
|
||||||
|
: format === "MP4"
|
||||||
|
? new MP4(board.width, board.height)
|
||||||
|
: new WebM();
|
||||||
|
|
||||||
const header = game.getHeader();
|
const header = game.getHeader();
|
||||||
|
|
||||||
await board.titleFrame(header);
|
await board.titleFrame(header);
|
||||||
board.render();
|
board.render();
|
||||||
animation.add(format === "GIF" ? board.toImgElement() : board.canvas, 5000);
|
|
||||||
|
// @ts-ignore
|
||||||
|
await encoder.add(getData(board, encoder), 5);
|
||||||
|
|
||||||
await board.frame(game.getBoardData(), header);
|
await board.frame(game.getBoardData(), header);
|
||||||
board.render();
|
board.render();
|
||||||
animation.add(
|
// @ts-ignore
|
||||||
format === "GIF" ? board.toImgElement() : board.canvas,
|
await encoder.add(getData(board, encoder), 1);
|
||||||
MOVE_TIME
|
|
||||||
);
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const move = game.next();
|
const move = game.next();
|
||||||
@@ -39,13 +50,11 @@ const createAnimation = async (
|
|||||||
|
|
||||||
await board.frame(game.getBoardData(), header, move);
|
await board.frame(game.getBoardData(), header, move);
|
||||||
board.render();
|
board.render();
|
||||||
animation.add(
|
// @ts-ignore
|
||||||
format === "GIF" ? board.toImgElement() : board.canvas,
|
await encoder.add(getData(board, encoder), 1);
|
||||||
MOVE_TIME
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await animation.render();
|
return await encoder.render();
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createAnimation;
|
export default createAnimation;
|
||||||
|
|||||||
13
src/main.ts
13
src/main.ts
@@ -46,7 +46,7 @@ const play = async (board: Board, pgn: string | null, interval: number) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createDownloadLink = async (pgn: string, style: Style) => {
|
const createDownloadLink = async (pgn: string, style: Style) => {
|
||||||
const file = await createAnimation(pgn, style, 720, "WebM");
|
const file = await createAnimation(pgn, style, 720, "MP4");
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.innerText = "DOWNLOAD";
|
link.innerText = "DOWNLOAD";
|
||||||
link.setAttribute("href", URL.createObjectURL(file));
|
link.setAttribute("href", URL.createObjectURL(file));
|
||||||
@@ -57,7 +57,7 @@ const createDownloadLink = async (pgn: string, style: Style) => {
|
|||||||
console.log(createDownloadLink.name);
|
console.log(createDownloadLink.name);
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
const style = styles.lila;
|
const style = styles.calm;
|
||||||
|
|
||||||
// window.location.hash =
|
// window.location.hash =
|
||||||
// "#QiBEdWtlIEthcmwgLyBDb3VudCBJc291YXJkCkQgMTg1OC4/Py4/PwpFIFBhcmlzClIgMS0wClMgUGFyaXMgRlJBClcgUGF1bCBNb3JwaHkKCmU0IGU1IE5mMyBkNiBkNCBCZzQgZHhlNSBCeGYzIFF4ZjMgZHhlNSBCYzQgTmY2IFFiMyBRZTcgTmMzIGM2IEJnNSBiNSBOeGI1IGN4YjUgQnhiNSsgTmJkNyBPLU8tTyBSZDggUnhkNyBSeGQ3IFJkMSBRZTYgQnhkNysgTnhkNyBRYjgrIE54YjggUmQ4Iw==";
|
// "#QiBEdWtlIEthcmwgLyBDb3VudCBJc291YXJkCkQgMTg1OC4/Py4/PwpFIFBhcmlzClIgMS0wClMgUGFyaXMgRlJBClcgUGF1bCBNb3JwaHkKCmU0IGU1IE5mMyBkNiBkNCBCZzQgZHhlNSBCeGYzIFF4ZjMgZHhlNSBCYzQgTmY2IFFiMyBRZTcgTmMzIGM2IEJnNSBiNSBOeGI1IGN4YjUgQnhiNSsgTmJkNyBPLU8tTyBSZDggUnhkNyBSeGQ3IFJkMSBRZTYgQnhkNysgTnhkNyBRYjgrIE54YjggUmQ4Iw==";
|
||||||
@@ -65,17 +65,16 @@ const main = async () => {
|
|||||||
// const hash = window.location.hash;
|
// const hash = window.location.hash;
|
||||||
// const pgn = hash === "" ? null : decompressPGN(hash.slice(1));
|
// const pgn = hash === "" ? null : decompressPGN(hash.slice(1));
|
||||||
const pgn = pgns[pgns.length - 12];
|
const pgn = pgns[pgns.length - 12];
|
||||||
const board = new Board(8).setStyle(style).setSize(720).hideBorder();
|
const board = new Board(8).setStyle(style).setSize(720).showBorder();
|
||||||
|
|
||||||
$app?.appendChild(board.canvas);
|
$app?.appendChild(board.canvas);
|
||||||
|
|
||||||
const interval = 1000;
|
const interval = 1000;
|
||||||
play(board, pgn, interval);
|
play(board, pgn, interval);
|
||||||
|
|
||||||
// createDownloadLink(pgns[2], style).then((link) => {
|
createDownloadLink(pgns[2], style).then((link) => {
|
||||||
// document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
// console.log("Animation created!");
|
});
|
||||||
// });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WebFont.load({
|
WebFont.load({
|
||||||
|
|||||||
Reference in New Issue
Block a user