0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Clearing a canvas does not start a new path

0
Posted at

A canvas animation can redraw an old outline after the pixels have been cleared. The clear operation succeeded, but the drawing code later strokes a path that still contains earlier segments. The visible symptom can look like a failed clear when the problem is the path being reused.

Treat bitmap clearing and path construction as separate steps. Clear the affected pixels when required, and begin a new path before constructing independent geometry.

Give each shape a path boundary

Call beginPath before constructing a shape that should be independent of earlier geometry. Then add its segments and fill or stroke it as appropriate. The next independent shape can start another path rather than inheriting the previous shape's lines and curves.

For a hypothetical crop overlay, each pointer movement produces a new rectangle. Clear or redraw the overlay surface, begin a new path, and construct the rectangle for the latest coordinates. If the old rectangles remain in the current path, a later stroke can draw several outlines even though the surface was cleared between movements.

Do not replace beginPath with closePath. Closing a path adds a closing segment where needed; it does not discard every subpath that has already been constructed. A closed rectangle can still remain part of the path used by a later stroke.

Check the coordinate system used by the clear

A transform can also affect where clearRect operates. If the drawing code has translated or scaled the context, a rectangle expressed in canvas pixel coordinates may not clear the region you intended. Check the active transform when only part of the old image disappears.

For a full-surface clear, save the state, use an identity transform, clear with the buffer dimensions, and restore the state. This addresses the transform only. An active clipping region still restricts the operation, so clear outside that clipping scope.

Avoid using the CSS display width as an automatic substitute for canvas.width. The canvas drawing buffer and the element's displayed size can differ. A clear operation needs dimensions expressed in the coordinate system active for that operation.

Resetting dimensions has a broader effect

Changing width or height resets context state as well as the bitmap. Use it when the output dimensions change, then reapply the settings required for the render. It can otherwise hide a path problem while creating a state problem.

A resize can still be the correct action when the output dimensions really changed. In that case, reapply the drawing state the next render requires. Do not rely on a previous color, scale, or clipping setting surviving the resize. Keep the resize in the setup step and the ordinary redraw in its own step. That separation makes a later change in output dimensions easier to distinguish from a path construction error.

Test with two rectangles in clearly different positions. Draw the first, clear, then construct and stroke the second. Only the second should appear. Repeat the test with a transform and with a clipping region to see which operation controls each result.

When the output contains an unexpected line, inspect the sequence of path construction before adjusting the export format. Encoding a PNG or JPEG preserves the canvas result presented to the encoder. It will not remove a stale path that was already drawn into that result.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?