@platzh1rsch/pacman-canvas
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

Pacman Canvas

npm version

Basically https://pacman.platzh1rsch.ch/ as a npm package for simple integration into your website.

Get started

simple setup

React Example

import type { Game } from "@platzh1rsch/pacman-canvas";
import { getGameInstance } from "@platzh1rsch/pacman-canvas";
import { useEffect, useRef, useState } from "react";
import styles from "./gameCanvas.module.css";


export default function GameCanvasSimple() {
  
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const [canvasContext, setCanvasContext] =
    useState<CanvasRenderingContext2D | null>(null);


  const game: Game = getGameInstance();
  
  useEffect(() => {
    if (canvasRef.current) {
      setCanvasContext(canvasRef.current.getContext("2d"));
    }
  }, []);

  useEffect(() => {
    if (game && canvasContext) {
      game.setCanvasContext2d(canvasContext);
    }
  }, [canvasContext]);

  return (
    <>
      <div>
        <section>
          {/* Game controls */}
          <button onClick={() => game.pauseResume()}>Pause / Resume</button>
          <button
            onClick={() => game.newGame()}
          >
            Restart Game
          </button>
          <button onClick={() => game.endGame()}>End Game</button>
        </section>
        
        <section>
          <div
            id={styles["canvas-container"]}
            onClick={() => {
              game.pauseResume();
            }}
          >
            <canvas
              ref={canvasRef}
              style={{ background: "black" }}
              id="myCanvas"
              width="540"
              height="390"
            >
              <p>Canvas not supported</p>
            </canvas>
          </div>
        </section>
      </div>
    </>
  );
}

Package Sidebar

Install

npm i @platzh1rsch/pacman-canvas

Weekly Downloads

15

Version

0.0.9

License

MIT

Unpacked Size

709 kB

Total Files

124

Last publish

Collaborators

  • platzh1rsch