# @flyskywhy/react-native-browser-polyfill

> Browser polyfill with native canvas 2d 3d for making React Native compatible with web libs like zdog (and pixi.js, three.js, phaser.js to be validated)

Latest version **2.0.2** (published 2024-11-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @flyskywhy/react-native-browser-polyfill
pnpm add @flyskywhy/react-native-browser-polyfill
yarn add @flyskywhy/react-native-browser-polyfill
bun add @flyskywhy/react-native-browser-polyfill
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2024-11-27 |
| First published | 2021-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 2.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Evan Bacon |
| Maintainers | flyskywhy |
| Keywords | 2d, 3d, browser, document, dogz, dom, offscreen-canvas, polyfill, react, react-native, shim, web, window, zdog |

## Links

- npm: https://www.npmjs.com/package/@flyskywhy/react-native-browser-polyfill
- Repository: https://github.com/flyskywhy/react-native-browser-polyfill
- Homepage: https://github.com/flyskywhy/react-native-browser-polyfill#readme
- Issues: https://github.com/flyskywhy/react-native-browser-polyfill/issues
- npm.io page: https://npm.io/package/@flyskywhy/react-native-browser-polyfill

## Dependencies (7)

- [atob](https://npm.io/package/atob.md) 2.1.2
- [btoa](https://npm.io/package/btoa.md) 1.2.1
- [fbjs](https://npm.io/package/fbjs.md) ^1.0.0
- [buffer](https://npm.io/package/buffer.md) ^5.6.0
- [xmldom-qsa](https://npm.io/package/xmldom-qsa.md) ^1.0.3
- [text-encoding](https://npm.io/package/text-encoding.md) ^0.7.0
- [@canvas/image-data](https://npm.io/package/@canvas/image-data.md) ^1.0.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2024-11-27
- 2.0.1 — 2023-02-03
- 2.0.0 — 2022-12-24
- 1.0.10 — 2022-12-16
- 1.0.9 — 2022-12-03
- 1.0.8 — 2022-11-13
- 1.0.7 — 2022-10-15
- 1.0.6 — 2022-10-13
- 1.0.5 — 2022-09-17
- 1.0.4 — 2022-03-01
- 1.0.3 — 2021-12-31
- 1.0.2 — 2021-06-25
- 1.0.1 — 2021-06-20
- 1.0.0 — 2021-06-17

## README

# @flyskywhy/react-native-browser-polyfill

Browser polyfill with native canvas 2d 3d for React Native, can directly use canvas based html frameworks without code modified, e.g. [zdog](https://github.com/metafizzy/zdog).

`@flyskywhy/react-native-browser-polyfill` is forked from [@expo/browser-polyfill](https://github.com/expo/browser-polyfill), then use `@flyskywhy/react-native-gcanvas` instead of `expo-2d-context`, use `event-target-shim` instead of `fbemitter`, and fix some bugs.

### Installation

```bash
npm install @flyskywhy/react-native-browser-polyfill @flyskywhy/react-native-gcanvas
```

And follow [the steps to install @flyskywhy/react-native-gcanvas](https://github.com/flyskywhy/react-native-gcanvas#react-native).

### Usage

Import the library just into your project root `/index.js` , `/index.android.js` , `/index.ios.js` or `/index.native.js`.

If you don't want import it in your project root, you can also import the library into any JavaScript file where you want use it. But if `inlineRequires` is true in your `metro.config.js`, you will get `ERROR  ReferenceError: Can't find variable: document` or `ERROR  ReferenceError: Property 'document' doesn't exist, js engine: hermes`, then you should change `inlineRequires` to false or import the library into your project root e.g. `/index.android.js`.

```js
import '@flyskywhy/react-native-browser-polyfill';
```

If canvas 2d or 3d needed, ref to README.md of [@flyskywhy/react-native-gcanvas](https://github.com/flyskywhy/react-native-gcanvas), or just ref to [ZdogAndTests.js](https://github.com/flyskywhy/GCanvasRNExamples/blob/master/app/components/ZdogAndTests.js).

## Example
### zdog
No need modify any code of framework [zdog](https://github.com/metafizzy/zdog) itself.

Only modify one line code of app demo [Made with Zdog CodePen Collection](https://codepen.io/collection/DzdGMe/), e.g. just modify `.zdog-canvas` in `JS` of [https://codepen.io/clarke-nicol/pen/OezRdM](https://codepen.io/clarke-nicol/pen/OezRdM) into `this.canvas` in this GCanvasRNExamples APP commit [react -> react-native: `Zdog and Tests` step3 Zdog works well](https://github.com/flyskywhy/GCanvasRNExamples/commit/7855e91).

Here is the result of [ZdogAndTests.js](https://github.com/flyskywhy/GCanvasRNExamples/blob/master/app/components/ZdogAndTests.js), you can directly discover that the `render` and `mousemove` is same with the original html version [https://codepen.io/clarke-nicol/pen/OezRdM](https://codepen.io/clarke-nicol/pen/OezRdM).

<img src="https://raw.githubusercontent.com/flyskywhy/react-native-browser-polyfill/master/assets/zdog-and-tests.gif" width="480">

## Implements

## DOM

DOM is provided with very low support, these are used for libs like pixi.js that validate type.

```js
class Node
class Element
class Document
class HTMLImageElement
class Image
class ImageBitmap
class HTMLVideoElement
class Video
class HTMLCanvasElement
class Canvas
```

### Image, HTMLImageElement, ImageBitmap

Image can load from `https://somewhere.com/some.png` or from require('some.png') on Android, iOS and Web, ref to the [ZdogAndTests.js](https://github.com/flyskywhy/GCanvasRNExamples/blob/master/app/components/ZdogAndTests.js) or [nonDeclarative.js](https://github.com/flyskywhy/GCanvasRNExamples/blob/master/src/nonDeclarative.js).

```js
const image = document.createElement('img');
const image = new global.HTMLImageElement();
const image = new Image();

image.onload = () => console.log('Can recieve event load by onload');
image.addEventListener('load', () => console.log('Also can recieve event load by addEventListener');
```

### Canvas, HTMLCanvasElement
[Example As Usage of @flyskywhy/react-native-gcanvas](https://github.com/flyskywhy/react-native-gcanvas#example-as-usage)

`document.createElement('canvas')` (as offscreen canvas) usage also is described in `src/window.js`.

### window as global

```js
addEventListener;
removeEventListener;
dispatchEvent;
Buffer;
TextDecoder;
TextEncoder;
document;
Document;
Element;
Image;
HTMLImageElement;
ImageBitmap;
CanvasRenderingContext2D;
WebGLRenderingContext;
// window.devicePixelRatio; // undefined as described in `src\resize.js`
window.screen.orientation;
userAgent;
location;
```

### Document

```js
document.createElement;
document.createElementNS;
```

### Element

#### All sizes return the window size:

```js
element.clientWidth;
element.clientHeight;
element.innerWidth;
element.innerHeight;
element.offsetWidth;
element.offsetHeight;
element.getBoundingClientRect;
element.getAttribute;
element.setAttribute;
```

#### Empty attributes that prevent libraries from crashing

```js
element.tagName;
element.setAttributeNS;
element.focus;
```

### Node

```js
node.ownerDocument;
node.className;
node.addEventListener;
node.removeEventListener;
node.dispatchEvent;
node.appendChild;
node.insertBefore;
node.removeChild;
```

# External Libraries

Some external node.js polyfills are added as well.

## [text-encoding](https://github.com/inexorabletash/text-encoding)

```
global.TextEncoder
global.TextDecoder
```

## [xmldom-qsa](https://github.com/zeligzhou/xmldom-qsa)

```
window.DOMParser
```

---
_Source: https://npm.io/package/@flyskywhy/react-native-browser-polyfill · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
