# packages/core/src/features/sprites/loader.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/272a94aaf62e0bd6ad8726a4c607a76a9ec44ca1/packages/core/src/features/sprites/loader.ts#L16).

[Back to reference](/reference/functions/sprite-sheets/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import { defineSpriteAnimation, makeFrame, type SpriteFrame, type SpriteFrameData, type SpriteSheetFormat } from './model.js';</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> import type { PibblAnimationDefinition } from '../../lib/animation/types.js';</span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> /** Shared decoded atlas and immutable frames/clips. Dispose after all consumers unmount. See {@link SpriteFrame}. */</span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> export interface SpriteSheet {</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a>   /** Frames indexed by atlas name or numeric grid index. */ readonly frames: Readonly&lt;Record&lt;string | number, SpriteFrame&gt;&gt;;</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a>   /** Named, seekable animation definitions from the atlas. */ readonly animations: Readonly&lt;Record&lt;string, PibblAnimationDefinition&lt;SpriteFrame&gt;&gt;&gt;;</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a>   /** Release decoded images; unmount every consumer before calling. */ readonly dispose: () =&gt; void;</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a> }</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a> /** Dimensions of a regular grid, in image pixels. See {@link SpriteFrame}. */</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a> export interface SpriteGrid { /** Positive cell width in pixels. */ readonly frameWidth: number; /** Positive cell height in pixels. */ readonly frameHeight: number; /** Outer border width in pixels; defaults to zero. */ readonly margin?: number; /** Gap between cells in pixels; defaults to zero. */ readonly spacing?: number }</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a> /** Load a grid image or metadata through an explicitly supplied import adapter. See {@link SpriteFrame}. </span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a>  * @param url - Grid image URL or atlas metadata URL.</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a>  * @param options - Grid dimensions or an explicit metadata format adapter.</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a>  * @returns A decoded sprite sheet with explicit disposal ownership.</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a>  */</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a> export async function loadSpriteSheet(url: string, options: { readonly grid: SpriteGrid } | { readonly format: SpriteSheetFormat }): Promise&lt;SpriteSheet&gt; {</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a>   const owner = { disposed: false };</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a>   const images: HTMLImageElement[] = [];</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>   const dispose = () =&gt; { if (owner.disposed) return; owner.disposed = true; for (const image of images) image.removeAttribute('src'); };</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>   const load = async (source: string) =&gt; {</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>     const image = new globalThis.Image(); images.push(image); image.src = source; await image.decode(); return image;</span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a>   };</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>   try {</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>     let specs: readonly SpriteFrameData[];</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>     let clips: Readonly&lt;Record&lt;string, readonly { frame: string; duration: number }[]&gt;&gt; = {};</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>     if ('grid' in options) {</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>       const { frameWidth: w, frameHeight: h, margin = 0, spacing = 0 } = options.grid;</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>       if (![w, h, margin, spacing].every(Number.isInteger) || w &lt;= 0 || h &lt;= 0 || margin &lt; 0 || spacing &lt; 0) throw new RangeError('Invalid sprite grid dimensions.');</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a>       const image = await load(url); const frames: SpriteFrameData[] = [];</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a>       for (let y = margin; y + h &lt;= image.naturalHeight - margin; y += h + spacing)</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a>         for (let x = margin; x + w &lt;= image.naturalWidth - margin; x += w + spacing)</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a>           frames.push({ name: String(frames.length), rect: { x, y, width: w, height: h } });</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a>       if (!frames.length) throw new RangeError('Sprite grid contains no complete frames.');</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>       specs = frames;</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>     } else {</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>       const response = await fetch(url); if (!response.ok) throw new Error(`Sprite metadata failed: ${response.status}`);</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>       const data = options.format.parse(await response.json());</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a>       for (const path of data.images) await load(new URL(path, new URL(url, document.baseURI)).href);</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a>       specs = data.frames; clips = data.animations ?? {};</span>
<span id="L40"><a href="#L40" aria-label="Line 40">40</a>     }</span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a>     const frames: Record&lt;string, SpriteFrame&gt; = Object.create(null);</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>     for (const spec of specs) {</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>       const image = images[spec.page ?? 0], r = spec.rect;</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>       if (!image || ![r.x, r.y, r.width, r.height].every(Number.isFinite) || r.x &lt; 0 || r.y &lt; 0 || r.width &lt;= 0 || r.height &lt;= 0 || r.x + r.width &gt; image.naturalWidth || r.y + r.height &gt; image.naturalHeight) throw new RangeError(`Invalid sprite rectangle: ${spec.name}`);</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a>       if (frames[spec.name]) throw new Error(`Duplicate sprite frame: ${spec.name}`);</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>       const width = spec.sourceSize?.width ?? (spec.rotated ? r.height : r.width);</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a>       const height = spec.sourceSize?.height ?? (spec.rotated ? r.width : r.height);</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>       const trim = { x: spec.trim?.x ?? 0, y: spec.trim?.y ?? 0 };</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a>       if (![width, height, trim.x, trim.y].every(Number.isFinite) || width &lt;= 0 || height &lt;= 0 || trim.x &lt; 0 || trim.y &lt; 0 || trim.x + (spec.rotated ? r.height : r.width) &gt; width || trim.y + (spec.rotated ? r.width : r.height) &gt; height) throw new RangeError(`Invalid sprite logical bounds: ${spec.name}`);</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a>       frames[spec.name] = makeFrame({ ...spec, rect: Object.freeze({ ...r }), sourceSize: Object.freeze({ width, height }), trim: Object.freeze(trim), image, owner });</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a>     }</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>     const animations: Record&lt;string, PibblAnimationDefinition&lt;SpriteFrame&gt;&gt; = Object.create(null);</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>     for (const [name, entries] of Object.entries(clips)) animations[name] = defineSpriteAnimation({ frames: entries.map(entry =&gt; {</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>       if (!frames[entry.frame]) throw new Error(`Unknown sprite frame: ${entry.frame}`);</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a>       return { frame: frames[entry.frame], duration: entry.duration };</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a>     }) });</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>     return Object.freeze({ frames: Object.freeze(frames), animations: Object.freeze(animations), dispose });</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>   } catch (error) { dispose(); throw error; }</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a> }</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a> </span></code></pre>

## Documentation version

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.
