import { RefObject } from "react" ; export function isCanvasRendered ( containerRef ) { return containerRef . current ?. firstChild !== null ; } export function renderCanvas ( containerRef , canvas ) { if ( containerRef . current !== null ) { const clone = cloneCanvas ( canvas ) ; if ( containerRef . current . firstChild ) { containerRef . current . firstChild . replaceWith ( clone ) ; } else { containerRef . current . appendChild ( clone ) ; } } } function cloneCanvas ( canvas ) { const clone = document . createElement ( "canvas" ) ; clone . width = canvas . width ; clone . height = canvas . height ; const dataTestid = canvas . getAttribute ( "data-testid" ) ; if ( dataTestid ) { clone . setAttribute ( "data-testid" , dataTestid ) ; } clone . getContext ( "2d" ) ! . drawImage ( canvas , 0 , 0 ) ; return clone ; }