UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

1 lines 2.45 kB
{"version":3,"file":"index.min.mjs","sources":["../../../src/env/index.ts"],"sourcesContent":["/**\n * This file is consumed by fabric.\n * The `./node` and `./browser` files define the env variable that is used by this module.\n * The `./browser` module is defined to be the default env and doesn't set the env at all.\n * This is done in order to support isomorphic usage for browser and node applications\n * since window and document aren't defined at time of import in SSR, we can't set env so we avoid it by deferring to the default env.\n */\n\nimport { config } from '../config';\nimport { getEnv as getBrowserEnv } from './browser';\nimport type { TFabricEnv } from './types';\nimport type { DOMWindow } from 'jsdom';\n\nlet env: TFabricEnv;\n\n/**\n * Sets the environment variables used by fabric.\\\n * This is exposed for special cases, such as configuring a test environment, and should be used with care.\n *\n * **CAUTION**: Must be called before using the package.\n *\n * @example\n * <caption>Passing `window` and `document` objects to fabric (in case they are mocked or something)</caption>\n * import { getEnv, setEnv } from 'fabric';\n * // we want fabric to use the `window` and `document` objects exposed by the environment we are running in.\n * setEnv({ ...getEnv(), window, document });\n * // done with setup, using fabric is now safe\n */\nexport const setEnv = (value: TFabricEnv) => {\n env = value;\n};\n\n/**\n * In order to support SSR we **MUST** access the browser env only after the window has loaded\n */\nexport const getEnv = () => env || (env = getBrowserEnv());\n\nexport const getFabricDocument = (): Document => getEnv().document;\n\nexport const getFabricWindow = (): (Window & typeof globalThis) | DOMWindow =>\n getEnv().window;\n\n/**\n * @returns the config value if defined, fallbacks to the environment value\n */\nexport const getDevicePixelRatio = () =>\n Math.max(config.devicePixelRatio ?? getFabricWindow().devicePixelRatio, 1);\n"],"names":["env","setEnv","value","getEnv","getBrowserEnv","getFabricDocument","document","getFabricWindow","window","getDevicePixelRatio","_config$devicePixelRa","Math","max","config","devicePixelRatio"],"mappings":"sFAaA,IAAIA,EAeSC,MAAAA,EAAUC,IACrBF,EAAME,CAAK,EAMAC,EAASA,IAAMH,IAAQA,EAAMI,KAE7BC,EAAoBA,IAAgBF,IAASG,SAE7CC,EAAkBA,IAC7BJ,IAASK,OAKEC,EAAsBA,KAAA,IAAAC,EAAA,OACjCC,KAAKC,IAA2B,QAAxBF,EAACG,EAAOC,wBAAgBJ,IAAAA,EAAAA,EAAIH,IAAkBO,iBAAkB,EAAE"}