• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŽฅ Small Canvas 2D Camera


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

More Repositories

1

awesome-creative-coding

Creative Coding: Generative Art, Data visualization, Interaction Design, Resources.
HTML
12,228
star
2

prompts

โฏ Lightweight, beautiful and user-friendly interactive prompts
JavaScript
8,805
star
3

ramme

Unofficial Instagram Desktop App.
JavaScript
3,319
star
4

tiny-glob

Super tiny and ~350% faster alternative to node-glob
JavaScript
856
star
5

zet

Set() as it should be.
JavaScript
521
star
6

sqliterally

Lightweight SQL query builder
JavaScript
263
star
7

terkelg

A website inside an SVG, inside an image, inside HTML, inside markdown, inside a GitHub readme.md.
TypeScript
231
star
8

facon

Tiny utility (365B) to create DOM elements with manner.
JavaScript
222
star
9

math

Math snippets with graphic programming in mind.
113
star
10

zuckerberg.smile

๐Ÿค– Control Mark Zuckerbergs smile property
JavaScript
89
star
11

skaler

A (329B) client-side image resizer.
JavaScript
88
star
12

workshy

A small (376B) lazy function scheduler for a butter smooth main thread.
JavaScript
80
star
13

math-toolbox

Lightweight and modular math toolbox
JavaScript
76
star
14

globrex

Glob to regular expression with support for extended globs.
JavaScript
70
star
15

cantinflas

Tiny mustache-like template engine in ~50 LOC.
JavaScript
65
star
16

sisteransi

ANSI escape codes for some terminal swag.
JavaScript
58
star
17

eliminate

Delete files and directories without all the bullshit.
JavaScript
52
star
18

terkel.com-2016

My Personal website. Build with Vue and ThreeJS.
JavaScript
40
star
19

powerwalker

๐ŸƒWalk directories recursively.
JavaScript
28
star
20

hent

A small utility to fetch remote files into buffers
JavaScript
23
star
21

npm-scripts-as-build-tool

Nuggets on how to use NPM Scripts as your build tool. You don't need Grunt or Gulp
21
star
22

cursor-travel

๐Ÿ“ Measure how far your cursor travels.
Swift
19
star
23

favorite-awesomeness

A tiny collection of my favorite awesomelists
18
star
24

shrinktome

๐Ÿ“˜Shrink facebook by 5% every 10th second. You're welcome!
JavaScript
16
star
25

stopgap

Easily create/remove temporary directories.
JavaScript
16
star
26

simultan

Simultaneously run an async function on any iterable with limited concurrency
JavaScript
15
star
27

cursormuseum

A very brief history of pointing devices
CSS
14
star
28

mkdirplz

Make directories recursively -plz ๐Ÿ™
JavaScript
13
star
29

antedate

A tiny pre-renderer for client side applications.
JavaScript
13
star
30

webpack-starter

'Just Add Water' Webpack 2, babel and glslify gourmet mix.
JavaScript
13
star
31

starter

No tooling starter because simplicity is the ultimate sophistication.
CSS
13
star
32

1d

๐ŸฅžMake multi-dimensional arrays as flat as a pancake.
JavaScript
11
star
33

utters

Small (257B) promise wrapper for SpeechSynthesisUtterance
JavaScript
11
star
34

globalyzer

Detect and extract the static part of a glob string.
JavaScript
10
star
35

vimrc

I heard you like escape rooms?
Vim Script
10
star
36

foldersstructure

macOS Automator workflow to scaffold my project directory structure.
10
star
37

threejs-create

Create a generic three.js application for quick prototyping
JavaScript
9
star
38

exclamation

Holy Stratosphere, Robin exclamationsโ—
JavaScript
7
star
39

invisible

The Invisible Game: Huckle buckle beanstalk
JavaScript
6
star
40

brolly

Internet of Things Umbrella Game.
JavaScript
6
star
41

yeezify

Instantly change all images on any given webpage to Kanye West.
JavaScript
6
star
42

IMDb-Runtimes

A simple Google Chrome extension to convert runtimes on IMDb to hours and minutes.
JavaScript
4
star
43

judgedbycover

A Twitter bot that judge books based on their cover.
JavaScript
3
star
44

dmjx-intro

Website for DMJX freshers' weekend
HTML
1
star
45

terkeliknibe

I created this website after nearly four months of apartment searching
SCSS
1
star