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

canvas-scrolling-background

v0.2.2

Published

Manages an infinite scrolling background for your game

Downloads

4

Readme

NPM version Known Vulnerabilities npm NPM downloads Gitter

Installation

To install this module through npm, simply use the following command:

$ npm install canvas-scrolling-background

and to use it, you can import it as an ES6 module:

// Webpack
import ScrollingBackground from 'canvas-scrolling-background';

// Browser
import ScrollingBackground from './node_modules/canvas-scrolling-background/canvas-scrolling-background.js';

Note: Since it is a default export you can name it whatever you like such as: import Pikachu from 'canvas-scrolling-background';

Usage

Since this module expects that you already have a basic framework for your game, unless your game is just a scrolling background, you have to provide the following on initialization:

| param | type | description | default | |------------------- |------------------- |----------------------------------------------------------- |--------- | | canvas | HTMLCanvasElement | The canvas to draw the scrolling background on | | | image | string | The path to image to use as the background | | | options | Object | | | | options.x | number | The initial x position of the background on the canvas | 0 | | options.y | number | The initial y position of the background on the canvas | 0 | | options.direction | string | The direction the background should scroll (left or right) | left |

The canvas should already exist and so csb takes a reference to that in order to draw onto the same canvas as the rest of your game.

So let's say that you're making a game similar to the chrome dino runner game that you can play when you don't have a great internet connection. The image for this background can be found at 'https://cdn.elg.im/t-rex/img/1x-horizon.png'

Note: Only left and right scrolling are supported at this time with support for up and down scrolling coming soon.

So we could initialize this as:

// Get the existing canvas.
const canvas = document.getElementById('my-canvas');

// The background should start at a x position of 0 which is default and it should scroll to the left which it does by
// default also. The only thing we want to do is show the background near the bottom of the canvas.
const options = {
  y: canvas.height - 10;
}

const sb = new ScrollingBackground(options);

// Since the image has to be loaded and that's an asynchronous operation, csb dispatches a signal when the image is
// loaded and the background is ready to be updated.
sb.loaded.add(start);

function start() {
  update();
}

function update() {
  // Your other canvas updates...

  // Runs the background update method to update the position of the background, the speed parameter determines how
  // fast the background scrolls.
  sb.update(3);

  // As any normal game loop, you have to call it on a requestAnimationFrame loop.
  requestAnimationFrame(update);
}

So while the example is pretty well commented the one part that might be confusing in the loaded signal. If you're not used to singals just know that they're like events but bound to a property so that they don't rely on loose typing. So for example the loaded signal as an event might look like:

sb.on('loadaed', => {});

So really the signal is like an event being emitted but more strict. Anyways, this signal lets you know that the background image has finished loading and is ready to be used. This is when you provide your update method to csb.

Acknowledgements

Thanks to @jeremyskelly for helping with the update method.

License

MIT