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

camera-2d-simple

v3.0.0

Published

2D camera for WebGL

Downloads

3,653

Readme

2D Camera

npm version build status file size code style prettier demo

Simple camera built on top of gl-matrix for 2D scenes. Heavily inspired by Mikola's Orbit Camera.

Also see:

Install

npm install camera-2d-simple gl-matrix

Note, gl-matrix is a required dependency but it's not prebundled!

API

import createCamera from 'camera-2d-simple';

Constructor

# createCamera(target = [0,0], distance = 1, rotation = 0, viewCenter = [0,0], scaleBounds = [[0, Infinity], [0, Infinity]], translationBounds = [[-Infinity, Infinity], [-Infinity, Infinity]])

Creates a 2d camera looking at target from a certain distance.

  • target is the 2d vector the camera is looking at.
  • distance is the distance between the target and the camera.
  • rotation is angle in radiance around the z axis with respect to the viewport center.
  • viewCenter is the center point of the canvas w.r.t the view coordinates. When operating in normalized-device coordinates this must be [0,0] but the center can differ when operating in pixel coordinates.
  • scaleBounds are the min and max allowed scalings in the x and y direction.
  • translationBounds are the min and max allowed translation in the x and y direction.

Returns A new 2d camera object

Properties

# camera.view

The current view matrix (mat4) of the camera.

# camera.viewCenter

The current view center.

# camera.translation

The camera translation needed to look at the target.

# camera.target

The camera center in normalized device coordinates. This is a shorthand for inverseOf(camera.view) * [0,0,0,1].

# camera.scaling

The camera scaling. Larger scaling means the camera is closer to the target. This is the inverse of distance, i.e., 1 / distance.

# camera.scaleBounds

The scale limits.

# camera.distance

Distance of the camera to the target. This is the inverse of scaling, i.e., 1 / scaling.

# camera.rotation

Rotation in radians around the z axis.

Methods

# camera.lookAt(target = [0,0], distance = 1, rotation = 0)

Move the camera center to target given the distance and rotation.

# camera.translate([x,y])

Moves the center of the camera by x and y pixel.

# camera.pan([x,y])

Same as camera.translate()

# camera.scale(delta, scaleCenter)

Zooms in or out by delta with respect to scaleCenter in [x,y]. The new distance will be distance * delta.

For x and y specific scaling, you can specify a tuple as detla ([xDelta, yDelta]).

# camera.zoom(delta, scaleCenter)

Same as camera.scale()

# camera.rotate(angle)

Rotate the camera by angle (in radians) around the z axis with respect to the viewport center.

# camera.setScaleBounds(bounds)

Set the scaling limits. Expects a tuple of the min and max allowed scale factors.

For x and y specific scale bound, you can specify a tuple of tuple ([[xScaleMin, xScaleMax], [yScaleMin, yScaleMax]]). Make sure to take special care when using scale() in this scenario as the scale can get out of sync.

# camera.setView(view)

Set the camera to the view matrix (mat4).

# camera.setViewCenter(viewCenter)

Set viewCenter w.r.t. the canvas.

# camera.reset()

Reset the camera to the initial target, distance, and rotation.