@linxin666/dsh-pet 0.4.7 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -63,7 +63,10 @@
63
63
  0 4px 12px rgba(2, 6, 23, 0.35),
64
64
  inset 0 1px 0 rgba(226, 232, 255, 0.1),
65
65
  0 0 12px rgba(77, 107, 254, 0.18);
66
- backdrop-filter: blur(8px);
66
+ /* No backdrop-filter (issue #17): the glass is 90-94% opaque, so the blur was
67
+ invisible while forcing this permanently-mounted layer to re-composite the
68
+ page behind it on every sprite frame change; a blurred fixed layer costs
69
+ CPU on software-compositing and power-saving paths. */
67
70
  letter-spacing: 0.02em;
68
71
  animation: pet-bubble-in 240ms ease-out;
69
72
  }
@@ -242,7 +245,8 @@
242
245
  0 8px 24px rgba(2, 6, 23, 0.45),
243
246
  0 0 0 1px rgba(77, 107, 254, 0.1),
244
247
  inset 0 1px 0 rgba(226, 232, 255, 0.08);
245
- backdrop-filter: blur(10px);
248
+ /* No backdrop-filter (issue #17): at 95-97% opacity the blur never showed,
249
+ and the panel overlaid the sprite's own animation while it was open. */
246
250
  display: flex;
247
251
  flex-direction: column;
248
252
  gap: 7px;
@@ -657,7 +661,9 @@
657
661
  0 4px 12px rgba(2, 6, 23, 0.35),
658
662
  inset 0 1px 0 rgba(226, 232, 255, 0.1),
659
663
  0 0 12px rgba(77, 107, 254, 0.18);
660
- backdrop-filter: blur(8px);
664
+ /* No backdrop-filter (issue #17), same reason as .bubbleStatus: a 92-95%
665
+ opaque pill gains nothing visible from the blur but pays for the filter on
666
+ every frame the pet animates. */
661
667
  letter-spacing: 0.02em;
662
668
  animation: pet-usage-in 280ms cubic-bezier(0.22, 1, 0.36, 1);
663
669
  }
@@ -36,7 +36,7 @@ describe('createSequenceTimeline', () => {
36
36
  const sequence: PetAnimation[] = ['running', 'waiting']
37
37
 
38
38
  it('resolves identically to the per-call helper at animation-sample elapsed values', () => {
39
- // The sprite frame loop asks once per rAF tick with accumulated
39
+ // The sprite frame loop asks once per frame tick with accumulated
40
40
  // millisecond offsets; the precomputed table must agree everywhere.
41
41
  const timeline = createSequenceTimeline(sequence, tracks)
42
42
  for (let elapsed = 0; elapsed <= 1_500; elapsed += 7) {
@@ -51,3 +51,39 @@ describe('createSequenceTimeline', () => {
51
51
  expect(timeline.frameAt(1_420)).toEqual({ animation: 'running', frameIndex: 0 })
52
52
  })
53
53
  })
54
+
55
+ describe('SequenceTimeline.remainingAt (issue #17)', () => {
56
+ const sequence: PetAnimation[] = ['running', 'waiting']
57
+
58
+ it('reports the time left in the frame the query lands on', () => {
59
+ const timeline = createSequenceTimeline(sequence, tracks)
60
+ // running frames are 100ms then 200ms, waiting 300ms then 100ms.
61
+ expect(timeline.remainingAt(0)).toBe(100)
62
+ expect(timeline.remainingAt(40)).toBe(60)
63
+ expect(timeline.remainingAt(100)).toBe(200)
64
+ expect(timeline.remainingAt(299)).toBe(1)
65
+ // The second item's first frame, measured from the item boundary.
66
+ expect(timeline.remainingAt(300)).toBe(300)
67
+ expect(timeline.remainingAt(600)).toBe(100)
68
+ })
69
+
70
+ it('accounts for a loop wrap without overshooting', () => {
71
+ const timeline = createSequenceTimeline(sequence, tracks)
72
+ // 700ms is one full sequence: the wrap lands on running frame 0 again.
73
+ expect(timeline.frameAt(700)).toEqual({ animation: 'running', frameIndex: 0 })
74
+ expect(timeline.remainingAt(700)).toBe(100)
75
+ expect(timeline.remainingAt(750)).toBe(50)
76
+ })
77
+
78
+ it('steps over a zero-length frame instead of reporting a zero delay', () => {
79
+ const zeroTracks = {
80
+ running: track([0, 200]),
81
+ waiting: track([100]),
82
+ } as Record<PetAnimation, PetTrackDef>
83
+ const timeline = createSequenceTimeline(sequence, zeroTracks)
84
+ // The loop schedules its next wake from this value, so a zero-duration
85
+ // frame must resolve to the next real frame rather than to a 0ms timer.
86
+ expect(timeline.remainingAt(0)).toBe(200)
87
+ expect(timeline.remainingAt(200)).toBe(100)
88
+ })
89
+ })
@@ -10,11 +10,26 @@ export interface SequenceFrame {
10
10
 
11
11
  /**
12
12
  * Precomputed timeline for one looping sequence: the per-item duration table
13
- * is built once instead of per query. The sprite frame loop asks at
14
- * animation rate (~60 Hz), where the per-call map/reduce is pure waste.
13
+ * is built once instead of per query. The sprite frame loop asks once per
14
+ * frame change, where the per-call map/reduce is pure waste.
15
15
  */
16
16
  export interface SequenceTimeline {
17
17
  frameAt(elapsedMs: number): SequenceFrame
18
+ /**
19
+ * Milliseconds from `elapsedMs` until the frame it lands on ends. The sprite
20
+ * loop sleeps for exactly this long instead of polling at the screen refresh
21
+ * rate (issue #17); the floor of 1 ms keeps a zero-length frame from turning
22
+ * into a busy timer.
23
+ */
24
+ remainingAt(elapsedMs: number): number
25
+ }
26
+
27
+ /** One sequence position, with the residual offset inside its own frame. */
28
+ interface SequencePoint {
29
+ animation: PetAnimation
30
+ track: PetTrackDef
31
+ frameIndex: number
32
+ offsetInFrame: number
18
33
  }
19
34
 
20
35
  /** Build a {@link SequenceTimeline} over one manifest sequence. */
@@ -24,22 +39,33 @@ export function createSequenceTimeline(
24
39
  ): SequenceTimeline {
25
40
  const itemDurations = sequence.map(animation => tracks[animation].durations.reduce((sum, value) => sum + value, 0))
26
41
  const sequenceDuration = itemDurations.reduce((sum, value) => sum + value, 0)
42
+ // One traversal serves both queries: frameAt needs the index it lands on and
43
+ // remainingAt needs how far into that frame it is, so resolving them
44
+ // separately would walk the same tables twice for one loop tick.
45
+ const locate = (elapsedMs: number): SequencePoint => {
46
+ let offset = sequenceDuration > 0 ? Math.max(0, elapsedMs) % sequenceDuration : 0
47
+ let itemIndex = 0
48
+ while (itemIndex < sequence.length - 1 && offset >= itemDurations[itemIndex]!) {
49
+ offset -= itemDurations[itemIndex]!
50
+ itemIndex += 1
51
+ }
52
+ const animation = sequence[itemIndex]!
53
+ const track = tracks[animation]
54
+ let frameIndex = 0
55
+ while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]!) {
56
+ offset -= track.durations[frameIndex]!
57
+ frameIndex += 1
58
+ }
59
+ return { animation, track, frameIndex, offsetInFrame: offset }
60
+ }
27
61
  return {
28
62
  frameAt(elapsedMs: number): SequenceFrame {
29
- let offset = Math.max(0, elapsedMs) % sequenceDuration
30
- let itemIndex = 0
31
- while (itemIndex < sequence.length - 1 && offset >= itemDurations[itemIndex]!) {
32
- offset -= itemDurations[itemIndex]!
33
- itemIndex += 1
34
- }
35
- const animation = sequence[itemIndex]!
36
- const track = tracks[animation]
37
- let frameIndex = 0
38
- while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]!) {
39
- offset -= track.durations[frameIndex]!
40
- frameIndex += 1
41
- }
42
- return { animation, frameIndex }
63
+ const point = locate(elapsedMs)
64
+ return { animation: point.animation, frameIndex: point.frameIndex }
65
+ },
66
+ remainingAt(elapsedMs: number): number {
67
+ const point = locate(elapsedMs)
68
+ return Math.max(1, (point.track.durations[point.frameIndex] ?? 0) - point.offsetInFrame)
43
69
  },
44
70
  }
45
71
  }
@@ -0,0 +1,81 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import type { PetCell } from '../registry.ts'
3
+ import { framePosition, scaledCellSize, trimTrack } from './spritesheet.ts'
4
+
5
+ /** The built-in hatch-pet cell (192 x 208). */
6
+ const CELL: PetCell = { width: 192, height: 208 }
7
+
8
+ /** The whale-girl display: `display.size` 160 over a 208px-tall cell. */
9
+ const SCALE = 160 / CELL.height
10
+
11
+ describe('scaledCellSize (issue #17)', () => {
12
+ it('rounds the scaled cell so a fractional sprite scale stays on whole pixels', () => {
13
+ // 192 * 160/208 = 147.69px and 208 * 160/208 = 160px before rounding.
14
+ expect(scaledCellSize(CELL, SCALE)).toEqual({ width: 148, height: 160 })
15
+ })
16
+
17
+ it('agrees with the sprite box the component renders for the same scale', () => {
18
+ // The element is sized from the same helper, so a cell of the scaled atlas
19
+ // and the box that shows it are the same rectangle instead of drifting by
20
+ // a fraction of a pixel per frame.
21
+ const scaled = scaledCellSize(CELL, SCALE)
22
+ expect(Math.round(CELL.width * SCALE)).toBe(scaled.width)
23
+ expect(Math.round(CELL.height * SCALE)).toBe(scaled.height)
24
+ })
25
+
26
+ it('floors a collapsed cell at one pixel instead of rendering nothing', () => {
27
+ expect(scaledCellSize(CELL, 0.001)).toEqual({ width: 1, height: 1 })
28
+ })
29
+ })
30
+
31
+ describe('framePosition (issue #17)', () => {
32
+ it('places every column on an exact multiple of the scaled cell width', () => {
33
+ const scaled = scaledCellSize(CELL, SCALE)
34
+ for (let col = 0; col < 8; col += 1) {
35
+ const pos = framePosition(CELL, 0, col, SCALE)
36
+ expect(pos.x).toBe(-col * scaled.width)
37
+ expect(Number.isInteger(pos.x)).toBe(true)
38
+ // Row 0 is the top of the atlas; the offset is -0, which is still whole.
39
+ expect(Math.abs(pos.y)).toBe(0)
40
+ }
41
+ })
42
+
43
+ it('places every row on an exact multiple of the scaled cell height', () => {
44
+ const scaled = scaledCellSize(CELL, SCALE)
45
+ for (let row = 0; row < 9; row += 1) {
46
+ const pos = framePosition(CELL, row, 0, SCALE)
47
+ expect(pos.y).toBe(-row * scaled.height)
48
+ expect(Number.isInteger(pos.y)).toBe(true)
49
+ expect(Math.abs(pos.x)).toBe(0)
50
+ }
51
+ })
52
+
53
+ it('never returns a fractional offset at a scale that does not divide the cell', () => {
54
+ // 120/208 leaves 110.77px cells: the old formula returned -110.77px per
55
+ // column, which is what forced the compositor to resample the atlas.
56
+ const scale = 120 / CELL.height
57
+ const scaled = scaledCellSize(CELL, scale)
58
+ expect(scaled).toEqual({ width: 111, height: 120 })
59
+ expect(framePosition(CELL, 5, 7, scale)).toEqual({ x: -7 * 111, y: -5 * 120 })
60
+ })
61
+
62
+ it('keeps the raw atlas grid when no scale is applied', () => {
63
+ expect(framePosition(CELL, 3, 5)).toEqual({ x: -5 * CELL.width, y: -3 * CELL.height })
64
+ })
65
+ })
66
+
67
+ describe('trimTrack', () => {
68
+ it('trims to the smaller of the track table and the row frame count', () => {
69
+ const track = { frames: [0, 1, 2, 3, 4, 5], durations: [1, 2, 3, 4, 5, 6], loop: true }
70
+ expect(trimTrack(track, 4)).toEqual({
71
+ frames: [0, 1, 2, 3],
72
+ durations: [1, 2, 3, 4],
73
+ loop: true,
74
+ })
75
+ })
76
+
77
+ it('degrades a row with no detected frames to the first frame', () => {
78
+ const track = { frames: [0, 1], durations: [10, 20], loop: false, fallback: 'idle' as const }
79
+ expect(trimTrack(track, 0)).toEqual({ frames: [0], durations: [10], loop: false, fallback: 'idle' })
80
+ })
81
+ })
@@ -19,15 +19,40 @@ export function rowOfTrack(animation: PetAnimation): number {
19
19
  return rowOf(animation)
20
20
  }
21
21
 
22
+ /**
23
+ * Integer size of one cell after `scale`, in device-independent px.
24
+ *
25
+ * Rounding happens here, once, so the rendered atlas grid is integral end to
26
+ * end (issue #17): `background-size` is this size times the column/row count
27
+ * and every frame offset is an exact multiple of it. A fractional cell size
28
+ * leaves each frame on a sub-pixel boundary — 192px cells at `display.size`
29
+ * 160 scale to 147.69px — and the compositor then has to resample the whole
30
+ * scaled atlas on every frame change instead of translating an already-scaled
31
+ * bitmap by whole pixels. The pet advances a frame every 300-700 ms, so that
32
+ * resample was running continuously for a background surface the size of the
33
+ * sprite. A floor of 1px keeps a legally tiny display.size from collapsing the
34
+ * cell to zero and rendering nothing.
35
+ */
36
+ export function scaledCellSize(cell: PetCell, scale: number): { width: number; height: number } {
37
+ return {
38
+ width: Math.max(1, Math.round(cell.width * scale)),
39
+ height: Math.max(1, Math.round(cell.height * scale)),
40
+ }
41
+ }
42
+
22
43
  /**
23
44
  * Background-position (px) of one frame cell within the scaled atlas.
24
45
  * The background image is scaled by `scale` (element size ÷ cell size), and
25
46
  * background-position offsets are applied in SCALED coordinates — using raw
26
47
  * atlas coordinates here would drift each frame by the scale factor and
27
- * render torn/overlapping frames.
48
+ * render torn/overlapping frames. The scaled cell is quantized by
49
+ * {@link scaledCellSize}, so the offset of frame N is exactly N whole cells:
50
+ * the atlas is snapped to the pixel grid and a frame change is a plain
51
+ * translate of the scaled bitmap.
28
52
  */
29
53
  export function framePosition(cell: PetCell, row: number, col: number, scale = 1): { x: number; y: number } {
30
- return { x: -col * cell.width * scale, y: -row * cell.height * scale }
54
+ const scaled = scaledCellSize(cell, scale)
55
+ return { x: -col * scaled.width, y: -row * scaled.height }
31
56
  }
32
57
 
33
58
  /**
@@ -316,13 +316,14 @@ describe('loadPetRegistry', () => {
316
316
 
317
317
  // The repo checkout also resolves doro and miku (frames2d gameplay pets),
318
318
  // jyn, jyn-foxtail and jyn-loli (frames2d gameplay pets), long-niang
319
- // (sprite2d pet) and starry-doll (community sprite2d pet) from
319
+ // (sprite2d pet), starry-doll and deep-chan (community sprite2d pets) from
320
320
  // assets/; the npm files whitelist excludes them (Workshop delivery), so
321
321
  // npm installs see the atlas pets until a Workshop install lands them
322
322
  // under $DSH_HOME/pets. blue-throated-bee-eater ships bundled alongside
323
323
  // the other atlas pets.
324
324
  expect(registry.entries.map(entry => entry.id)).toEqual([
325
325
  'blue-throated-bee-eater',
326
+ 'deep-chan',
326
327
  'doro',
327
328
  'jyn',
328
329
  'jyn-foxtail',