ice/packages/cache-canvas
jimizai 511bfbc1b1
fix: bug fixed (#6803)
2024-02-22 11:02:11 +08:00
..
src fix: bug fixed (#6803) 2024-02-22 11:02:11 +08:00
CHANGELOG.md chore: update versions (#6509) 2023-09-14 10:58:37 +08:00
README.md feat: support canvans 2d for cache (#6367) 2023-07-16 23:07:27 -07:00
package.json chore: add self-closing function (#6734) 2024-01-25 01:36:54 -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} />
    </>
  );
};