The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
// The window object's browser-shaped surface: location, history, navigator, storage, viewport, media
// queries, and the host's window-level events.
import { Event, EventTarget } from "./events.ts";
import { URL } from "./web.ts";
const sys = __native.sys as NativeSys & { storageGet(k: string): string | null; storageSet(k: string, v: string): void; locale(): string };
export const viewport = { width: __boot.width, height: __boot.height, dpr: __boot.dpr };
// ---------------------------------------------------------------- location / history
let current = new URL(__boot.url);
const navigate = (href: string, replace = false) => {
const next = new URL(href, current.href);
if (next.origin !== current.origin) {
// Same-origin navigation guard: keep the window on the app's origin.
console.warn(`navigation outside ${current.origin} blocked: ${next.href}`);
return;
}
void replace;
sys.navigate(next.href);
};
export const location = {
get href() { return current.href; }, set href(v: string) { navigate(v); },
get origin() { return current.origin; },
get protocol() { return current.protocol; },
get host() { return current.host; },
get hostname() { return current.hostname; },
get port() { return current.port; },
get pathname() { return current.pathname; }, set pathname(v: string) { const u = new URL(current.href); u.pathname = v; navigate(u.href); },
get search() { return current.search; }, set search(v: string) { const u = new URL(current.href); u.search = v; navigate(u.href); },
get hash() { return current.hash; }, set hash(v: string) { current.hash = v; },
assign(v: string) { navigate(v); },
replace(v: string) { navigate(v, true); },
reload() { sys.navigate(current.href); },
toString() { return current.href; },
};
const stack: { state: any; url: string }[] = [{ state: null, url: current.href }];
export const history = {
get length() { return stack.length; },
get state() { return stack[stack.length - 1].state; },
scrollRestoration: "auto",
pushState(state: any, _title: string, url?: string | URL | null) {
if (url != null) { const u = new URL(String(url), current.href); if (u.origin !== current.origin) throw new Error("SecurityError"); current = u; }
stack.push({ state, url: current.href });
},
replaceState(state: any, _title: string, url?: string | URL | null) {
if (url != null) { const u = new URL(String(url), current.href); if (u.origin !== current.origin) throw new Error("SecurityError"); current = u; }
stack[stack.length - 1] = { state, url: current.href };
},
back() {}, forward() {}, go() {},
};
// ---------------------------------------------------------------- storage
export class Storage {
/** @internal */ _map = new Map<string, string>();
/** @internal */ _key: string | null;
/** @internal */ _timer = 0;
constructor(key: string | null) {
this._key = key;
if (key) {
try {
const saved = sys.storageGet(key);
if (saved) for (const [k, v] of Object.entries(JSON.parse(saved))) this._map.set(k, String(v));
} catch { /* start empty */ }
}
}
get length() { return this._map.size; }
key(i: number) { return [...this._map.keys()][i] ?? null; }
getItem(k: string) { return this._map.get(String(k)) ?? null; }
setItem(k: string, v: string) { this._map.set(String(k), String(v)); this._save(); }
removeItem(k: string) { if (this._map.delete(String(k))) this._save(); }
clear() { this._map.clear(); this._save(); }
/** @internal */ _save() {
// Written through immediately: the file is tiny and a crash must not lose the identity secret.
if (this._key) sys.storageSet(this._key, JSON.stringify(Object.fromEntries(this._map)));
}
}
export const localStorage = new Storage("local:" + current.origin);
export const sessionStorage = new Storage(null);
// ---------------------------------------------------------------- navigator / screen
const lang = (() => { try { return sys.locale() || "en-US"; } catch { return "en-US"; } })();
export const navigator = {
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) ThreeRT/0.1 Safari/605.1.15",
appName: "Netscape", appVersion: "5.0", vendor: "ThreeRT", product: "Gecko",
platform: "MacIntel",
language: lang, languages: [lang, "en-US"],
hardwareConcurrency: 8, deviceMemory: 8, maxTouchPoints: 0,
onLine: true, cookieEnabled: false, webdriver: false,
clipboard: {
writeText: (t: string) => { (__native.net as any)?.clipboardWrite?.(String(t)); return Promise.resolve(); },
readText: () => Promise.resolve(String((__native.net as any)?.clipboardRead?.() ?? "")),
},
sendBeacon: () => false,
vibrate: () => false,
mediaDevices: undefined,
gpu: undefined,
};
export const screen = {
get width() { return viewport.width; }, get height() { return viewport.height; },
get availWidth() { return viewport.width; }, get availHeight() { return viewport.height; },
colorDepth: 24, pixelDepth: 24, orientation: { type: "landscape-primary", angle: 0, addEventListener() {}, removeEventListener() {} },
};
// ---------------------------------------------------------------- media queries
function evalMedia(q: string): boolean {
return q.split(",").some((part) => {
const p = part.trim().toLowerCase();
if (!p) return false;
return p.split(/\s+and\s+/).every((cond) => {
const c = cond.trim();
if (c === "screen" || c === "all") return true;
if (c === "print") return false;
const m = /^\(\s*([a-z-]+)\s*(?::\s*([^)]+))?\)$/.exec(c);
if (!m) return false;
const [, f, raw] = m;
const v = (raw ?? "").trim();
const px = parseFloat(v);
switch (f) {
case "max-width": return viewport.width <= px;
case "min-width": return viewport.width >= px;
case "max-height": return viewport.height <= px;
case "min-height": return viewport.height >= px;
case "prefers-reduced-motion": return v === "no-preference";
case "prefers-color-scheme": return v === "light";
case "hover": return v === "hover";
case "pointer": return v === "fine";
case "any-pointer": return v === "fine";
case "orientation": return v === (viewport.width >= viewport.height ? "landscape" : "portrait");
case "min-resolution": return viewport.dpr * 96 >= parseFloat(v) * (v.endsWith("dppx") ? 96 : 1);
case "-webkit-min-device-pixel-ratio": return viewport.dpr >= px;
default: return false;
}
});
});
}
class MediaQueryList extends EventTarget {
onchange: any = null;
constructor(public media: string) { super(); }
get matches() { return evalMedia(this.media); }
addListener(f: any) { this.addEventListener("change", f); }
removeListener(f: any) { this.removeEventListener("change", f); }
}
export function matchMedia(q: string) { return new MediaQueryList(String(q)); }
export { evalMedia };
// ---------------------------------------------------------------- dialogs (the game never uses them)
export function alert(m?: unknown) { console.warn("alert:", m); }
export function confirm(m?: unknown) { console.warn("confirm:", m); return false; }
export function prompt(m?: unknown) { console.warn("prompt:", m); return null; }
export function open(url?: string) { console.warn("window.open blocked:", url); return null; }
// ---------------------------------------------------------------- host window events
export function windowEvent(win: EventTarget & any, doc: any, name: string, data: number[]) {
switch (name) {
case "resize":
viewport.width = data[0]; viewport.height = data[1]; viewport.dpr = data[2];
win.dispatchEvent(new Event("resize"));
break;
case "focus": case "blur":
win.dispatchEvent(new Event(name));
break;
case "visibility": {
const hidden = data[0] === 0;
if (doc && doc._hidden !== hidden) { doc._hidden = hidden; doc.dispatchEvent(new Event("visibilitychange", { bubbles: true })); }
break;
}
}
}