Files
gabrielvidalandClaude Opus 5.5 fffb4f9a98
Deploy demo to GitHub Pages / build (push) Canceled after 0s
Deploy demo to GitHub Pages / deploy (push) Canceled after 0s
feat: pet.goNear(x, y) — walk to the surface under a point
For several pets on one page to find each other: there is no element to
goTo, only the other pet's feet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 23:50:59 +01:00

2.8 KiB

Public API

Entry: packages/pet-pet/src/index.ts. Zero dependencies, works in any page. Install and bundles: package.

import { createPet } from 'pet-pet'
const pet = createPet({ container: document.querySelector('main'), size: '10vh' })

Options (PetOptions)

key default notes
container viewport Bounds the pet lives in (client box ∩ viewport).
root document Subtree scanned for platforms. Use the container to skip overlays.
size '10vh' Body length. number (px) or 'Npx' / 'Nvh' / 'Nvw' / 'Nvmin', re-resolved on resize.
preset golden Palette: golden grey cream choco (see rendering).
shape soft Silhouette preset: soft loaf head stripe block.
appearance / physics / behavior / platforms / debug see config.ts Partial overrides. platforms.cull (body lengths, 0 = off) scans only near the bounds (world).
motion { blend: 1, turnTime: 0.18 } Transition duration multiplier (0 = off), left/right turn in seconds (animations).
spawn random x, top { x, y } in viewport px; the pet falls in.

Methods

  • react(name, { say?, hold?, duration? }) → Promise<boolean>: plays a reaction. Aliases: hello→wave celebrate→success loading→thinking ok→approve no→cancel. hold loops until another task replaces it.
  • say(text, ms?): speech bubble.
  • goTo(elementOrSelector) → Promise<boolean>: path-plans to that element's top (world).
  • goNear(x, y) → Promise<boolean>: path-plans to the surface under a viewport point (another pet's feet, say).
  • feed(x?): drops a seed; the pet goes and eats it.
  • sleep() / wake(), refresh() (rescan platforms), set(patch) (live config), state, destroy().
  • idle(): ends the current task (a held reaction, a walk); the brain picks its own next one.
  • hidden (get/set): hides the pet's layer without stopping it, e.g. while its container is scrolled out of a clipping ancestor (the bounds only clip to the viewport).
  • on(event, fn) → off, off(event, fn).

Events

pet {count} · grab {part} · throw {speed} · impact {speed,x,y} · land {surface,height} · state {task,anim} · react {name} · eat {x,y} · sleep · wake. Speeds are in body lengths per second.

Data attributes

  • data-pet-react="approve" (+ data-pet-say="…"): click triggers a reaction.
  • data-pet-say="…": click shows a bubble.
  • data-pet-platform: force an element to be a platform.
  • data-pet-ignore: exclude an element and its subtree from platforms and typing watch.

Typing in input / textarea / contenteditable inside the container makes the pet come and watch (brain).