|
| 1 | +import { GlobalRegistrator } from '@happy-dom/global-registrator' |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {LitecanvasInstance} instance |
| 5 | + * @param {string} event |
| 6 | + * @param {Function} callback |
| 7 | + * @returns {Promise<Function>} |
| 8 | + */ |
| 9 | +global.onLitecanvas = function (instance, event, callback) { |
| 10 | + return new Promise((resolve) => { |
| 11 | + const removeListener = instance.listen(event, (...args) => { |
| 12 | + const res = callback(...args) |
| 13 | + if (false !== res) { |
| 14 | + removeListener() |
| 15 | + resolve() |
| 16 | + } |
| 17 | + }) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +GlobalRegistrator.register() |
| 22 | + |
| 23 | +// maybe fix node navigator |
| 24 | +if (!global.navigator) { |
| 25 | + global.navigator = {} |
| 26 | +} |
| 27 | + |
| 28 | +// fake userActivation |
| 29 | +global.navigator.userActivation = global.navigator.userActivation || {} |
| 30 | +global.navigator.userActivation.hasBeenActive = true |
| 31 | + |
| 32 | +// fake AudioContext |
| 33 | +global.AudioContext = class { |
| 34 | + createBuffer() { |
| 35 | + return { |
| 36 | + getChannelData() { |
| 37 | + return new Map() |
| 38 | + }, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + createBufferSource() { |
| 43 | + return { |
| 44 | + connect() {}, |
| 45 | + start() {}, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// fake AudioBuffer |
| 51 | +global.AudioBuffer = class {} |
| 52 | + |
| 53 | +// fake requestAnimationFrame |
| 54 | +global.requestAnimationFrame = (callback) => { |
| 55 | + return setTimeout(() => { |
| 56 | + callback(performance.now()) |
| 57 | + }, 1000 / 60) |
| 58 | +} |
| 59 | + |
| 60 | +// fake cancelAnimationFrame |
| 61 | +global.cancelAnimationFrame = (rafId) => { |
| 62 | + clearTimeout(rafId) |
| 63 | +} |
| 64 | + |
| 65 | +// fake canvas tag |
| 66 | +createCanvasTag(window) |
| 67 | + |
| 68 | +/** |
| 69 | + * @param {Window & globalThis} window |
| 70 | + * @returns {HTMLElement} |
| 71 | + */ |
| 72 | +function createCanvasTag(window) { |
| 73 | + const _createElement = global.window.document.createElement |
| 74 | + |
| 75 | + /** |
| 76 | + * @param {string} tagName |
| 77 | + * @param {*} options |
| 78 | + */ |
| 79 | + window.document.createElement = function (tagName, options) { |
| 80 | + const el = _createElement.apply(window.document, arguments) |
| 81 | + if ('canvas' === tagName.toLowerCase()) { |
| 82 | + el.getContext = function (type) { |
| 83 | + if (!this.context) { |
| 84 | + this.context = createContext2d(this) |
| 85 | + } |
| 86 | + return this.context |
| 87 | + } |
| 88 | + el.width = 300 |
| 89 | + el.height = 150 |
| 90 | + } |
| 91 | + return el |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function createContext2d(canvas) { |
| 96 | + const ctx = { |
| 97 | + canvas, |
| 98 | + |
| 99 | + fillStyle: '#000000', |
| 100 | + font: '10px sans-serif', |
| 101 | + globalAlpha: 1, |
| 102 | + globalCompositeOperation: 'source-over', |
| 103 | + imageSmoothingEnabled: true, |
| 104 | + lineDashOffset: 0, |
| 105 | + lineWidth: 1, |
| 106 | + strokeStyle: '#000000', |
| 107 | + textAlign: 'start', |
| 108 | + textBaseline: 'alphabetic', |
| 109 | + textRendering: 'auto', |
| 110 | + } |
| 111 | + |
| 112 | + const fn = () => {} |
| 113 | + const methods = |
| 114 | + 'getContextAttributes,drawImage,beginPath,fill,stroke,clip,isPointInPath,isPointInStroke,createLinearGradient,createRadialGradient,createConicGradient,createPattern,createImageData,getImageData,putImageData,setLineDash,getLineDash,closePath,moveTo,lineTo,quadraticCurveTo,bezierCurveTo,arcTo,rect,roundRect,arc,ellipse,clearRect,fillRect,strokeRect,save,restore,reset,isContextLost,fillText,strokeText,measureText,scale,rotate,translate,transform,getTransform,setTransform,resetTransform,drawFocusIfNeeded' |
| 115 | + |
| 116 | + for (const method of methods.split(',')) { |
| 117 | + ctx[method] = fn |
| 118 | + } |
| 119 | + |
| 120 | + return ctx |
| 121 | +} |
0 commit comments