Skip to content

Add canvas 2d clearing system#324

Merged
waynemwashuma merged 2 commits into
wimaengine:devfrom
waynemwashuma:add-canvas-clearing-system
May 27, 2026
Merged

Add canvas 2d clearing system#324
waynemwashuma merged 2 commits into
wimaengine:devfrom
waynemwashuma:add-canvas-clearing-system

Conversation

@waynemwashuma
Copy link
Copy Markdown
Collaborator

Objective

Introduce deterministic frame clearing for the Canvas2D renderer and avoid redundant window resize operations. Previously, Canvas2D rendering accumulated previous frame contents because the canvas was never cleared automatically before rendering. Additionally, resize commands always mutated the canvas even when dimensions were unchanged. This adds a dedicated canvas clearing system and optimizes resize handling.

Solution

Root cause

The Canvas2D renderer intentionally skipped clearing the canvas each frame. This caused rendered frames to persist between updates, producing visual artifacts such as:

  • ghosting
  • overdraw accumulation
  • stale frame remnants
  • incorrect motion rendering

Separately, window resize commands always triggered canvas dimension writes even when dimensions were identical, causing unnecessary canvas resets and potential rendering invalidation. This is what previously cleared the frames

Solution

Added dedicated clearCanvas2d system (scheduled at the beginning of update schedule) which:

  • resolves the main window canvas
  • acquires the 2d rendering context
  • clears the frame using:

Frame clearing is now treated as a first-class rendering stage rather than implicit renderer behavior.

Optimized resize handling

Added an early bailout for resize commands preventing unnecessary:

  • canvas reallocations
  • rendering context invalidation
  • framebuffer resets
  • resize-triggered redraw work

when dimensions have not changed.

Why this approach

Alternative approaches considered:

  • keeping clearing inside render systems
  • clearing per material renderer
  • clearing conditionally during render submission

Those approaches couple frame lifecycle management to renderer implementations and make multi-pass rendering harder to reason about.

Using a dedicated clearing system:

  • centralizes frame preparation
  • aligns with ECS scheduling principles
  • improves renderer composability
  • enables future configurable clear stages
  • preserves clean render pipeline separation

The resize optimization similarly avoids unnecessary canvas mutations with minimal complexity.

Showcase

N/A

Migration guide

No migration required.

Behavioral change

Canvas2D rendering now clears the frame automatically every update cycle. Projects that intentionally relied on persistent frame accumulation for effects such as manual trails, paint-style rendering and temporal accumulation must now implement custom persistence behavior explicitly.

Checklist

  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@waynemwashuma waynemwashuma self-assigned this May 27, 2026
@waynemwashuma waynemwashuma merged commit 4e5baa7 into wimaengine:dev May 27, 2026
5 checks passed
@waynemwashuma waynemwashuma deleted the add-canvas-clearing-system branch May 27, 2026 14:48
waynemwashuma added a commit that referenced this pull request May 27, 2026
## Objective

- Part of #324

## Solution

N/A

## Showcase

N/A

## Migration guide

No migration required.

## Checklist

* [ ] I have updated the documentation accordingly.
* [ ] I have added tests to cover my changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant