This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from paulkre/output-path-selection
Output path selection
- Loading branch information
Showing
7 changed files
with
95 additions
and
41 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,23 +1,10 @@ | ||
import P5 from "p5" | ||
import { withSaveFrame } from "./withSaveFrame" | ||
|
||
export type Api = { | ||
saveFrame(): Promise<void> | ||
saveFrame(path?: string): Promise<void> | void | ||
} | ||
|
||
export const createApi: (p: P5) => Api = p => ({ | ||
saveFrame: async () => { | ||
const canvas = document.querySelector<HTMLCanvasElement>("body > canvas") | ||
if (!canvas) return | ||
|
||
p.noLoop() | ||
await fetch(`/api/saveFrame/${p.frameCount}`, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "image/png", | ||
}, | ||
body: canvas.toDataURL().replace(/^data:image\/png;base64,/, ""), | ||
}) | ||
p.loop() | ||
}, | ||
saveFrame: withSaveFrame(p), | ||
}) |
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,51 @@ | ||
import P5 from "p5" | ||
import isValidPath from "is-valid-path" | ||
|
||
const usedPaths: string[] = [] | ||
|
||
const padZeros = (n: string, width: number) => | ||
n.length >= width ? n : new Array(width - n.length + 1).join("0") + n | ||
|
||
const validateExtension = (path: string) => { | ||
if (!path.match(/\.png$/)) throw Error("Output file format must be PNG") | ||
} | ||
|
||
const sanitizeOutPath = (path: string, frameCount: number) => { | ||
if (usedPaths.find(el => el === path)) | ||
throw Error("Cannot use the same output file name twice") | ||
|
||
validateExtension(path) | ||
|
||
const matches = path.match(/(#+)/g) | ||
if (matches) { | ||
if (matches.length > 1) | ||
throw Error("Too many number placeholders in output path") | ||
|
||
const paddedNumber = padZeros(String(frameCount), matches[0].length) | ||
path = path.replace(matches[0], paddedNumber) | ||
} else usedPaths.push(path) | ||
|
||
if (!isValidPath(path)) throw Error("Invalid output file path") | ||
|
||
return path | ||
} | ||
|
||
export const withSaveFrame = (p: P5) => async ( | ||
path: string = "out/out.#####.png", | ||
) => { | ||
const canvas = document.querySelector<HTMLCanvasElement>("body > canvas") | ||
if (!canvas) return | ||
|
||
path = sanitizeOutPath(path, p.frameCount) | ||
|
||
p.noLoop() | ||
await fetch(`/api/saveFrame/${encodeURIComponent(path)}`, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "image/png", | ||
}, | ||
body: canvas.toDataURL().replace(/^data:image\/png;base64,/, ""), | ||
}) | ||
p.loop() | ||
} |
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