pub struct SsContextWeb { /* private fields */ }Expand description
The page’s WebGPU context: one adapter, one device, one SSRenderer,
shared by every canvas and every player on the page.
const ctx = await SsContextWeb.create();
const surf = ctx.createSurface(canvas);
const player = surf.createPlayer();
await player.loadSsab(bytes);
player.setAnimation('idle');
player.play();Implementations§
Source§impl SsContextWeb
impl SsContextWeb
Sourcepub async fn create() -> Result<SsContextWeb, JsValue>
pub async fn create() -> Result<SsContextWeb, JsValue>
Initialise WebGPU for the page. Rejects if the browser has no WebGPU adapter/device (the caller should surface a fallback message).
Call this once per page and share the result. Calling it twice builds two devices, and nothing is shared between them.
Sourcepub fn create_surface(
&self,
canvas: HtmlCanvasElement,
) -> Result<SsSurfaceWeb, JsValue>
pub fn create_surface( &self, canvas: HtmlCanvasElement, ) -> Result<SsSurfaceWeb, JsValue>
Attach a canvas and get the scene that draws into it. Any number of canvases may share this context.
Sourcepub fn add_texture(
&self,
key: u32,
rgba: &[u8],
width: u32,
height: u32,
) -> Result<(), JsValue>
pub fn add_texture( &self, key: u32, rgba: &[u8], width: u32, height: u32, ) -> Result<(), JsValue>
Supply a texture under a textureRefs entry’s key, from raw straight-alpha RGBA.
Device-scoped, so it covers every canvas and every player at once — which is exactly why it is here and not on a surface or a player.
Prefer SsContextWeb::add_texture_from_bytes when what you have is an
encoded image: this path premultiplies on the CPU and keeps the pixels for
device-loss recovery, so a 2048² atlas costs two 16 MB passes through wasm
memory that createImageBitmap does not.
Throws, uploading nothing, when rgba is not width * height * 4 bytes or
the device cannot hold the texture (past its max_texture_dimension_2d).
Sourcepub async fn add_texture_from_bytes(
&self,
key: u32,
bytes: Vec<u8>,
) -> Result<(), JsValue>
pub async fn add_texture_from_bytes( &self, key: u32, bytes: Vec<u8>, ) -> Result<(), JsValue>
Supply a texture under its key from encoded image bytes (PNG) — the main
texture path of this face.
Both .ssab forms end here. An embedded pack’s atlases come from
SsPlayerWeb::embedded_textures and need no fetch; a reference pack’s
come from fetching each SsPlayerWeb::texture_refs entry whose
embedded is false, resolved against the .ssab’s own URL — which is how
this face supports both forms without carrying a decoder.
The bytes are kept (they are the encoded image, not the pixels) so
SsContextWeb::restore_device can rebuild this texture on its own.
Sourcepub fn add_texture_from_bitmap(
&self,
key: u32,
bitmap: &ImageBitmap,
) -> Result<(), JsValue>
pub fn add_texture_from_bitmap( &self, key: u32, bitmap: &ImageBitmap, ) -> Result<(), JsValue>
Supply a texture under its key from an ImageBitmap the page decoded.
For a host with its own asset pipeline — the analogue of
crate::SSRenderer::set_texture. The bitmap must be premultiplied
(createImageBitmap(blob, { premultiplyAlpha: 'premultiply' })), which is
what the whole pipeline composites in.
Device loss is the supplier’s to repair on this path, exactly as on the
Rust one: nothing is retained here, so the page registers it again after
SsContextWeb::restore_device. Use
SsContextWeb::add_texture_from_bytes to be spared that.
Throws, registering nothing, for a bitmap the device cannot hold — past
its max_texture_dimension_2d, or closed.
Sourcepub fn has_texture(&self, key: u32) -> bool
pub fn has_texture(&self, key: u32) -> bool
Whether an atlas is already registered for key.
The texture map is device-scoped, so this answers for the whole page: it is how a page that decodes atlases itself keeps “N players of one pack decode its atlas once between them” true. The player-side flush gets that for free (it filters before the decode); a page supplying textures itself has to ask.
Sourcepub fn is_device_lost(&self) -> bool
pub fn is_device_lost(&self) -> bool
Whether the browser has taken the WebGPU device away. While
this is true every surface’s frame is a no-op; call
SsContextWeb::restore_device to bring the whole page back.
Sourcepub fn sdk_version(&self) -> String
pub fn sdk_version(&self) -> String
The SpriteStudio-SDK version whose runtime this build links, written as
its release tag (v7.0.0), or local from a plain cargo build. The
package carries the same string in wasm/VERSION.
Sourcepub async fn restore_device(&self) -> Result<(), JsValue>
pub async fn restore_device(&self) -> Result<(), JsValue>
Rebuild the page’s WebGPU stack after a device loss and restore every
surface and player on it (pipelines, buffers, atlases, projections) —
playback continues from the frame it was interrupted on, with no re-fetch
of any .ssab and no re-fetch of any atlas.
One call for the page, however many canvases it holds.
Rejects if WebGPU is gone for good, or if the new adapter cannot present a
surface’s baked format (then the host should rebuild from create).
Sourcepub fn surface_count(&self) -> usize
pub fn surface_count(&self) -> usize
How many canvases this context currently drives (diagnostic).
Trait Implementations§
Source§impl From<SsContextWeb> for JsValue
impl From<SsContextWeb> for JsValue
Source§fn from(value: SsContextWeb) -> Self
fn from(value: SsContextWeb) -> Self
Source§impl FromWasmAbi for SsContextWeb
impl FromWasmAbi for SsContextWeb
Source§impl IntoWasmAbi for SsContextWeb
impl IntoWasmAbi for SsContextWeb
Source§impl LongRefFromWasmAbi for SsContextWeb
impl LongRefFromWasmAbi for SsContextWeb
Source§type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
RefFromWasmAbi::AbiSource§type Anchor = RcRef<SsContextWeb>
type Anchor = RcRef<SsContextWeb>
RefFromWasmAbi::AnchorSource§unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor
RefFromWasmAbi::ref_from_abiSource§impl NullableFromWasmAbi for SsContextWeb
impl NullableFromWasmAbi for SsContextWeb
Source§impl NullableIntoWasmAbi for SsContextWeb
impl NullableIntoWasmAbi for SsContextWeb
Source§impl OptionFromWasmAbi for SsContextWeb
impl OptionFromWasmAbi for SsContextWeb
Source§impl OptionIntoWasmAbi for SsContextWeb
impl OptionIntoWasmAbi for SsContextWeb
Source§impl RefFromWasmAbi for SsContextWeb
impl RefFromWasmAbi for SsContextWeb
Source§type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
Self are recovered from.Source§type Anchor = RcRef<SsContextWeb>
type Anchor = RcRef<SsContextWeb>
Self for the duration of the
invocation of the function that has an &Self parameter. This is
required to ensure that the lifetimes don’t persist beyond one function
call, and so that they remain anonymous.Source§impl RefMutFromWasmAbi for SsContextWeb
impl RefMutFromWasmAbi for SsContextWeb
Source§type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
type Abi = WasmPtr<WasmRefCell<SsContextWeb>>
RefFromWasmAbi::AbiSource§type Anchor = RcRefMut<SsContextWeb>
type Anchor = RcRefMut<SsContextWeb>
RefFromWasmAbi::AnchorSource§unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor
unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor
RefFromWasmAbi::ref_from_abiimpl SupportsConstructor for SsContextWeb
impl SupportsInstanceProperty for SsContextWeb
impl SupportsStaticProperty for SsContextWeb
Source§impl TryFromJsValue for SsContextWeb
impl TryFromJsValue for SsContextWeb
Source§fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
Source§const NULLABLE: bool = false
const NULLABLE: bool = false
Self accepts null as a distinct value space, as
Nullable<T> does. An enclosing Option then passes null through
instead of reading it as None, so Option<T> is itself nullable
exactly when T is not. JsValue does not set this: it accepts
null as one value among all of them, not as a distinct space, and
Option<JsValue> reads it as None like the ABI does. This is the
JS-value side of NullableFromWasmAbi, which claims null at the
ABI.Source§impl VectorFromWasmAbi for SsContextWeb
impl VectorFromWasmAbi for SsContextWeb
type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[SsContextWeb]>
Source§impl VectorIntoWasmAbi for SsContextWeb
impl VectorIntoWasmAbi for SsContextWeb
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi
fn vector_into_abi(vector: Box<[SsContextWeb]>) -> Self::Abi
Source§impl WasmDescribeVector for SsContextWeb
impl WasmDescribeVector for SsContextWeb
Auto Trait Implementations§
impl !RefUnwindSafe for SsContextWeb
impl !Send for SsContextWeb
impl !Sync for SsContextWeb
impl !UnwindSafe for SsContextWeb
impl Freeze for SsContextWeb
impl Unpin for SsContextWeb
impl UnsafeUnpin for SsContextWeb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
Source§type MaybeAbi = <T as FromWasmAbi>::Abi
type MaybeAbi = <T as FromWasmAbi>::Abi
Option<Self>.Source§unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>
Source§impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
Source§type MaybeAbi = <T as IntoWasmAbi>::Abi
type MaybeAbi = <T as IntoWasmAbi>::Abi
Option<Self>.Source§fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
Option<Self> ABI.impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.