( svg: SVGSVGElement, settings: FrameSettings<Contexts> )
| 6 | import { transitionFrameElements } from '../internal/transitionFrameElements.js' |
| 7 | |
| 8 | const createFrame = <Contexts extends Record<string, string> = Record<string, string>>( |
| 9 | svg: SVGSVGElement, |
| 10 | settings: FrameSettings<Contexts> |
| 11 | ): Frame<Contexts> => { |
| 12 | if (!(svg instanceof SVGSVGElement)) { |
| 13 | throw new Error('ARWES createFrame requires a <svg> element.') |
| 14 | } |
| 15 | |
| 16 | const { animator } = settings |
| 17 | |
| 18 | let width = 0 |
| 19 | let height = 0 |
| 20 | // eslint-disable-next-line prefer-const |
| 21 | let observer: ResizeObserver |
| 22 | |
| 23 | const container = |
| 24 | settings.container ?? document.createElementNS('http://www.w3.org/2000/svg', 'g') |
| 25 | const contexts = { ...settings.contexts } |
| 26 | const animations = new Map<SVGElement, Map<keyof Contexts, AnimatedXAnimationFunctionReturn>>() |
| 27 | |
| 28 | const resize = (): void => { |
| 29 | // In certain browsers, when the SVG viewBox has values with decimals above the 0.5, |
| 30 | // the browser clips the values to the edge. Round down the dimensions so it doesn't happen. |
| 31 | width = Math.floor(svg.clientWidth) |
| 32 | height = Math.floor(svg.clientHeight) |
| 33 | |
| 34 | if (svg.getAttribute('viewBox') !== `0 0 ${width} ${height}`) { |
| 35 | svg.setAttribute('viewBox', `0 0 ${width} ${height}`) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const render = (): void => { |
| 40 | renderFrameElements( |
| 41 | container, |
| 42 | settings.elements, |
| 43 | animator, |
| 44 | animations as Map<SVGElement, Map<string, AnimatedXAnimationFunctionReturn>> |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | const draw = (): void => { |
| 49 | if (width <= 0 || height <= 0) { |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | drawFrameElements(container, width, height, settings.elements, contexts) |
| 54 | } |
| 55 | |
| 56 | const transition = (context: string, state: string): void => { |
| 57 | contexts[context] = state |
| 58 | |
| 59 | draw() |
| 60 | |
| 61 | transitionFrameElements( |
| 62 | container, |
| 63 | contexts, |
| 64 | animations as Map<SVGElement, Map<string, AnimatedXAnimationFunctionReturn>>, |
| 65 | settings.elements |
no test coverage detected