Ted Hopp
Ted Hopp

Reputation: 234795

Is it okay to hold a reference to a canvas 2d context?

Is it okay to hold a long-lasting reference to a canvas context (one obtained by getContext('2d'))? In particular, might there be issues if the canvas element is resized? If there are issues, would it improve matters to use a context obtained from a separate canvas node (as described in this thread) instead of one derived from the DOM?

The reason for the question is, I need to do a lot of text metric calculations behind the scenes and I thought that maintaining a reference to a context would be a good way to do this.

Upvotes: 2

Views: 337

Answers (1)

kangax
kangax

Reputation: 39168

I've never had any problems with storing a reference to a context. Resizing canvas doesn't affect it.

Spec doesn't seem to mention anything about new context in resizing section.

However, I would take a closer look at setContext section, just in case.

Upvotes: 2

Related Questions