@flyskywhy/react-native-browser-polyfill

中文文档
2.0.2发布于 2024/11/27MITCommonJS近 30 天下载 4377

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)

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Example
  4. zdog
  5. Implements
  6. DOM
  7. Image, HTMLImageElement, ImageBitmap
  8. Canvas, HTMLCanvasElement
  9. window as global
  10. Document
  11. Element
  12. Node
  13. text-encoding
  14. xmldom-qsa

@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.

@flyskywhy/react-native-browser-polyfill is forked from @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

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

And follow the steps to install @flyskywhy/react-native-gcanvas.

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.

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

If canvas 2d or 3d needed, ref to README.md of @flyskywhy/react-native-gcanvas, or just ref to ZdogAndTests.js.

Example

zdog

No need modify any code of framework zdog itself.

Only modify one line code of app demo Made with Zdog CodePen Collection, e.g. just modify .zdog-canvas in JS of 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.

Here is the result of 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.

Implements

DOM

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

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 or nonDeclarative.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

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

window as global

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

document.createElement;
document.createElementNS;

Element

All sizes return the window size:

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

element.tagName;
element.setAttributeNS;
element.focus;

Node

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

global.TextEncoder
global.TextDecoder

xmldom-qsa

window.DOMParser

版本与发布记录

@flyskywhy/react-native-browser-polyfill 共发布 14 个版本,首次发布于 2021/06/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2024/11/272 年前
2.0.12023/02/033 年前
2.0.02022/12/243 年前
1.0.102022/12/163 年前
1.0.92022/12/033 年前
1.0.82022/11/134 年前

关于 @flyskywhy/react-native-browser-polyfill 的常见问题

@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)

如何安装 @flyskywhy/react-native-browser-polyfill?

在项目目录运行 npm install @flyskywhy/react-native-browser-polyfill,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@flyskywhy/react-native-gcanvas(*)、react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flyskywhy/react-native-browser-polyfill 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flyskywhy__react-native-browser-polyfill 是否存在。

@flyskywhy/react-native-browser-polyfill 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@flyskywhy/react-native-browser-polyfill 可以商用吗?

@flyskywhy/react-native-browser-polyfill 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flyskywhy/react-native-browser-polyfill 的下载量有多少?

根据 npm 官方下载统计,@flyskywhy/react-native-browser-polyfill 在 2026/10/15 – 2026/11/13 这 30 天内被下载 4,377 次,日均约 146 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flyskywhy/react-native-browser-polyfill 还在维护吗?

最新版本 2.0.2 发布于 2024/11/27(2 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。