ice/packages/cache-canvas
luhc228 b8b1d5e41f
fix: source map url in prod files but the sourceMap files not published (#6511)
* fix: source map url in prod files

* chore: changeset
2023-09-06 10:09:59 +08:00
..
src fix: fix native back (#6457) 2023-08-14 00:13:18 -07:00
CHANGELOG.md chore: update versions (#6449) 2023-08-21 14:03:57 +08:00
README.md feat: support canvans 2d for cache (#6367) 2023-07-16 23:07:27 -07:00
package.json fix: source map url in prod files but the sourceMap files not published (#6511) 2023-09-06 10:09:59 +08:00
tsconfig.json feat: support canvans 2d for cache (#6367) 2023-07-16 23:07:27 -07:00

README.md

@ice/cache-canvas

React component for supporting canvas for cache.

Usage

npm i @ice/cache-canvas -S
import MainGame from './game'; // eva.js 的封装

const GAME_CANVAS = 'game-canvas';

export default (props) => {
  useEffect(() => {
    const gameEl = document.getElementById(GAME_CANVAS);
    new MainGame(gameEl, getGameHeight());
  }, []);

  const init = () => {
     return new Promise((resolve) => {
      const canvas: HTMLCanvasElement | null = document.getElementById(GAME_CANVAS_ID) as HTMLCanvasElement;
      if (canvas && typeof canvas.getContext === 'function') {
        let ctx: CanvasRenderingContext2D | null = canvas.getContext('2d');

        ctx?.fillRect(25, 25, 100, 100);
        ctx?.clearRect(45, 45, 60, 60);
        ctx?.strokeRect(50, 50, 50, 50);
      }

      setTimeout(() => {
        console.log('canvas paint ready!');
        resolve(true);
      }, 5000);
    });
  }

  return (
    <>
       <CanvasCache id={GAME_CANVAS} useCache={false} init={init} />
    </>
  );
};