Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
deakins
Advanced tools
Readme
Small Canvas 2D viewport management.
Small and simple 2D viewport/camera management for Canvas named after the legendary English cinematographer Roger Deakins.
$ npm install deakins
This module is delivered as:
dist/index.mjs
dist/deakins.umd.js
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);
Initializes a new Deakins
camera instance.
Type: CanvasRenderingContext2D
type: number
default: 1000
This value is used to immitate a FOV camera effect as you zoom.
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.
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
.
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]);
type: [number, number]
default: [0, 0]
type: boolean
default: false
Zoom to the specified distance from the 2D plane.
camera.zoomTo(500);
type: number
default: 1000
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();
Call .end()
when you're done drawing to reset the context.
type: [number, number]
Transform a point
from world coordinates into screen coordinates - useful for placing DOM elements over the scene.
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.
Call this in your resize handler to make sure the viewport is updated.
TypeScript conversion based on camera by Rob Ashton with minor tweaks. Added support for more options, lazy camera movements and slightly modified API.
MIT © Terkel Gjervig
FAQs
Small Canvas 2D viewport management
The npm package deakins receives a total of 4 weekly downloads. As such, deakins popularity was classified as not popular.
We found that deakins demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.