Klorad SDK docs · 0.2
Build with Klorad.
Build geospatial applications with maps, 3D models and connected data. Start with a working scene, explore examples, or look up an API.
The SDK packages require access; the examples run in your browser without it. Request package access.
A twin in six steps.
From an empty scene to readings on its objects, with the code of each step beside the scene it changes.
The finished twin
Running in your browser, on the packages you install.
Press play to build it from an empty scene, or enter at any step. Each step shows the code that has just run.
/** Space: one origin on Earth. From there you work in metres. */
export function anchor() {
return createScene({
name: "Site gate",
// Any point on Earth; this one is the White Tower of Thessaloniki.
// Positions you give later, in metres east, north and up of it, are
// kept as places on Earth.
coordinateSystem: { origin: { lat: 40.62637, lon: 22.94838 } },
});
}// Footprints and paths are metres east and north of the object's position.
const YARD: PlanPoint[] = [[-22, -12], [22, -12], [22, 16], [-22, 16]];
const HALL: PlanPoint[] = [[-14, -8], [14, -8], [14, 8], [-14, 8]];
const ROAD: PlanPoint[] = [[0, -24], [0, 10]];
/** Scene Objects: an area, a block and a line, the shapes of a map. */
export function build(scene: Scene) {
scene.add({
id: "yard", name: "Yard", position: { east: 0, north: 6 },
geometry: { kind: "polygon", footprint: YARD },
});
scene.add({
id: "hall", name: "Hall", position: { east: -6, north: 12 },
geometry: { kind: "extrusion", footprint: HALL, height: 8 },
});
scene.add({
id: "road", name: "Access road", position: { east: 16, north: 0 },
geometry: { kind: "polyline", path: ROAD, width: 5 },
});
}const ARM: PlanPoint[] = [[-0.1, 0], [0.1, 0], [0.1, 5.5], [-0.1, 5.5]];
/** Physical correspondence: what mirrors what, and which way data flows. */
export function bind(scene: Scene) {
// A Digital Shadow: readings flow one way, from the sensor into it.
scene.add({
id: "counter", name: "Entrance counter", position: { east: -6, north: 1 },
geometry: { kind: "box", width: 3, depth: 3, height: 1 },
correspondence: "shadow",
binding: { source: "gateway", entity: "entrance-01" },
});
// A Digital Twin: two way. It can also send a command to its device.
scene.add({
id: "barrier", name: "Barrier",
// Hinged one metre up, reaching east across the road.
position: { east: 13.4, north: -9, up: 1 }, orientation: { heading: 90 },
geometry: { kind: "extrusion", footprint: ARM, height: 0.2 },
correspondence: "twin",
binding: { source: "plc", entity: "barrier-1" },
// The PLC reports on change only, so its last word stands for an hour.
staleAfterMs: 3_600_000,
});
}/** Integration and Time: sources report, the scene orders what they say. */
export function connect(scene: Scene) {
const gateway = fixtureSource({
id: "gateway",
channels: [{
entity: "entrance-01", quantity: "occupancy", unit: "people",
value: (tick) => 14 + Math.round(9 * Math.sin(tick / 2)),
}],
});
// A simulated PLC: it reports its state, and obeys a command a moment later.
const plc = fixtureDevice({
id: "plc",
latencyMs: 1200,
entities: { "barrier-1": { state: "closed" } },
respond: ({ name }) => ({ state: name === "open" ? "open" : "closed" }),
});
// Each reading lands on the objects bound to its source and entity,
// ordered by when it was observed, not by when it arrived.
const links = [connectSource(scene, gateway), connectSource(scene, plc)];
return { plc, stop: () => links.forEach((link) => link.stop()) };
}/** Rendering: one function turns the state of an object into its looks. */
export const looks: StyleFn = ({ object, readings, stale, palette }) => {
// What only gives context steps back, so that what reports stands out.
if (object.correspondence === "object") {
return { color: palette.object, opacity: object.id === "hall" ? 0.4 : 0.25 };
}
if (stale) return { color: palette.stale, opacity: 0.7, label: "no reading" };
const people = readings.occupancy?.value;
if (typeof people === "number") {
// The counter stands as tall as the hall is full.
const heightScale = people / 6;
return { color: palette.shadow, heightScale, label: `${people} people` };
}
// The arm lifts only when the device itself reports that it opened.
return readings.state?.value === "open"
? { color: "#3ccf91", pose: { pitch: 80 }, label: "open" }
: { color: palette.twin, label: "closed" };
};
// <ThreeView objectStyle={looks} /> draws the scene with it, on every change./** Interaction and actuation: an Action passes the Entitlement first. */
export async function act(scene: Scene, plc: FixtureDevice) {
const interaction = createInteraction(scene, {
// In an app: an AccessPolicy of @klorad/access (roles, teams, Worlds).
entitlement: {
check: ({ actor }) => actor.roles?.includes("operator")
? { allowed: true } : { allowed: false, reason: "operators only" },
},
actuators: [plc],
actions: [{
id: "open", label: "Open barrier", behaviour: "flash",
appliesTo: (object) => object.correspondence === "twin",
command: () => ({ name: "open" }),
}],
behaviours: [{ id: "flash", run: (b) => b.animate({ kind: "highlight" }) }],
});
const open = { action: "open", objectId: "barrier" };
const guest = { id: "guest", roles: ["visitor"] };
const eleni = { id: "eleni", roles: ["operator"] };
// The visitor is refused, and nothing reaches the device.
await interaction.perform({ ...open, actor: guest });
// The operator's command is sent; the arm lifts when the PLC reports it.
return interaction.perform({ ...open, actor: eleni });
}6 steps, 0:47
Also here
- GuidesOne task per page: connect a system, import a model, publish a public view.
- Connect your systemMQTT, webhooks or a REST API, and the path from your server to the browser.
- Klorad One and the SDKWhat the platform's editor does without code, and which steps need code.
- For agentsEvery page as Markdown in one fetch, and the rules a coding agent follows.