Skip to content

Latest commit

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Camera

This is a very small library for doing viewport/camera management in a HTML5 Canvas based game in a pixel and aspect-ratio independent manner.

That is, you can use the canvas context however you like, with any sized canvas and the output will be the same (consider the case of stretching the canvas to fit various screen resolutions and devices).

Table of contents

Usage

constcanvas=document.createElement("canvas")constcontext=this.canvas.getContext("2d")constcamera=newCamera(context)

Very exciting, there are a few methods to manipulate the camera with once it is created


Moving the camera

Move the centre of the viewport to the location specified by x and y

camera.moveTo(x,y)

Controlling camera zoom

Zoom out/in to the specified distance from the 2D plane

camera.zoomTo(z)

Interacting with the scene

Transform a coordinate pair from screen coordinates (relative to the canvas) into world coordinates (useful for intersection between mouse and entities)

Optional: obj can supply an object to be populated with the x/y (for object-reuse in garbage collection efficient code)

constcoords=camera.screenToWorld(x,y,[obj])console.log(coords.x,coords.y)

Overlaying the scene

Transform a coordinate pair from world coordinates into screen coordinates (relative to the canvas) - useful for placing DOM elements over the scene.

Optional: obj can supply an object to be populated with the x/y (for object-reuse in garbage collection efficient code).

constcoords=camera.worldToScreen(x,y,[obj])console.log(coords.x,coords.y)

Rendering

Each render pass, rendering to the context should be applied with.

camera.begin()// Draw stuffcamera.end()

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


Properties

PropertyDescriptionDefault Value
aspectRatioCanvas Aspect Ratiocontext.canvas.width / context.canvas.height
contextdrawing context on the canvascanvas.getContext('2d')
distanceUsed for the zoom1000
fieldOfViewThe extent of the observable canvas0.7853981633974483
lookAtRefers to the camera position in the x and y axis[0, 0]
viewportHolds: camera's - width, height, left, top, right, bottom, scalespecified in the next table

viewport

PropertyDefinition
widthdistance * Math.tan(fieldOfView)
heightwidth / aspectRatio
leftlookAtX - (width / 2.0)
toplookAtY - (height / 2.0)
rightleft + width
bottomtop + height
scale[canvas.width / width, canvas.height / height]

Methods

MethodDescription
begin()Applies Scale and Translation Rendering
end()2d Context restore() method Rendering
applyScale()2d Context scale(Camera.viewport.scale[0], Camera.viewport.scale[0]) method
applyTranslation()2d Context translate(-Camera.viewport.left, -Camera.viewport.top) method
updateViewport()Camera.viewport data update
zoomTo(z)Moving the camera
moveTo(x, y)Controlling camera zoom
screenToWorld(x, y, obj)Interacting with the scene
worldToScreen(x, y, obj)Overlaying the scene
addListeners()Event Listeners for Zoom and scroll around world and Center camera on "R" key

Settings

Example of default settings:

settings={distange: 1000,initialPosition: [0,0],fieldOfView: Math.PI/4.0,scaleX: 1.0,scaleY: 1.0}

About

Just a camera implementation over the top of HTML5 canvas

Resources

Stars

101 stars

Watchers

7 watching

Forks

Releases

Packages

Contributors

Languages