npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magnit-game-frame

v0.0.11

Published

A single-file custom-element that exposes an Update and Render loop, manages players along with their inputs, and will adjust a child `canvas` element to fit the element's width and height.

Downloads

9

Readme

Magnit Game Frame Custom Element

A single-file custom-element that exposes an Update and Render loop, manages players along with their inputs, and will adjust a child canvas element to fit the element's width and height.

Quick Start

HTML
<game-frame></game-frame>
TS
// js
import 'path/to/game-frame.component.js';
// npm: 
// import 'magnit-game-frame/index';
// import { GameFrameComponent[, etc...] } from 'magnit-game-frame';

/* [...] */

const gameFrame = document.querySelector('game-frame');
gameFrame.onUpdate((deltaTime: number) =>
{
    // as fast as possible
    // warning: console.log, in this function, will slow down or crash your page.
});
gameFrame.onRender((deltaTime: number) =>
{
    // at the designated fps; defaults to 60 times each second
});

Features

  • onUpdate
  • onRender
  • onFixedUpdate
  • fps attribute

Simple Interactive Demo

HTML
<game-frame fps="30">
    <canvas></canvas>
</game-frame>
TS
let upCommandIsActive: boolean = false;
let downCommandIsActive: boolean = false;
let leftCommandIsActive: boolean = false;
let rightCommandIsActive: boolean = false;

const position: { x: number, y: number } = { x: 100, y: 100 };
const size: { width: number, height: number } = { width: 50, height: 50 };

let currentDeltaTime = 0;

const gameFrame = this.shadowRoot!.querySelector('game-frame') as GameFrameComponent;
gameFrame.onUpdate = (deltaTimeMs: number) =>
{
    currentDeltaTime = deltaTimeMs;
    upCommandIsActive    = gameFrame.defaultPlayerKeys.has('ArrowUp')    || gameFrame.defaultPlayerKeys.has('KeyW');
    downCommandIsActive  = gameFrame.defaultPlayerKeys.has('ArrowDown')  || gameFrame.defaultPlayerKeys.has('KeyS');
    leftCommandIsActive  = gameFrame.defaultPlayerKeys.has('ArrowLeft')  || gameFrame.defaultPlayerKeys.has('KeyA');
    rightCommandIsActive = gameFrame.defaultPlayerKeys.has('ArrowRight') || gameFrame.defaultPlayerKeys.has('KeyD');

    const inputSpeed = (gameFrame.defaultPlayerKeys.has('ShiftLeft')) ? .5 : .25;
    if(upCommandIsActive)    { position.y -= inputSpeed * deltaTimeMs; }            
    if(downCommandIsActive)  { position.y += inputSpeed * deltaTimeMs; }
    if(leftCommandIsActive)  { position.x -= inputSpeed * deltaTimeMs; }
    if(rightCommandIsActive) { position.x += inputSpeed * deltaTimeMs; }
}
gameFrame.onRender = () =>
{
    if(gameFrame.canvasContext == null || gameFrame.canvas == null)
    {
        return;
    }

    gameFrame.canvasContext.clearRect(0, 0, gameFrame.canvas.width, gameFrame.canvas.height);

    // draw rectangle
    gameFrame.canvasContext.fillStyle = '#ff303f';
    gameFrame.canvasContext.fillRect(position.x - size.width/2, position.y - size.height/2, size.width, size.height);

    // draw circle
    gameFrame.canvasContext.fillStyle = '#30ff3f';
    gameFrame.canvasContext.beginPath();
    gameFrame.canvasContext.arc(position.x, position.y, size.width/2, 0, Math.PI * 2);
    gameFrame.canvasContext.fill();

    // draw triangle
    gameFrame.canvasContext.fillStyle = '#3f30ff';
    gameFrame.canvasContext.beginPath();
    gameFrame.canvasContext.moveTo(position.x, position.y - size.height/2);                // top point
    gameFrame.canvasContext.lineTo(position.x - size.width/2, position.y + size.height/2); // left point
    gameFrame.canvasContext.lineTo(position.x + size.width/2, position.y + size.height/2); // right point
    gameFrame.canvasContext.fill();
}
gameFrame.beginGame();