All Projects → terkelg → deakins

terkelg / deakins

Licence: MIT license
🎥 Small Canvas 2D Camera

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to deakins

Kissui.scrollanim
CSS3 scroll animation library
Stars: ✭ 1,442 (+968.15%)
Mutual labels:  viewport
React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+1891.85%)
Mutual labels:  viewport
Within Viewport
JavaScript utility that determines whether an element is completely within the browser viewport
Stars: ✭ 244 (+80.74%)
Mutual labels:  viewport
Preact Scroll Viewport
Preact Component that renders homogeneous children only when visible
Stars: ✭ 118 (-12.59%)
Mutual labels:  viewport
React Viewport Slider
react-viewport-slider is a react component (obviously) that displays a sequence of contents using 100% of the viewport with and height.
Stars: ✭ 144 (+6.67%)
Mutual labels:  viewport
Emergence.js
Detect element visibility in the browser
Stars: ✭ 1,891 (+1300.74%)
Mutual labels:  viewport
Element In View
Check if an element is in viewport. (580 bytes and IE9+)
Stars: ✭ 54 (-60%)
Mutual labels:  viewport
guepard
flash to html5 converter, as3 to javascript translator
Stars: ✭ 58 (-57.04%)
Mutual labels:  canvas2d
React Virtual List
Virtual list in React
Stars: ✭ 147 (+8.89%)
Mutual labels:  viewport
React In Viewport
Detect if React component is in viewport
Stars: ✭ 183 (+35.56%)
Mutual labels:  viewport
Flowless
Efficient VirtualFlow for JavaFX
Stars: ✭ 120 (-11.11%)
Mutual labels:  viewport
Mobile Web Favorites
This is a favorites, with a mobile web tips.
Stars: ✭ 1,724 (+1177.04%)
Mutual labels:  viewport
Rem Vw Layout
移动端 REM布局 与 Viewport (VW) 布局的实例运用
Stars: ✭ 176 (+30.37%)
Mutual labels:  viewport
Isonscreen
Simple jQuery plugin to determine if an element is within the viewport
Stars: ✭ 115 (-14.81%)
Mutual labels:  viewport
Ember In Viewport
Detect if an Ember View or Component is in the viewport @ 60FPS
Stars: ✭ 247 (+82.96%)
Mutual labels:  viewport
React Fit To Viewport
Stars: ✭ 92 (-31.85%)
Mutual labels:  viewport
Viewport.jquery
viewport.jquery - simple but handy jQuery plugin adding methods and CSS selectors to check if element is in certain viewport
Stars: ✭ 156 (+15.56%)
Mutual labels:  viewport
render-props
㸚 Easy-to-use React state containers which utilize the render props (function as child) pattern
Stars: ✭ 33 (-75.56%)
Mutual labels:  viewport
angular-inviewport
A simple lightweight library for Angular with no other dependencies that detects when an element is within the browser viewport and adds a "sn-viewport-in" or "sn-viewport-out" class to the element
Stars: ✭ 72 (-46.67%)
Mutual labels:  viewport
Ng In Viewport
Allows us to check if an element is within the browsers visual viewport
Stars: ✭ 178 (+31.85%)
Mutual labels:  viewport


Deakins

Small Canvas 2D Camera

Small and simple 2D viewport/camera management for Canvas. Named after the legendary English cinematographer Roger Deakins known for Blade Runner, Fargo, No Country for Old Men and The Shawshank Redemption.

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 Deakins(context);
const player = new Player();

function loop() {
  camera.begin();

  // Look at point in space,
  camera.lookAt(player.position);

  // Zoom
  camera.zoomTo(500);

  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 imitate a FOV camera effect as you zoom.

options.flipAspectRatio

type: boolean
default: false

By default, 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 inside the screen space margin.

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

Enable lazy look-at. If true, the camera won't move before the look at point is inside the margin defined in options.margin. If false the look-at point stays centered. Below is an illustration of a lazy camera.

camera.zoomTo(zoom)

Zoom to the specified distance from the 2D plane.

camera.zoomTo(500);

zoom

type: number
default: 1000

camera.begin()

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].