/** * A sprite's preview in the inspector: magnified by whole device pixels with its corner on one, so * pixel art stays sharp; shrunk freely when it does not fit, and with room for a shader's padding. */ import { describe, expect, it } from 'vitest'; import { previewFit } from '../../src/editor/inspector/previewFit'; const room = { w: 300, h: 140 }; const options = { margin: 8, maxZoom: 8, ratio: 1 }; /** Whether `v` device pixels is a whole one, within float noise. */ const whole = (v: number) => Math.abs(v - Math.round(v)) < 1e-9; describe('previewFit', () => { it('magnifies a small sprite by a whole factor and centres it on whole pixels', () => { // 124 / 15 = 8.27 is capped at 8; the 120-px picture leaves 90 and 10 at the sides. const fit = previewFit(15, 15, room, options); expect(fit).toEqual({ zoom: 8, x: 90, y: 10 }); }); it('rounds the zoom down to whole device pixels', () => { // 124 / 48 = 2.58: two pixels per unit, not a blur between two and three. expect(previewFit(48, 48, room, options).zoom).toBe(2); // At a device ratio of 1.5 the same room holds 3.875 device pixels per unit: three, = 2 panel px. const fit = previewFit(48, 48, room, { ...options, ratio: 1.5 }); expect(fit.zoom).toBe(2); expect(whole(fit.x * 1.5) && whole(fit.y * 1.5)).toBe(true); }); it('puts the corner of an odd size on a whole device pixel', () => { const fit = previewFit(15, 9, room, { ...options, ratio: 1.25 }); expect(whole(fit.zoom * 1.25)).toBe(true); expect(whole(fit.x * 1.25) && whole(fit.y * 1.25)).toBe(true); }); it('shrinks a sprite bigger than the room to fit, without rounding to nothing', () => { const fit = previewFit(800, 200, room, options); expect(fit.zoom).toBeCloseTo(284 / 800); expect(800 * fit.zoom).toBeLessThanOrEqual(room.w - 16); }); it("keeps room for a shader's padding", () => { // 10 % on each side: 124 / (15 * 1.2) = 6.9 → 6 rather than 8. expect(previewFit(15, 15, room, { ...options, pad: { u: 0.1, v: 0.1 } }).zoom).toBe(6); }); });