Before submitting
Version
7.2.0
Environment
Chrome, Safari, Firefox
Link To Reproduction
No response
Steps To Reproduce
- Create an iframe-based preview/editor.
- Point Fabric to the iframe environment with
fabric.setEnv({ ...fabric.getEnv(), window, document }).
- Create/render a
fabric.Textbox and trigger text measurement.
- Destroy that iframe and create a new one.
- Call
fabric.setEnv() again with the new iframe window/document.
- Render or measure the same text again.
In our app this reproduces consistently after:
- opening the editor,
- viewing banner text,
- leaving the editor,
- opening the editor again.
Expected Behavior
After fabric.setEnv() switches to a new document, Fabric should recreate the internal text measuring canvas/context for the current document.
Text metrics should stay correct after iframe recreation or editor reopen.
Actual Behavior
Fabric keeps using the original cached measuring context created for the first document/window.
After the iframe is destroyed and recreated, text measurement still uses the stale context from the old document. This causes incorrect text metrics, and text can visually collapse / "stick together" after reopening the editor.
Additional Context
This looks related to the module-level measuringContext cache used by text measurement.
The context is created once and then reused forever, even after fabric.setEnv() changes the active document. In iframe-based apps this means the measuring context may belong to a destroyed iframe window/document.
A fix like this resolves the issue on our side:
let measuringContext: CanvasRenderingContext2D | null;
let measuringContextDocument: Document | null;
function getMeasuringContext() {
const fabricDocument = getFabricDocument();
if (!measuringContext || measuringContextDocument !== fabricDocument) {
const canvas = createCanvasElementFor({ width: 0, height: 0 });
measuringContext = canvas.getContext('2d');
measuringContextDocument = fabricDocument;
}
return measuringContext;
}
Before submitting
Version
7.2.0
Environment
Chrome, Safari, Firefox
Link To Reproduction
No response
Steps To Reproduce
fabric.setEnv({ ...fabric.getEnv(), window, document }).fabric.Textboxand trigger text measurement.fabric.setEnv()again with the new iframewindow/document.In our app this reproduces consistently after:
Expected Behavior
After
fabric.setEnv()switches to a newdocument, Fabric should recreate the internal text measuring canvas/context for the current document.Text metrics should stay correct after iframe recreation or editor reopen.
Actual Behavior
Fabric keeps using the original cached measuring context created for the first document/window.
After the iframe is destroyed and recreated, text measurement still uses the stale context from the old document. This causes incorrect text metrics, and text can visually collapse / "stick together" after reopening the editor.
Additional Context
This looks related to the module-level
measuringContextcache used by text measurement.The context is created once and then reused forever, even after
fabric.setEnv()changes the active document. In iframe-based apps this means the measuring context may belong to a destroyed iframe window/document.A fix like this resolves the issue on our side: