Skip to content
Merged
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
118 changes: 67 additions & 51 deletions app/src/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import { Canvas } from "./room/canvas";
import { FakeRoom } from "./room/fake";

export function About(): JSX.Element {
const canvas = <canvas class="border-3 border-link-hue rounded-lg w-full h-full" id="demo" />;

const room = new FakeRoom(new Canvas(canvas as HTMLCanvasElement));
onCleanup(() => room.close());

const audioEnabled = solid(room.sound.enabled);
const enableAudio = () => {
room.sound.enabled.set(true);
room.sound.tts.enabled.set(true);
};

const services = ["Meet", "Zoom", "Teams", "Discord", "Skype", "WebEx", "FaceTime", "WhatsApp"];
const [currentService, setCurrentService] = createSignal(0);

Expand All @@ -30,6 +19,68 @@ export function About(): JSX.Element {
onCleanup(() => clearInterval(interval));
});

return (
<Layout>
<div class="flex flex-wrap gap-4 w-full prose-invert lg:prose-lg justify-center">
<div class="basis-md grow">
<p>
I built <a href="/">hang.live</a> because the internet forgot how to hang out. We forgot how to
be <b>weird</b>.
</p>
<p>
So here's a fun (and free) place to hang out with friends. Spend your free time with{" "}
<i>real people</i>, not doomscrolling feeds of AI slop. Unless you're into that.
</p>
</div>
<div class="grow basis-sm">
<div class="rounded-2xl border border-gray-800 p-6">
<div class="flex items-center justify-between mb-4">
<div class="flex items-center gap-2">
<span class="icon-[mdi--play] text-green-500" />
<span class="text-xl font-semibold underline decoration-green-500/80 underline-offset-2">
Start a hang
</span>
</div>
</div>

<CreateHang />
</div>
</div>

<div class="sm:m-8 m-4 h-128 w-full relative">
<Demo />
</div>

<p>
Powered by new and <a href="https://github.com/kixelated/moq">open source</a> web tech:{" "}
<a href="https://moq.dev">MoQ</a>. There's more to live than another {services[currentService()]}{" "}
clone. <i>Crazy</i>, I know.
</p>
</div>
</Layout>
);
}

function Demo(): JSX.Element {
const canvas = document.createElement("canvas");
canvas.className = "border-3 border-link-hue rounded-lg w-full h-full";
canvas.id = "demo";
const renderer = new Canvas(canvas);
onCleanup(() => renderer.close());
if (!renderer.supported) return <p>Interactive demo needs WebGL2.</p>;

const room = new FakeRoom(renderer);
onCleanup(() => {
room.close();
room.sound.close();
});

const audioEnabled = solid(room.sound.enabled);
const enableAudio = () => {
room.sound.enabled.set(true);
room.sound.tts.enabled.set(true);
};

const random = () => (Math.random() > 0.5 ? -0.5 : 0.5) * (Math.random() * Math.random());
const randomLocation = () => ({ x: random(), y: random() });
const randomLeft = () => ({ x: -0.5 * Math.random() * Math.random(), y: random() });
Expand Down Expand Up @@ -231,45 +282,10 @@ export function About(): JSX.Element {
});

return (
<Layout>
<div class="flex flex-wrap gap-4 w-full prose-invert lg:prose-lg justify-center">
<div class="basis-md grow">
<p>
I built <a href="/">hang.live</a> because the internet forgot how to hang out. We forgot how to
be <b>weird</b>.
</p>
<p>
So here's a fun (and free) place to hang out with friends. Spend your free time with{" "}
<i>real people</i>, not doomscrolling feeds of AI slop. Unless you're into that.
</p>
</div>
<div class="grow basis-sm">
<div class="rounded-2xl border border-gray-800 p-6">
<div class="flex items-center justify-between mb-4">
<div class="flex items-center gap-2">
<span class="icon-[mdi--play] text-green-500" />
<span class="text-xl font-semibold underline decoration-green-500/80 underline-offset-2">
Start a hang
</span>
</div>
</div>

<CreateHang />
</div>
</div>

<div class="sm:m-8 m-4 h-128 w-full relative">
<DemoHeader />
<AudioPrompt show={!audioEnabled()} onClick={enableAudio} />
{canvas}
</div>

<p>
Powered by new and <a href="https://github.com/kixelated/moq">open source</a> web tech:{" "}
<a href="https://moq.dev">MoQ</a>. There's more to live than another {services[currentService()]}{" "}
clone. <i>Crazy</i>, I know.
</p>
</div>
</Layout>
<>
<DemoHeader />
<AudioPrompt show={!audioEnabled()} onClick={enableAudio} />
{canvas}
</>
);
}
24 changes: 14 additions & 10 deletions app/src/components/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ export default function Profile(props: { local: Local }): JSX.Element {

{/* Avatar/Video Preview */}

<div class="relative text-center">
<div class="h-64 rounded-3xl flex items-center justify-center">{canvas}</div>
</div>
<Show when={local.canvas.supported} fallback={<p>Camera preview needs WebGL2.</p>}>
<div class="relative text-center">
<div class="h-64 rounded-3xl flex items-center justify-center">{canvas}</div>
</div>
</Show>

{/* Media Controls */}
<div class="flex gap-3 justify-center mb-6">
Expand Down Expand Up @@ -117,25 +119,27 @@ export default function Profile(props: { local: Local }): JSX.Element {
*/
class LocalPreview {
canvas: Canvas;
sound: Sound;
space: Space;
sound?: Sound;
space?: Space;

signals = new Effect();

constructor(element: HTMLCanvasElement, camera: HangLocalSource) {
// Create a minimal canvas without the background effects
this.canvas = new Canvas(element);
if (!this.canvas.supported) return;

// Create a minimal sound context (muted for preview)
this.sound = new Sound();

this.space = new Space(this.canvas, this.sound, {
const space = new Space(this.canvas, this.sound, {
// Not elegant, but disable some of the functionality for this profile preview.
profile: true,
});
this.space = space;

const broadcast = this.space.add("local", camera);
this.signals.cleanup(() => this.space.remove("local"));
const broadcast = space.add("local", camera);
this.signals.cleanup(() => space.remove("local"));

this.signals.run((effect: Effect) => {
const position = effect.get(broadcast.position);
Expand All @@ -155,8 +159,8 @@ class LocalPreview {

close() {
this.signals.close();
this.space.close();
this.space?.close();
this.canvas.close();
this.sound.close();
this.sound?.close();
}
}
34 changes: 23 additions & 11 deletions app/src/room/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { GLContext } from "./gl/context";

export class Canvas {
#canvas: HTMLCanvasElement;
#glContext: GLContext;
#camera: Camera;
#backgroundRenderer: BackgroundRenderer;
#glContext?: GLContext;
#camera?: Camera;
#backgroundRenderer?: BackgroundRenderer;

/** Whether this canvas can render with WebGL2. */
readonly supported: boolean;

// Use a callback to render after the background.
onRender?: (now: DOMHighResTimeStamp) => void;
Expand All @@ -24,14 +27,17 @@ export class Canvas {
}

get gl(): WebGL2RenderingContext {
if (!this.#glContext) throw new Error("WebGL2 not supported");
return this.#glContext.gl;
}

get glContext(): GLContext {
if (!this.#glContext) throw new Error("WebGL2 not supported");
return this.#glContext;
}

get camera() {
get camera(): Camera {
if (!this.#camera) throw new Error("WebGL2 not supported");
return this.#camera;
}

Expand All @@ -41,10 +47,14 @@ export class Canvas {
this.visible = new Signal(false);
this.viewport = new Signal(Vector.create(0, 0));

// Initialize WebGL2 context
this.#glContext = new GLContext(this.#canvas, this.viewport);
this.#camera = new Camera();
this.#backgroundRenderer = new BackgroundRenderer(this.#glContext);
const glContext = GLContext.create(this.#canvas, this.viewport);
this.#glContext = glContext;
this.supported = glContext !== undefined;
if (!glContext) return;

const camera = new Camera();
this.#camera = camera;
this.#backgroundRenderer = new BackgroundRenderer(glContext);

const resize = (entries: ResizeObserverEntry[]) => {
for (const entry of entries) {
Expand All @@ -68,14 +78,14 @@ export class Canvas {
this.#canvas.height = newHeight;

// Update WebGL viewport
this.#glContext.resize(newWidth, newHeight);
glContext.resize(newWidth, newHeight);

// The internal logic ignores devicePixelRatio because we automatically scale when rendering.
const viewport = Vector.create(newWidth / dpr, newHeight / dpr);
this.viewport.set(viewport);

// Update camera projection
this.#camera.updateOrtho(viewport);
camera.updateOrtho(viewport);

// Render immediately to avoid black flicker during resize
if (this.visible.peek()) {
Expand Down Expand Up @@ -130,6 +140,8 @@ export class Canvas {
}

#render(now: DOMHighResTimeStamp) {
if (!this.#glContext || !this.#backgroundRenderer) return;

// Clear the screen
this.#glContext.clear();

Expand Down Expand Up @@ -169,6 +181,6 @@ export class Canvas {

close() {
this.#signals.close();
this.#backgroundRenderer.cleanup();
this.#backgroundRenderer?.cleanup();
}
}
10 changes: 6 additions & 4 deletions app/src/room/gl/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ export class GLContext {
canvas: HTMLCanvasElement;
viewport: Signal<Vector>;

constructor(canvas: HTMLCanvasElement, viewport: Signal<Vector>) {
/** Create a context when the browser supports WebGL2. */
static create(canvas: HTMLCanvasElement, viewport: Signal<Vector>): GLContext | undefined {
const gl = canvas.getContext("webgl2", {
alpha: false,
antialias: true,
depth: true,
premultipliedAlpha: false,
});

if (!gl) {
throw new Error("WebGL2 not supported");
}
if (!gl) return;
return new GLContext(canvas, viewport, gl);
}

private constructor(canvas: HTMLCanvasElement, viewport: Signal<Vector>, gl: WebGL2RenderingContext) {
this.gl = gl;
this.canvas = canvas;
this.viewport = viewport;
Expand Down
12 changes: 12 additions & 0 deletions app/src/sup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { Sound } from "./room/sound";
import Settings from "./settings";

export function Sup(props: { canvas: Canvas; room: string }): JSX.Element {
if (!props.canvas.supported) return <WebGLUnsupported />;

const connection = new Moq.Connection.Reload({ enabled: true });
onCleanup(() => connection.close());

Expand Down Expand Up @@ -66,6 +68,16 @@ export function Sup(props: { canvas: Canvas; room: string }): JSX.Element {
);
}

function WebGLUnsupported(): JSX.Element {
return (
<WebLayout>
<h1 class="text-2xl font-bold mb-4">WebGL2 is required</h1>
<p class="mb-4">This room needs WebGL2 to render video, but it is unavailable in your browser.</p>
<p>Use a browser with WebGL2 enabled and reload the page.</p>
</WebLayout>
);
}

function App(props: {
connection: Moq.Connection.Reload;
canvas: Canvas;
Expand Down
Loading