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

gamewriter

v1.0.2

Published

GameWriter uses the document to write over a canavas for increased performance when using text in 2d or 3d games.

Downloads

16

Readme

NPM version Known Vulnerabilities npm NPM downloads Gitter

Why

A lot of JavaScript animation and game libraries like to offer ways to write text to the canvas graphically which is convenient but not always performant. GameWriter handles the ugly parts of writing on top of the canvas using the document so that you can have crisp, clean, and performant text on your game.

Fonts

Before we start, a quick message about fonts. In order to keep the performance high and avoid unnecessary events and callbacks, all fonts that you want used in your game should be loaded before using GameWriter. This means that you should have the links to the fonts defined in the head of the document or in the CSS so that GameWriter can focus on writing and not loading.

Table of Contents

Installation

To install GameWriter, you can do so through npm like so:

$ npm install gamewriter

and then in your project, import it like so:

// Webpack
import GameWriter from 'gamewriter';

// Browser
import GameWriter from './path/to/gamewriter.js';

Initialization

To initialize GameWriter, you must at the very least pass a reference to the canvas element being used:

const canvas = document.getElementById('myCanvas');

const gamewriter = new GameWriter(canvas);

along with the canvas, the following options can be passed to GameWriter:

| param | type | description | default | |--------------------- |------------------- |--------------------------------------------------------------------------------------------------------------------------- |--------- | | canvas | HTMLCanvasElement | The canvas to draw the text onto | | | options | Object | | | | options.autoDisplay | boolean | Indicates whether text nodes are displayed on the canvas after they are created or if they have to be displayed manually. | true | | options.classes | Array | Class names to add to each text node created. | [] |

API

The following are the properties and methods available for use on an instance of gamewriter.

addText

Adds a new piece of text to the game.

| param | type | description | default | |--------------|--------|------------------------------------|---------| | text | string | The text to write onto the game | | | x | number | The x position of the text | | | y | number | The y position of the text | | | options | Object | | | | options.size | number | The size of the text (1-10) | 1 |

example:

const title = gamewriter.addText('My Game', 100, 150, { size: 5 });

removeText

Removes a piece of text from the game.

| param | type | description | default | |--------------|--------|--------------------------------------------|---------| | text | Text |A reference to the text object to remove | |

example:

const title = gamewriter.addText('My Game', 100, 150);

gamewriter.removeText(title);

clear

Removes all pieces of text from the game.

example:

const title = gamewriter.addText('My Game', 100, 150);
const subTitle = gamewriter.addText('Its the best', 100, 200);

gamewriter.clear();

Text API

The following are properties and methods available for every text object created through addText. Any of the properties that have a setter also have a getter.

x

Sets a new x position for the text.

| param | type | description | default | |--------------|--------|--------------------------------------|---------| | newX | number | The new x position of for the text | |

example:

const title = gamewriter.addText('My Game', 100, 150);

title.x = 350;

y

Sets a new y position for the text.

| param | type | description | default | |--------------|--------|--------------------------------------|---------| | newY | number | The new y position of for the text | |

example:

const title = gamewriter.addText('My Game', 100, 150);

title.y = 350;

move

Moves the text to a specified (x, y) position.

| param | type | description | default | |--------------|--------|--------------------------------------|---------| | x | number | The new x position of for the text | | | y | number | The new y position of for the text | |

example:

const title = gamewriter.addText('My Game', 100, 150);

title.y = 350;

size

Sets a new size for the text.

| param | type | description | default | |--------------|--------|--------------------------------------|---------| | newSize | number | The new size of the text | |

example:

const title = gamewriter.addText('My Game', 100, 150);

title.size = 3;

text

Change the text displayed.

| param | type | description | default | |--------------|--------|--------------------------------------|---------| | text | string | The new text to display | |

example:

const title = gamewriter.addText('My Game', 100, 150);

title.text = 'My New Game';

setDynamic

Sets a piece of the text to be dynamic. This dynamic part of the text can then be easily changed with changeDynamic.

| param | type | description | default | |--------------|--------|-----------------------------------------------------------|---------| | text | string | The part ofo the text that should be dynamic | |

example:

const score = gamewriter.addText('Score: 0', 400, 50);

score.setDynamic('0');

changeDynamic

Change the text content of the dynamic text portion of the text.

| param | type | description | default | |--------------|--------|----------------------------------------------------------------|---------| | text | string | The text to display in place of the dynamic text. | |

example:

const score = gamewriter.addText('Score: 0', 400, 50);

score.setDynamic('0');

score.changeDynamic('5');

hide

Hides the text.

example:

const title = gamewriter.addText('My Game', 100, 150);

title.hide();

show

Shows the text.

example:

const title = gamewriter.addText('My Game', 100, 150);

title.show();

Tests

To run the tests available for GameWriter, use:

$ npm run test

License

MIT