Repository navigation
Camera: offset shifts the view for good, and a shake keeps it - #1768
Merged
Merged
Conversation
`Camera2d#offset` is now the one field for both a lasting look offset (above the hero, ahead of him) and the shake. The built-in shake used to write random values into it and zero it when done, wiping any offset the game had set. It now adds its jitter on top of the game's value and gives that value back exactly when it ends, also when the game changes the offset mid-shake, when the shake is removed or replaced early, or when two shakes run at once. Everything that works out what the camera shows now counts the offset: culling (`worldView`), the pointer (`localToWorld`, `worldToLocal`), tile layers (which picked their tiles from the camera position alone and left a strip undrawn) and parallax `ImageLayer`s. `reset()` clears it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Camera2d#offsetbecomes the one field for both a lasting look offset (above the hero, ahead of where he faces) and the shake. It was documented as "offset for shake effect" and only the shake used it: the built-in shake wrote random values into it and zeroed it when done, so any offset a game set was wiped by the next shake.Changes
Shake: adds its jitter on top of the game's offset and gives that value back exactly when it ends. This also holds when the game changes the offset mid-shake, when the shake is removed or replaced early, or when two shakes run at once (they add up). The bookkeeping stays inside
ShakeEffect; there is no new field on the camera.Everything that works out what the camera shows now counts the offset:
worldView)localToWorld,worldToLocal)ImageLayersDrawing, lights and fog already counted it.
reset()clears the offset, so it does not leak from one scene into the next.The
offsetJSDoc is rewritten with an example, and the camera skill gains a section on framing the target off centre.Behaviour change: parallax backgrounds now shake along with the world, scaled by their ratio. Before, they stood still during a shake. A background with ratio 0 is unaffected. The offset is applied after the camera's bounds, so the view can show past them.
Testing
tests/camera-offset.spec.js(21 tests). It covers:reset()mid-shake and a persistent shake15 deliberately broken versions of the code were all caught.
The full suite passes (8,788 tests) and
pnpm lintpasses.In the browser, every map of the Tiled map loader example on Canvas, WebGL and WebGPU, at zoom 1, 0.5 and 2: the camera at P with offset O draws the same frame, pixel for pixel, as the camera moved to P + O with no offset. With tile layers blind to the offset (the old code), 10 of the 12 maps had missing tiles.
🤖 Generated with Claude Code
https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx