Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Canvas 2D · 20 Static Layers and Caching

Series contents · Technology map

Stage: Scene Editing and Compositing · Editable example · Storage ID: canvas-2d-step-20

Goals and learning outcomes

Prerequisites

Prerequisite concepts: Pixel Arrays, Direct Writes. Prepare the result files and observation records from Unit 19.

Key concepts and explanations

Prerendering

You can draw an unchanging complex image once on a separate canvas. Copy it with drawImage instead of constructing paths on every frame.

Invalidation

A cache becomes stale when dimensions or colors change. Specify when it must be rebuilt to preserve sharpness and content.

Practice setup

First check the execution scope guidance on the example screen. The following describes preparation for extending the assignment in a separate project. Implement this yourself in an HTML and ES module project on a separate local HTTP development server. Record the browser version, viewport, and DPR.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. Prepare day-20.js and index.html in the external practice project. Create rebuildBackground to draw the grid background on an auxiliary canvas.
  2. Use the cache on the moving shapes and rebuild the background only when resizing.
  3. Save the output with the input condition every frame, then run again with only on resize. Expected: The number of static path constructions decreases. Compare this with the actual screen and logs, and record the actual results and reasons for differences in day-20-notes.md.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Cache Updatesevery frameonly on resizeThe number of static path constructions decreases

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Which drawings can become more expensive with caching?
  2. What do you invalidate when theme colors change?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

This is an editable browser experiment. Measurements vary with the current device and browser.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Reading Pixels and Applying Filters · Next: High Resolution and Responsive Layout

Open code, practice, and notes →