# packages/core/src/features/sprites/model.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/model.ts#L60).

[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 { defineAnimation } from '../../lib/animation/definition.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> </span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> /** Pixel rectangle within an atlas page or original frame. See {@link SpriteFrame}. */</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a> export interface SpriteRectangle { /** Horizontal pixel offset. */ readonly x: number; /** Vertical pixel offset. */ readonly y: number; /** Width in pixels. */ readonly width: number; /** Height in pixels. */ readonly height: number }</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a> /** Normalized frame information returned by a format adapter. See {@link SpriteFrame}. */</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a> export interface SpriteFrameData {</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a>   /** Unique frame name in this sheet. */ readonly name: string;</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a>   /** Zero-based atlas page index; defaults to zero. */ readonly page?: number;</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a>   /** Packed rectangle within the atlas page. */ readonly rect: SpriteRectangle;</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a>   /** Original frame dimensions before trimming. */ readonly sourceSize?: Readonly&lt;{ /** Width in pixels. */ width: number; /** Height in pixels. */ height: number }&gt;;</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a>   /** Trimmed content offset within the original frame. */ readonly trim?: Readonly&lt;{ /** Horizontal pixel offset. */ x: number; /** Vertical pixel offset. */ y: number }&gt;;</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a>   /** Whether the packed rectangle is rotated clockwise. */ readonly rotated?: boolean;</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a> }</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a> /** One normalized timed frame reference. Durations are milliseconds. See {@link SpriteFrame}. */</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a> export interface SpriteAnimationFrameData { /** Name of the frame used by this animation step. */ readonly frame: string; /** Positive duration of the animation step in milliseconds. */ readonly duration: number }</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a> /** Adapter result. Image paths resolve relative to the metadata URL. See {@link SpriteFrame}. */</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a> export interface SpriteSheetData {</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>   /** Atlas image URLs, relative to the metadata URL. */ readonly images: readonly string[];</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>   /** Normalized frame records for this sheet. */ readonly frames: readonly SpriteFrameData[];</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>   /** Named sequences of timed frame references. */ readonly animations?: Readonly&lt;Record&lt;string, readonly SpriteAnimationFrameData[]&gt;&gt;;</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> /** Import adapter; loaders invoke this directly without a format registry. See {@link SpriteFrame}. */</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a> export interface SpriteSheetFormat { /** Convert decoded metadata to normalized sheet data.</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>  * @param data - Parsed atlas JSON.</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>  * @returns Validated adapter data for loading the sheet.</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>  */ readonly parse: (data: unknown) =&gt; SpriteSheetData }</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a> </span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a> export interface FrameRecord extends SpriteFrameData { readonly image: HTMLImageElement; readonly owner: { disposed: boolean } }</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a> const records = new WeakMap&lt;SpriteFrame, FrameRecord&gt;();</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a> /** Immutable atlas frame. Obtain frames from loadSpriteSheet; images belong to the sheet. See {@link SpriteFrame}. */</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a> export class SpriteFrame {</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a>   private readonly identity = true;</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>   /** Original logical frame width, before transparent borders were trimmed.</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>    * @returns Width in logical pixels.</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>    */</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>   get width(): number { return frameRecord(this).sourceSize?.width ?? frameRecord(this).rect.width; }</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a>   /** Original logical frame height, before transparent borders were trimmed.</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a>    * @returns Height in logical pixels.</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>   get height(): number { return frameRecord(this).sourceSize?.height ?? frameRecord(this).rect.height; }</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>   /** Stable name supplied by the atlas or numeric grid index.</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>    * @returns The frame name.</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>    */</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a>   get name(): string { void this.identity; return frameRecord(this).name; }</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a> }</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a> export function makeFrame(record: FrameRecord): SpriteFrame {</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>   const frame = new SpriteFrame(); records.set(frame, record); Object.freeze(frame); return frame;</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a> }</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a> export function frameRecord(frame: SpriteFrame): FrameRecord {</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a>   const record = records.get(frame);</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>   if (!record) throw new TypeError('Expected a SpriteFrame from loadSpriteSheet.');</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>   if (record.owner.disposed) throw new Error('Sprite sheet has been disposed.');</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>   return record;</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a> }</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a> /** Author a finite discrete animation using FPS or explicit positive millisecond durations. See {@link SpriteFrame}.</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>  * @param options - Frames with a shared FPS or individual millisecond durations.</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>  * @returns A seekable discrete animation definition.</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> export function defineSpriteAnimation(options: {</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>   readonly frames: readonly SpriteFrame[];</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>   readonly fps: number;</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a> } | {</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a>   readonly frames: readonly Readonly&lt;{ frame: SpriteFrame; duration: number }&gt;[];</span>
<span id="L65"><a href="#L65" aria-label="Line 65">65</a> }): PibblAnimationDefinition&lt;SpriteFrame&gt; {</span>
<span id="L66"><a href="#L66" aria-label="Line 66">66</a>   if (!options.frames.length) throw new RangeError('Sprite animation requires at least one frame.');</span>
<span id="L67"><a href="#L67" aria-label="Line 67">67</a>   if ('fps' in options &amp;&amp; (!Number.isFinite(options.fps) || options.fps &lt;= 0)) throw new RangeError('Sprite FPS must be finite and positive.');</span>
<span id="L68"><a href="#L68" aria-label="Line 68">68</a>   const entries = options.frames.map(item =&gt; {</span>
<span id="L69"><a href="#L69" aria-label="Line 69">69</a>     const entry = 'fps' in options ? { frame: item as SpriteFrame, duration: 1000 / options.fps } : item as { frame: SpriteFrame; duration: number };</span>
<span id="L70"><a href="#L70" aria-label="Line 70">70</a>     frameRecord(entry.frame);</span>
<span id="L71"><a href="#L71" aria-label="Line 71">71</a>     if (!Number.isFinite(entry.duration) || entry.duration &lt;= 0) throw new RangeError('Sprite duration must be finite and positive.');</span>
<span id="L72"><a href="#L72" aria-label="Line 72">72</a>     return { ...entry };</span>
<span id="L73"><a href="#L73" aria-label="Line 73">73</a>   });</span>
<span id="L74"><a href="#L74" aria-label="Line 74">74</a>   let duration = 0;</span>
<span id="L75"><a href="#L75" aria-label="Line 75">75</a>   const ends = entries.map(entry =&gt; duration += entry.duration);</span>
<span id="L76"><a href="#L76" aria-label="Line 76">76</a>   return defineAnimation({ duration, sample: ({ localTime }) =&gt; {</span>
<span id="L77"><a href="#L77" aria-label="Line 77">77</a>     let low = 0, high = ends.length - 1;</span>
<span id="L78"><a href="#L78" aria-label="Line 78">78</a>     while (low &lt; high) { const mid = (low + high) &gt;&gt;&gt; 1; if (localTime &lt; ends[mid]) high = mid; else low = mid + 1; }</span>
<span id="L79"><a href="#L79" aria-label="Line 79">79</a>     return entries[low].frame;</span>
<span id="L80"><a href="#L80" aria-label="Line 80">80</a>   } });</span>
<span id="L81"><a href="#L81" aria-label="Line 81">81</a> }</span>
<span id="L82"><a href="#L82" aria-label="Line 82">82</a> </span></code></pre>

## Documentation version

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