-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Punctual target (wrapper, canvas, frame) (#322)
Closes #316
- Loading branch information
Showing
11 changed files
with
574 additions
and
10 deletions.
There are no files selected for viewing
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public | ||
src/lib/punctual.js |
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,31 @@ | ||
import React from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { DisplaySettings } from "@/lib/display-settings"; | ||
|
||
interface PunctualCanvasProps { | ||
fullscreen?: boolean; | ||
displaySettings: DisplaySettings; | ||
ref: React.RefObject<HTMLCanvasElement>; | ||
} | ||
|
||
const PunctualCanvas = ({ | ||
fullscreen, | ||
displaySettings, | ||
ref, | ||
}: PunctualCanvasProps) => ( | ||
<canvas | ||
ref={ref} | ||
className={cn( | ||
"absolute top-0 left-0", | ||
fullscreen && "h-full w-full block overflow-hidden", | ||
)} | ||
style={{ | ||
imageRendering: "pixelated", | ||
display: displaySettings.showCanvas ? "" : "none", | ||
}} | ||
width={window.innerWidth / displaySettings.canvasPixelSize} | ||
height={window.innerHeight / displaySettings.canvasPixelSize} | ||
/> | ||
); | ||
|
||
export default React.memo(PunctualCanvas); |
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,96 @@ | ||
import { DisplaySettings } from "./display-settings.js"; | ||
import { ErrorHandler } from "./mercury-wrapper.js"; | ||
import { isWebglSupported } from "./webgl-detector.js"; | ||
|
||
// Based on https://github.com/dktr0/Punctual/blob/main/index.html | ||
export class PunctualWrapper { | ||
initialized: boolean = false; | ||
|
||
protected _canvas: HTMLCanvasElement; | ||
protected _punctual: any; | ||
protected _animation: any; | ||
protected _onError: ErrorHandler; | ||
protected _onWarning: ErrorHandler; | ||
protected _displaySettings: DisplaySettings; | ||
|
||
constructor({ | ||
canvas, | ||
onError, | ||
onWarning, | ||
displaySettings, | ||
}: { | ||
canvas: HTMLCanvasElement; | ||
onError?: ErrorHandler; | ||
onWarning?: ErrorHandler; | ||
displaySettings: DisplaySettings; | ||
}) { | ||
this._canvas = canvas; | ||
this._animation = null; | ||
this._onError = onError || (() => {}); | ||
this._onWarning = onWarning || (() => {}); | ||
this._displaySettings = displaySettings; | ||
} | ||
|
||
setDisplaySettings(displaySettings: DisplaySettings) { | ||
this._displaySettings = displaySettings; | ||
} | ||
|
||
async initialize() { | ||
if (this.initialized) return; | ||
|
||
if (!isWebglSupported()) { | ||
this._onError("WebGL is not supported on this browser."); | ||
return; | ||
} | ||
|
||
// @ts-ignore | ||
const P = await import("./punctual.js"); | ||
const { Punctual } = P; | ||
|
||
try { | ||
this._punctual = new Punctual(); | ||
} catch (error) { | ||
console.error(error); | ||
this._onError(`${error}`); | ||
return; | ||
} | ||
|
||
this._animation = requestAnimationFrame(this.animate); | ||
|
||
this.initialized = true; | ||
console.log("Punctual initialized"); | ||
} | ||
|
||
animate = () => { | ||
if (!this.initialized) return; | ||
|
||
const nowTime = Date.now() / 1000.0; | ||
this._punctual.preRender({ canDraw: true, nowTime }); | ||
this._punctual.render({ canDraw: true, zone: 0, nowTime }); | ||
this._punctual.postRender({ canDraw: true, nowTime }); | ||
|
||
this._animation = requestAnimationFrame(this.animate); | ||
}; | ||
|
||
async tryEval(code: string) { | ||
if (!this.initialized) await this.initialize(); | ||
|
||
try { | ||
const res = await this._punctual.define({ | ||
zone: 0, | ||
text: code, | ||
time: Date.now() / 1000.0, | ||
}); | ||
console.log(res); | ||
} catch (error) { | ||
console.error(error); | ||
this._onError(`${error}`); | ||
} | ||
} | ||
|
||
dispose() { | ||
cancelAnimationFrame(this._animation); | ||
this.initialized = false; | ||
console.log("Punctual disposed"); | ||
} | ||
} |
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,5 @@ | ||
declare module "./punctual.js" { | ||
export class Punctual { | ||
constructor(); | ||
} | ||
} |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useEvalHandler } from "@/hooks/use-eval-handler"; | ||
import { useSettings } from "@/hooks/use-settings"; | ||
import { sendToast } from "@/lib/utils"; | ||
import { isWebglSupported } from "@/lib/webgl-detector"; | ||
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||
import { defaultDisplaySettings } from "@/lib/display-settings"; | ||
import { PunctualWrapper } from "@/lib/punctual-wrapper"; | ||
import PunctualCanvas from "@/components/punctual-canvas"; | ||
|
||
declare global { | ||
interface Window { | ||
m: number; // meter value from Mercury | ||
} | ||
} | ||
|
||
export function Component() { | ||
const canvasRef = useRef<HTMLCanvasElement | null>(null); | ||
const hasWebGl = useMemo(() => isWebglSupported(), []); | ||
const [instance, setInstance] = useState<PunctualWrapper | null>(null); | ||
const [displaySettings, setDisplaySettings] = useState( | ||
defaultDisplaySettings, | ||
); | ||
|
||
useEffect(() => { | ||
if (hasWebGl) return; | ||
sendToast( | ||
"warning", | ||
"WebGL not available", | ||
"WebGL is disabled or not supported, so Hydra was not initialized", | ||
); | ||
}, [hasWebGl]); | ||
|
||
useEffect(() => { | ||
if (!hasWebGl) return; | ||
const canvas = canvasRef.current; | ||
if (!canvas) return; | ||
|
||
(async () => { | ||
const punctual = new PunctualWrapper({ | ||
canvas, | ||
onError: (err) => { | ||
sendToast("destructive", "Punctual error", err.toString()); | ||
}, | ||
onWarning: (msg) => { | ||
sendToast("warning", "Punctual warning", msg); | ||
}, | ||
displaySettings: displaySettings, | ||
}); | ||
|
||
await punctual.initialize(); | ||
setInstance(punctual); | ||
|
||
window.parent.punctual = window; | ||
})(); | ||
|
||
return () => { | ||
instance?.dispose(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
instance?.setDisplaySettings(displaySettings); | ||
}, [displaySettings]); | ||
|
||
useEvalHandler( | ||
useCallback( | ||
(msg) => { | ||
if (!instance) return; | ||
instance.tryEval(msg.body); | ||
}, | ||
[instance], | ||
), | ||
); | ||
|
||
useSettings( | ||
useCallback( | ||
(msg) => { | ||
if (!instance) return; | ||
if (msg.displaySettings) { | ||
setDisplaySettings(msg.displaySettings); | ||
} | ||
}, | ||
[instance], | ||
), | ||
); | ||
|
||
return ( | ||
hasWebGl && | ||
canvasRef && ( | ||
<PunctualCanvas | ||
ref={canvasRef} | ||
fullscreen | ||
displaySettings={displaySettings} | ||
/> | ||
) | ||
); | ||
} |
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ declare global { | |
hydra: any; | ||
mercury: any; | ||
strudel: any; | ||
punctual: any; | ||
} | ||
} | ||
|
||
|
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