1class DOMNode { 2 nodeName: string; 3 4 constructor(nodeName: string) { 5 this.nodeName = nodeName; 6 } 7 8 get ownerDocument() { 9 return window.document; 10 } 11 12 appendChild(element: any) { 13 // unimplemented 14 } 15} 16 17class DOMElement extends DOMNode { 18 style = {}; 19 // emitter = new EventEmitter(); 20 constructor(tagName: string) { 21 super(tagName.toUpperCase()); 22 } 23 24 get tagName() { 25 return this.nodeName; 26 } 27 28 insertBefore = () => {}; 29 getContext() { 30 // if (global.canvasContext) { 31 // return global.canvasContext; 32 // } 33 return { 34 fillRect: () => {}, 35 drawImage: () => {}, 36 getImageData: () => {}, 37 getContextAttributes: () => ({ 38 stencil: true, 39 }), 40 getExtension: () => ({ 41 loseContext: () => {}, 42 }), 43 }; 44 } 45 46 addEventListener(eventName: string, listener: () => void) { 47 // this.emitter.addListener(eventName, listener); 48 } 49 50 removeEventListener(eventName: string, listener: () => void) { 51 // this.emitter.removeListener(eventName, listener); 52 } 53 54 get clientWidth() { 55 return this.innerWidth; 56 } 57 get clientHeight() { 58 return this.innerHeight; 59 } 60 61 get innerWidth() { 62 return window.innerWidth; 63 } 64 get innerHeight() { 65 return window.innerHeight; 66 } 67} 68 69class DOMDocument extends DOMElement { 70 body = new DOMElement('BODY'); 71 72 constructor() { 73 super('#document'); 74 } 75 76 createElement(tagName: string) { 77 return new DOMElement(tagName); 78 } 79 80 createElementNS(tagName: string) { 81 const canvas = this.createElement(tagName); 82 canvas.getContext = () => ({ 83 fillRect: () => ({}), 84 drawImage: () => ({}), 85 getImageData: () => ({}), 86 getContextAttributes: () => ({ 87 stencil: true, 88 }), 89 getExtension: () => ({ 90 loseContext: () => ({}), 91 }), 92 }); 93 // @ts-ignore 94 canvas.toDataURL = () => ({}); 95 96 return canvas; 97 } 98 99 getElementById(id: string) { 100 return new DOMElement('div'); 101 } 102} 103 104// @ts-ignore 105process.browser = true; 106 107// @ts-ignore 108window.emitter = window.emitter || {}; // new EventEmitter(); 109window.addEventListener = 110 window.addEventListener || ((eventName: string, listener: () => void) => {}); 111 112window.removeEventListener = 113 window.removeEventListener || ((eventName: string, listener: () => void) => {}); 114 115// @ts-ignore 116window.document = new DOMDocument(); 117 118// @ts-ignore 119window.document.body = new DOMElement('body'); 120 121// @ts-ignore 122window.location = 'data:'; // <- Not sure about this... or anything for that matter ¯\_(ツ)_/¯ 123 124// This could be made better, but I'm not sure if it'll matter for PIXI 125// @ts-ignore 126global.navigator.userAgent = 'iPhone'; 127// @ts-ignore 128global.userAgent = global.navigator.userAgent; 129 130class HTMLImageElement2 { 131 align: string; 132 alt: null; 133 border: null; 134 complete: boolean; 135 crossOrigin: string; 136 localUri: string; 137 lowSrc: string; 138 currentSrc: string; 139 src: string; 140 width: number; 141 height: number; 142 isMap: boolean; 143 144 constructor(props: { localUri: string; width: number; height: number }) { 145 this.align = 'center'; 146 this.alt = null; 147 this.border = null; 148 this.complete = true; 149 this.crossOrigin = ''; 150 this.localUri = props.localUri; 151 this.lowSrc = props.localUri; 152 this.currentSrc = props.localUri; 153 this.src = props.localUri; 154 this.width = props.width; 155 this.height = props.height; 156 this.isMap = true; 157 } 158} 159 160// @ts-ignore 161global.Image = HTMLImageElement2; 162// @ts-ignore 163global.HTMLImageElement = HTMLImageElement2; 164