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

phaser3-scrolling-camera

v2.1.1

Published

Class for Phaser 3 framework, adding the capacity of scrolling by dragging or using the mouse wheel

Downloads

131

Readme

GitHub tag (latest by date) GitHub license

PHASER3 - SCROLLING CAMERA CLASS

ScrollingCamera extends the class Phaser.Cameras.Scene2D.Camera of Phaser 3 framework, adding the capacity of vertical and horizontal scrolling by dragging or using the mouse wheel, with optional snap system.
The scroll is customizable.

Live demo: https://jjcapellan.github.io/Phaser3-ScrollingCamera/

Features

  • Vertical or horizontal scroll
  • Natural snap effect (customizable)
  • Snap event (sends the snap position)
  • Interactive, even behind other cameras.
  • Accepts dragging and mouse wheel as input (keyboard supported).

Table of contents


Installation

Browser

There are two alternatives:

  • Download the file scrollcam.umd.js to your proyect folder and add a reference in your html:
<script src = "scrollcam.umd.js"></script>
  • Point a script tag to the CDN link:
<script src = "https://cdn.jsdelivr.net/gh/jjcapellan/[email protected]/dist/scrollcam.umd.js"></script>

Important: the class is exposed as ScrollingCamera

From NPM

npm i phaser3-scrolling-camera

Then you can acces the class as:

  • CommonJS module:
const Phaser = require('phaser');
const ScrollingCamera = require('phaser3-scrolling-camera').default;

// In scene.create function
const config = { ... }
const myScrollCam = new ScrollingCamera(this, config);
  • ES6 module:
import Phaser from 'phaser';
import ScrollingCamera from 'phaser3-scrolling-camera';

// In scene.create function
const config = { ... }
const myScrollCam = new ScrollingCamera(this, config);

How to use

This is the simplest case (uses the default parameters):

// In create function ...
let myCamera = new ScrollingCamera(this);

The constructor of ScrollingCamera only needs as argument a scene object. All the other arguments are optionals.
And this is the opposite case with all possible customizations:

// In create function ...
let cameraOptions = {
      x: 50,             // x position of the camera (default = 0)
      y: 50,             // y position of the camera (default = 0)
      width: 300,        // Width of the camera (default = game.config.width)
      height: 500,       // Height of the camera (default = game.config.height)
      contentBounds: {   // Determines the limits of the area where the camera is looking. (optional)
        x: 400,          // x position of contents from top-left origin (default = cameraOptions.x)
        y: 10,           // y position of contents from top-left origin (default = cameraOptions.y)
        length: 1200     // Distance measured in pixels along the camera main axis
      },
      wheel: {           // Mouse wheel params (optional)
        enable: true,    // Does this camera use the mouse wheel? (default = false)
        delta: 60        // Variation of scroll in pixels with each wheel change (default = 55)
      },
      drag: 0.90,        // Reduces the scroll speed per game step in 10%. (default = 0.95)      
      snap: {
        enable: false,   // Does this camera use snap effect? (default = false)
        padding: 20,     // Gap in pixels between snaps (default = 20)
        bounces: 3       // Number of bounces on target before the snap (default = 3)
      },
      horizontal: true   // Does this camera use horizontal scroll (default = false)
    };
const myCamera = new ScrollingCamera(this, cameraOptions);

In this image the content for the ScrollingCamera camera is positioned out of screen, in this way we haven't to hide it to the main camera of the scene (camera.ignore(...)):


Snap event

In the code bellow, the event snap returns the last snap position (snapIndex) where the camera was stopped.
This position is not in pixels, is an index representing the first snap, the second snap, ... (0, 1, ...).
This is usefull to extract a numeric value like in the demo.

const myCamera = new ScrollingCamera(this, configObject)

myCamera.on('snap', (snapIndex) => {
  console.log(snapIndex);
});

Public methods

Besides the functions inherited from the parent class, ScrollingCamera have these public methods:

setSpeed(speed: number)

Params:

  • speed : scroll speed in pixels/second

This method can be usefull, for example, to implement keyboard control:

// In create function ...
let myCamera = new ScrollingCamera(this);
let cursors = this.input.keyboard.createCursorKeys();
cursors.down.on('down', () => myCamera.setSpeed(50));
cursors.up.on('down', () => myCamera.setSpeed(-50));

moveToSnap(snapIndex: number)

Params:

  • snapIndex {number}: Snap position in function of gap beteen snaps (0 ,1 , 2, ...). Sets the scroll position to snapIndex instantly.
// In create function ...
let myCamera = new ScrollingCamera(this);
let cursors = this.input.keyboard.createCursorKeys();
cursors.down.on('down', () => myCamera.moveToSnap(myCamera.snapIndex - 1));
cursors.up.on('down', () => myCamera.moveToSnap(myCamera.snapIndex + 1));

Public properties

Besides inherited properties and the properties declared in the constructor, there are only one public property:

  • snapIndex {number}: Stores the snap position in function of gap beteen snaps (0 ,1 , 2, ...). This property is send with the event snap as argument.

License

ScrollingCamera is licensed under the terms of the MIT open source license.