fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 2.29 kB
Source Map (JSON)
{"version":3,"file":"index.min.mjs","names":["getBrowserEnv"],"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"],"mappings":"sFAaA,IAAI,EAeJ,MAAa,EAAU,GAAA,CACrB,EAAM,GAMK,MAAe,IAAQ,EAAMA,GAAAA,EAE7B,MAAoC,GAAA,CAAS,SAE7C,MACX,GAAA,CAAS,OAKE,MAAA,CAAA,IAAA,EAAA,OAAA,KACN,KAAA,EAAI,EAAO,mBAAA,KAAoB,GAAA,CAAkB,iBAAtC,EAAwD,EAAA,EAAA,OAAA,KAAA,oBAAA,KAAA,OAAA,KAAA,kBAAA,KAAA,gBAAA,KAAA"}