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

deakins

v1.0.0

Published

Small Canvas 2D viewport management

Downloads

9

Readme

Deakins

Small Canvas 2D viewport management.

Small and simple 2D viewport/camera management for Canvas named after the legendary English cinematographer Roger Deakins.

Install

$ npm install deakins

This module is delivered as:

Usage

import { deakins } from 'deakins';

const canvas = document.createElement(`canvas`);
const context = this.canvas.getContext(`2d`);
const camera = new Camera(context);

function loop() {
  camera.begin();

  // Look at point in space,
  // or follow a character ...
  camera.lookAt(10, 20);

  // Zoom
  camera.zoomTo(500);

  // Etc ...

  camera.end();
  requestAnimationFrame(loop);
}

loop();

Moving the camera

camera.lookAt([x, y]);

camera.zoomTo(z);

API

Deakins(context, [options])

Initializes a new Deakins camera instance.

context

Type: CanvasRenderingContext2D

options.fieldOfView

type: number default: 1000

This value is used to immitate a FOV camera effect as you zoom.

options.flipAspectRatio

type: boolean default: false

By defualt, everything scales based on the width of the canvas. When true, the aspect ratio is defined by the height instead.

options.margin

type: LookAtMargins default: {top: 0, right: 0, bottom: 0, left: 0}

Margins for all sides defined in screen space. This is used if the lazy option in lookAt is true. If true, the camera only follows the look-at point when the point is within the margins defined in lookAtMargins.

camera.lookAt(point, [lazy])

Move the centre of the viewport to the location specified by the point.

Call this in the RAF loop to follow a player character for example.

camera.moveTo([11, 8]); 

point

type: [number, number] default: [0, 0]

lazy

type: boolean default: false

camera.zoomTo(zoom)

Zoom to the specified distance from the 2D plane.

camera.zoomTo(500);

zoom

type: number default: 1000

camera.being()

On each render pass call .begin() before drawing to set the right transforms.

Appropriate transformations will be applied to the context, and world coordinates can be used directly with all canvas context calls.

camera.begin();

// Draw stuff here

camera.end();

camera.end()

Call .end() when you're done drawing to reset the context.

camera.worldToScreen(point)

point

type: [number, number]

Transform a point from world coordinates into screen coordinates - useful for placing DOM elements over the scene.

camera.screenToWorld()

returns: [number, number]

Transform and return the world space coordinate of the current look at point.

Useful if you want to project clicks and other screen space coordinates into 2D world coordinates.

camera.resize()

Call this in your resize handler to make sure the viewport is updated.

Credit

TypeScript conversion based on camera by Rob Ashton with minor tweaks. Added support for more options, lazy camera movements and slightly modified API.

License

MIT © Terkel Gjervig