Skip to content

perf(camera): avoid resizing canvas and re-querying context on every video frame #6157

@Rohit-rk07

Description

@Rohit-rk07

Summary

doUseCamera redraws camera frames every 100ms and currently performs canvas resize + context lookup on each frame.

Code references

  • js/utils/utils.js in draw():
    • canvas.width = w;
    • canvas.height = h;
    • canvas.getContext(2d).drawImage(video, 0, 0, w, h);

Why this matters

  • Setting canvas.width/height each frame resets the drawing buffer/state and is expensive.
  • Re-fetching getContext(2d) every frame adds avoidable overhead.
  • Combined with periodic capture this increases CPU use, especially on low-power devices.

Expected behavior

  • Set canvas.width/height once during camera/session setup (or only when dimensions change).
  • Cache the 2D context once and reuse it in the frame loop.

Suggested fix

Move canvas sizing and context initialization outside draw() and keep draw() limited to drawImage + export logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions