Skip to content

Refactor visualizer to use shared config ref, add DPR canvas scaling and stable draw updates - #10

Open
joesterne wants to merge 1 commit into
mainfrom
codex/update-resize-function-for-canvas-scaling
Open

Refactor visualizer to use shared config ref, add DPR canvas scaling and stable draw updates#10
joesterne wants to merge 1 commit into
mainfrom
codex/update-resize-function-for-canvas-scaling

Conversation

@joesterne

Copy link
Copy Markdown
Owner

Motivation

  • Ensure visualizer state updates are consistent between incoming messages and UI controls by centralizing config in a mutable ref.
  • Improve rendering fidelity on high-DPI displays by scaling the canvas and drawing with device pixel ratio awareness.

Description

  • Introduce visualizerConfigRef to hold { mode, color, density, speed } and add update helpers updateMode, updateColor, updateDensity, and updateSpeed to keep ref and React state in sync.
  • Rework the draw function to read configuration from visualizerConfigRef, use playbackSpeed derived from the ref, and apply ctx.setTransform for proper DPR-aware rendering.
  • Move and simplify the message event handler to update the ref safely, only call set* when values change, and call draw with the stored speed.
  • Update canvas resize logic to set pixel dimensions multiplied by window.devicePixelRatio, set CSS size, and preapply transform in the resize handler.
  • Wire UI controls to the new update helpers so adjustments update both the ref and component state.

Testing

  • No automated tests were run for this change.

Codex Task

### Motivation

- Ensure the popout visualizer renders crisply on high-DPI displays by sizing the canvas backing store to the device pixel ratio and keeping drawing coordinates in CSS pixels.

### Description

- Use `const dpr = window.devicePixelRatio || 1` when resizing the canvas and in the draw path.
- Set the backing store size with `canvas.width = window.innerWidth * dpr` and `canvas.height = window.innerHeight * dpr` while preserving CSS size with `canvas.style.width = '100vw'` and `canvas.style.height = '100vh'`.
- Scale the 2D context with `ctx.setTransform(dpr, 0, 0, dpr, 0, 0)` so renderer coordinates remain in CSS pixels and drawing remains correctly sized.
- Compute logical drawing dimensions as `const width = canvas.width / dpr` and `const height = canvas.height / dpr` and clear/draw using those values.

### Testing

- Ran `git diff --check` which passed without issues.
- Ran `npm run lint` (`tsc --noEmit`) which failed due to pre-existing duplicate block-scoped declarations in `src/components/Visualizer.tsx` unrelated to these changes, so lint did not complete successfully on the full project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant