okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c4b3d743
main
1/** Capture a complete view, including external SVG sprite symbols. */ 2export async function monitorPng ( element :HTMLElement ) { 3await document . fonts . ready ; 4const [{ toPng}, spriteResponse ] = await Promise . all ([ import ( 'html-to-image' ), fetch ( '/icons/sprite.svg' )]); 5if ( ! spriteResponse . ok ) throw new Error ( 'Screenshot icons could not be loaded. Try again.' ); 6const sprite = new DOMParser (). parseFromString ( await spriteResponse . text (), 'image/svg+xml' ); 7const clone = element . cloneNode ( true ) as HTMLElement ; 8clone . style . width = ` ${ element . getBoundingClientRect (). width } px` ; 9clone . style . position = 'fixed' ; clone . style . left = '-100000px' ; clone . style . top = '0' ; 10clone . setAttribute ( 'aria-hidden' , 'true' ); 11// What is only live state (the model state and the conversation cache) is not part of the picture. 12clone . querySelectorAll ( '[data-snapshot-skip]' ). forEach ( node => node . remove ()); 13for ( const use of clone . querySelectorAll ( 'svg use' )) { 14const id = use . getAttribute ( 'href' )?. split ( '#' )[ 1 ]; 15const symbol = id && sprite . getElementById ( id ), svg = use . parentElement ; 16if ( ! symbol || ! svg ) continue ; 17const group = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'g' ); 18for ( const attr of symbol . attributes ) { 19if ( attr . name === 'viewBox' ) svg . setAttribute ( attr . name , attr . value ); 20else if ( attr . name !== 'id' ) group . setAttribute ( attr . name , attr . value ); 21} 22for ( const child of symbol . childNodes ) group . appendChild ( child . cloneNode ( true )); 23use . replaceWith ( group ); 24} 25document . body . appendChild ( clone ); 26try { 27return await toPng ( clone , { pixelRatio :2 , backgroundColor :getComputedStyle ( document . documentElement ). getPropertyValue ( '--st-surface' ). trim (), style :{ position :'static' , left :'auto' , top :'auto' , margin :'0' } }); 28} finally { clone . remove (); } 29}