Expand description
§Oxide SDK
Guest-side SDK for building WebAssembly applications that run inside the
Oxide browser. This crate provides
safe Rust wrappers around the raw host-imported functions exposed by the
"oxide" wasm import module.
§Quick Start
Add oxide-sdk to your Cargo.toml and set crate-type = ["cdylib"]:
[lib]
crate-type = ["cdylib"]
[dependencies]
oxide-sdk = "0.2"Then write your app:
ⓘ
use oxide_sdk::*;
#[no_mangle]
pub extern "C" fn start_app() {
log("Hello from Oxide!");
canvas_clear(30, 30, 46, 255);
canvas_text(20.0, 40.0, 28.0, 255, 255, 255, "Welcome to Oxide");
}Build with cargo build --target wasm32-unknown-unknown --release.
§Interactive Apps
For apps that need a render loop, export on_frame:
ⓘ
use oxide_sdk::*;
#[no_mangle]
pub extern "C" fn start_app() {
log("Interactive app started");
}
#[no_mangle]
pub extern "C" fn on_frame(_dt_ms: u32) {
canvas_clear(30, 30, 46, 255);
let (mx, my) = mouse_position();
canvas_circle(mx, my, 20.0, 255, 100, 100, 255);
if ui_button(1, 20.0, 20.0, 100.0, 30.0, "Click me!") {
log("Button was clicked!");
}
}§API Categories
| Category | Functions |
|---|---|
| Canvas | canvas_clear, canvas_rect, canvas_circle, canvas_text, canvas_line, canvas_image, canvas_dimensions |
| Console | log, warn, error |
| HTTP | fetch, fetch_get, fetch_post, fetch_post_proto, fetch_put, fetch_delete |
| Protobuf | proto::ProtoEncoder, proto::ProtoDecoder |
| Storage | storage_set, storage_get, storage_remove, kv_store_set, kv_store_get, kv_store_delete |
| Audio | audio_play, audio_play_url, audio_pause, audio_resume, audio_stop, audio_set_volume, audio_channel_play |
| Timers | set_timeout, set_interval, clear_timer, time_now_ms |
| Navigation | navigate, push_state, replace_state, get_url, history_back, history_forward |
| Input | mouse_position, mouse_button_down, key_down, key_pressed, scroll_delta |
| Widgets | ui_button, ui_checkbox, ui_slider, ui_text_input |
| Crypto | hash_sha256, hash_sha256_hex, base64_encode, base64_decode |
| Other | clipboard_write, clipboard_read, random_u64, random_f64, notify, upload_file, load_module |
§Full API Documentation
See https://docs.oxide.foundation/oxide_sdk/ for the complete API reference, or browse the individual function documentation below.
Modules§
- proto
- Lightweight protobuf wire-format encoder/decoder.
Structs§
- Fetch
Response - Response from an HTTP fetch call.
- Uploaded
File - File returned from the native file picker.
Constants§
- KEY_0
- KEY_1
- KEY_2
- KEY_3
- KEY_4
- KEY_5
- KEY_6
- KEY_7
- KEY_8
- KEY_9
- KEY_A
- KEY_B
- KEY_
BACKSPACE - KEY_C
- KEY_D
- KEY_
DELETE - KEY_
DOWN - KEY_E
- KEY_END
- KEY_
ENTER - KEY_
ESCAPE - KEY_F
- KEY_G
- KEY_H
- KEY_
HOME - KEY_I
- KEY_J
- KEY_K
- KEY_L
- KEY_
LEFT - KEY_M
- KEY_N
- KEY_O
- KEY_P
- KEY_
PAGE_ DOWN - KEY_
PAGE_ UP - KEY_Q
- KEY_R
- KEY_
RIGHT - KEY_S
- KEY_
SPACE - KEY_T
- KEY_TAB
- KEY_U
- KEY_UP
- KEY_V
- KEY_W
- KEY_X
- KEY_Y
- KEY_Z
Functions§
- alt_
held - Returns
trueif Alt is held. - audio_
channel_ play - Play audio on a specific channel. Multiple channels play simultaneously.
Channel 0 is the default used by
audio_play. Use channels 1+ for layered sound effects, background music, etc. - audio_
channel_ set_ volume - Set volume for a specific channel (0.0 silent, 1.0 normal, up to 2.0 boost).
- audio_
channel_ stop - Stop playback on a specific channel.
- audio_
duration - Get the total duration of the currently loaded track in milliseconds. Returns 0 if unknown or nothing is loaded.
- audio_
get_ volume - Get the current audio volume.
- audio_
is_ playing - Returns
trueif audio is currently playing (not paused and not empty). - audio_
pause - Pause audio playback.
- audio_
play - Play audio from encoded bytes (WAV, MP3, OGG, FLAC). The host decodes and plays the audio. Returns 0 on success, negative on error.
- audio_
play_ url - Fetch audio from a URL and play it. The host performs the HTTP fetch and decodes the audio. Returns 0 on success, negative on error.
- audio_
position - Get the current playback position in milliseconds.
- audio_
resume - Resume paused audio playback.
- audio_
seek - Seek to a position in milliseconds. Returns 0 on success, negative on error.
- audio_
set_ loop - Enable or disable looping on the default channel.
When enabled, subsequent
audio_playcalls will loop indefinitely. - audio_
set_ volume - Set audio volume. 1.0 is normal, 0.0 is silent, up to 2.0 for boost.
- audio_
stop - Stop audio playback and clear the queue.
- base64_
decode - Decode a base64-encoded string back to bytes.
- base64_
encode - Base64-encode arbitrary bytes.
- canvas_
circle - Draw a filled circle.
- canvas_
clear - Clear the canvas with a solid RGBA color.
- canvas_
dimensions - Returns
(width, height)of the canvas in pixels. - canvas_
image - Draw an image on the canvas from encoded image bytes (PNG, JPEG, GIF, WebP). The browser decodes the image and renders it at the given rectangle.
- canvas_
line - Draw a line between two points.
- canvas_
rect - Draw a filled rectangle.
- canvas_
text - Draw text on the canvas.
- clear_
hyperlinks - Remove all previously registered hyperlinks.
- clear_
timer - Cancel a timer previously created with
set_timeoutorset_interval. - clipboard_
read - Read text from the system clipboard.
- clipboard_
write - Copy text to the system clipboard.
- ctrl_
held - Returns
trueif Ctrl (or Cmd on macOS) is held. - error
- Print an error to the browser console.
- fetch
- Perform an HTTP request. Returns the status code and response body.
- fetch_
delete - HTTP DELETE.
- fetch_
get - HTTP GET request.
- fetch_
post - HTTP POST with raw bytes.
- fetch_
post_ proto - HTTP POST with protobuf body (sets
Content-Type: application/protobuf). - fetch_
put - HTTP PUT with raw bytes.
- get_
location - Get the device’s mock geolocation as a
"lat,lon"string. - get_
state - Retrieve the opaque state bytes attached to the current history entry.
Returns
Noneif no state has been set. - get_url
- Get the URL of the currently loaded page.
- hash_
sha256 - Compute the SHA-256 hash of the given data. Returns 32 bytes.
- hash_
sha256_ hex - Return SHA-256 hash as a lowercase hex string.
- history_
back - Navigate backward in history. Returns
trueif a navigation was queued. - history_
forward - Navigate forward in history. Returns
trueif a navigation was queued. - history_
length - Return the total number of entries in the history stack.
- key_
down - Returns
trueif the given key is currently held down. SeeKEY_*constants for key codes. - key_
pressed - Returns
trueif the given key was pressed this frame. - kv_
store_ delete - Delete a key from the persistent KV store. Returns
trueon success. - kv_
store_ get - Retrieve a value from the persistent KV store.
Returns
Noneif the key does not exist. - kv_
store_ get_ str - Convenience wrapper: retrieve a UTF-8 string value.
- kv_
store_ set - Store a key-value pair in the persistent on-disk KV store.
Returns
trueon success. - kv_
store_ set_ str - Convenience wrapper: store a UTF-8 string value.
- load_
module - Fetch and execute another
.wasmmodule from a URL. The loaded module shares the same canvas, console, and storage context. Returns 0 on success, negative error code on failure. - log
- Print a message to the browser console (log level).
- modifiers
- Returns modifier key state as a bitmask: bit 0 = Shift, bit 1 = Ctrl, bit 2 = Alt.
- mouse_
button_ clicked - Returns
trueif the given mouse button was clicked this frame. - mouse_
button_ down - Returns
trueif the given mouse button is currently held down. Button 0 = primary (left), 1 = secondary (right), 2 = middle. - mouse_
position - Get the mouse position in canvas-local coordinates.
- navigate
- Navigate to a new URL. The URL can be absolute or relative to the current
page. Navigation happens asynchronously after the current
start_appreturns. Returns 0 on success, negative on invalid URL. - notify
- Send a notification to the user (rendered in the browser console).
- push_
state - Push a new entry onto the browser’s history stack without triggering a
module reload. This is analogous to
history.pushState()in web browsers. - random_
f64 - Get a random f64 in [0, 1).
- random_
u64 - Get a random u64 from the host.
- register_
hyperlink - Register a rectangular region on the canvas as a clickable hyperlink.
- replace_
state - Replace the current history entry (no new entry is pushed).
Analogous to
history.replaceState(). - scroll_
delta - Get the scroll wheel delta for this frame.
- set_
interval - Schedule a repeating timer that fires every
interval_msmilliseconds. When it fires the host calls your exportedon_timer(callback_id). Returns a timer ID that can be passed toclear_timer. - set_
timeout - Schedule a one-shot timer that fires after
delay_msmilliseconds. When it fires the host calls your exportedon_timer(callback_id). Returns a timer ID that can be passed toclear_timer. - shift_
held - Returns
trueif Shift is held. - storage_
get - Retrieve a value from local storage. Returns empty string if not found.
- storage_
remove - Remove a key from local storage.
- storage_
set - Store a key-value pair in sandboxed local storage.
- time_
now_ ms - Get the current time in milliseconds since the UNIX epoch.
- ui_
button - Render a button at the given position. Returns
trueif it was clicked on the previous frame. - ui_
checkbox - Render a checkbox. Returns the current checked state.
- ui_
slider - Render a slider. Returns the current value.
- ui_
text_ input - Render a single-line text input. Returns the current text content.
- upload_
file - Opens the native OS file picker and returns the selected file.
Returns
Noneif the user cancels. - url_
decode - Decode a percent-encoded string.
- url_
encode - Percent-encode a string for safe inclusion in URL components.
- url_
resolve - Resolve a relative URL against a base URL (WHATWG algorithm).
Returns
Noneif either URL is invalid. - warn
- Print a warning to the browser console.