BasicCamera

BasicCamera

A basic camera that will either render multiple CanvasObjects into one renderLayer, and/or just move the html canvas elements of each layer added.


Constructor

new BasicCamera(renderLayer, blitLayer1, blitLayer2opt, blitLayer3opt, blitLayer4opt, blitLayer5opt, vWidth, vHeight, constant1opt, constant2opt, constant3opt, constant4opt, constant5opt)

Constructs a new BasicCamera, there should generally only be one camera, but it is not enforced.

Parameters:
Name Type Attributes Description
renderLayer CanvasObject

The CanvasObject to draw all layers into.

blitLayer1 CanvasObject

The first offscreen CanvasObject layer to draw from onto the renderLayer

blitLayer2 CanvasObject <optional>

The second CanvasObject layer

blitLayer3 CanvasObject <optional>

The third CanvasObject layer

blitLayer4 CanvasObject <optional>

The fourth CanvasObject layer

blitLayer5 CanvasObject <optional>

The fifth CanvasObject layer, would get drawn on top of all the other layers.

vWidth Number

The width of the cameras viewport

vHeight Number

The height of the camera viewport

constant1 Number <optional>

If not 0 or undefined, layer 1 will be moved instead of drawn into the render layer, for this you will need to be sure the html canvas of layer 1 is added as part of the html page.

constant2 Number <optional>

If not 0 or undefined, layer 2 will be moved instead of drawn into the render layer, for this you will need to be sure the html canvas of layer 2 is added as part of the html page.

constant3 Number <optional>

If not 0 or undefined, layer 3 will be moved instead of drawn into the render layer, for this you will need to be sure the html canvas of layer 3 is added as part of the html page.

constant4 Number <optional>

If not 0 or undefined, layer 4 will be moved instead of drawn into the render layer, for this you will need to be sure the html canvas of layer 4 is added as part of the html page.

constant5 Number <optional>

If not 0 or undefined, layer 5 will be moved instead of drawn into the render layer, for this you will need to be sure the html canvas of layer 5 is added as part of the html page.

Members

_map

the 2D Array to use during drawDirectlyFromMap

_th

Tile height of each tile used with drawDirectlyFromMap

_tileSheetImage

the tileSheet to use during drawDirectlyFromMap

_tw

Tile width of each tile used with drawDirectlyFromMap

b1

A reference to the CanvasObject that is layer 1.

b2

A reference to the CanvasObject that is layer 2.

b3

A reference to the CanvasObject that is layer 3.

b4

A reference to the CanvasObject that is layer 4.

b5

A reference to the CanvasObject that is layer 5.

c1

If 1, layer 1 will be moved instead of drawn.

c2

If 1, layer 2 will be moved instead of drawn.

c3

If 1, layer 3 will be moved instead of drawn.

c4

If 1, layer 4 will be moved instead of drawn.

c5

If 1, layer 5 will be moved instead of drawn.

cameraFollowOffsetY

The vertical camera follow offset, typically you would set this to about negative half the cameraHeight minus tile height.

colorValues

Used during drawDirectlyFromMap if no _tileSheetImage has been set.

constructor

layerToRender

The CanvasObject that will copy from each of the layers.

mapUpdateMethod

A method to call when the end of _map is reached that would update _map. Used alongside the setupForRenderFromMap method

pTween1XOffset

pTween1YOffset

pTween2XOffset

pTween2YOffset

pTweenTime1

The millisecond amount of time for parallax layer one movement

pTweenTime2

The millisecond amount of time for parallax layer two.

pTweenType1

The type of tween for parallax layer one movement, default is Linear

pTweenType2

The type of tween for parallax layer two movement.

shakeForce

Defines the amount of shaking that will happen, default is 4.

shakeTime

Defines the time of each shake, set during the shake method.

v :Rectangle

The view port Rectangle; the view port of the camera.

If a layer is being drawn, layerToRender is copying pixels from the layer using the view port x y width and height. The render methods, blitLayerRender, tweenedBlitLayerRender, and drag update v based on the parameters given to them, and then draw or move each layer.

Type:

viewPortHeight

The height of the cameras view port.

viewPortWidth

The width of the cameras view port.

Methods

_mapIndexXMethod(a) → {Number}

A function that determines the x index to draw from based on the value given. By default this will assume that the tile value is a [y,x] array, and will return a[1] by default for this method. To change what this method does override it by calling _mapIndexXMethod = function(a) { .. your details... returning index x }; Used by setupForRenderFromMap

Parameters:
Name Type Description
a Object

The tile value

Returns:

The x index denoted by the tile value

Type
Number

_mapIndexYMethod(a) → {Number}

A function that determines the y index to draw from based on the value given. By default this will assume that the tile value is a [y,x] array, and will return a[0] by default for this method. To change what this method does override it by calling _mapIndexXMethod = function(a) { .. your details... returning index y }; Used by setupForRenderFromMap

Parameters:
Name Type Description
a Object

The tile value

Returns:

The y index denoted by the tile value

Type
Number

addParallaxLayer(canvasObject)

Can be used with the .tweenedBlitLayerRender method to produce parallax effects.

Only two layers can be added. Layers will parallax based on .pTweenTime1 (or .pTweenTime2 for if you use 2 layers) or, a default of whatever you pass for tweentime in .tweenedBittLayerRender multiplied by 2.

By default you just add a layer and it will move slower than the layers on top of it If you want to do more complex stuff, you would alter the .pTweenType1 and .pTweenTime1

Parameters:
Name Type Description
canvasObject CanvasObject

The CanvasObject to add as a parallax layer

addParallaxLayerForMove(canvasObejct)

Same as addParallaxLayer except the layer added will be moved instead of drawn. When this method is used, any parallax layer already added will also just move instead of be drawn. And you need to be sure to have that layer as part of the things being displayed. Move just moves it, regular addParallaxLayer adds it to be moved and drawn.

Parameters:
Name Type Description
canvasObejct CanvasObject

blitLayerRender(cameraOffsetPosition, limitXopt, limitYopt)

Renders each layer into layerToRender (that is assigned during construction) based on the cameraOffsetPosition point given and limitX and limitY. This method does not render parallax layers.

Parameters:
Name Type Attributes Description
cameraOffsetPosition MoverPoint

A MoverPoint to offset the camera around.

limitX Number <optional>

The horizontal limit of camera movement.

limitY Number <optional>

The vertical limit of camera movement.

blitLayerRenderView() → {Rectangle}

Returns the view port Rectangle of the camera.

Returns:
Type
Rectangle

drag(dragPos, dragSpeedopt, limitXopt, limitYopt, tweenTime, easeTypeopt)

Drag the camera around.

Parallax layers are not dragged, or displayed during drag.  For drag, only the 5 inital layers are available.
Parameters:
Name Type Attributes Default Description
dragPos MoverPoint

The MoverPoint position the drag starts from, this must be defined, normally is just the mouse position.

dragSpeed Number <optional>
64

The speed of the drag default is 64

limitX Number <optional>

default is v.width

limitY Number <optional>

default is v.height

tweenTime Number

The amount of time for the tween

easeType String <optional>
'Linear'

Ease type

Example
document.onmousemove = function(e) { camera.drag(mouseMP,64,0,1,100); }
          document.onmouseup = function(e) { camera.releaseDrag(); }
          gameLoop = function(e) { camera.justRender(); }

drawDirectlyFromMap()

Draws from _map onto layerToRender. When the setupForRenderFromMap method is used, this method will be used internally to render the layer. If _tileSheetImage is defined then BlitMath.specificPatternAreaBlit or patternAreaBlit is used to render the map. If _tileSheetImage is not defined then BlitMath.drawSquresFromAreaOfPattern is used, using the colorValues property of this class. If _map is not a map of [y,x] values, ._mapIndexXMethod and ._mapIndexYMethod will be used on each value in _map to determine the location of each tile to draw.

executeShake()

Executes the shake, called by tweenedBlitLayerRender. set up the shake using the shake method.

focus(mpToFocusOn, offsetXopt, offsetYopt)

Resets tweens, and adjusts the view port position based on the MoverPoint given and the offsets, and then calls justRender and then blitLayerRender. offsets should be the negative of what you would use as cameraOffsetPosition during blitLayerRender.

Parameters:
Name Type Attributes Description
mpToFocusOn MoverPoint

The MoverPoint to focus on

offsetX Number <optional>

Optional horizontal offset subtracted from mpToFocusOn

offsetY Number <optional>

Optional vertical offset subtracted from mpToFocusOn

isoTweenedBlitLayerRender(tileWidth, tileHeight, scene, pointToFollow, cFollowOffsetXopt, cFollowOffsetYopt, limitXopt, limitYopt, tweenTime, easeTypeopt, startingXopt, startingYopt)

follows pointToFollow in a SimpleIsoScene. for use with the SimpleIsoScene Class.

Parameters:
Name Type Attributes Default Description
tileWidth Number
tileHeight Number
scene SimpleIsoScene
pointToFollow MoverPoint
cFollowOffsetX Number <optional>
cFollowOffsetY Number <optional>
limitX Number <optional>
limitY Number <optional>
tweenTime Number
easeType String <optional>
'Linear'
startingX Number <optional>
0
startingY Number <optional>
0

justRender()

Renders each layer starting with layer 1 on the bottom, primarely for use with the drag method.

releaseDrag()

Releases the camera from being dragged, use after a drag operation has been made.

reset(vx, vy)

Resets the view port position and all tweens.

Parameters:
Name Type Description
vx Number

Reset to this x position instead of 0.

vy Number

Reset to this y position instead of 0.

resetTween()

Resets all tweens.

setIsoFollowOffsets(gameWidth, gameHeight, tileWidth, tileHeight)

Sets the camera follow offsets for use with isoTweenedBlitLayerRender calls.

Parameters:
Name Type Description
gameWidth Number

The total width of the game

gameHeight Number

The total height of the game

tileWidth Number

The tile width

tileHeight Number

tile height

setMap(toThis)

Sets the 2D Array to use during drawDirectlyFromMap

Parameters:
Name Type Description
toThis Array

A 2D Array of tile values denoting where in a tileset to draw from.

setupForRenderFromMap(map, tileSheetImg, tw, th, layer, vWopt, vHopt, indexXMethodopt, indexYMethodopt)

setup a layer to be renedered directly from a 2D map array.

Parameters:
Name Type Attributes Description
map Array

2D map Array to use

tileSheetImg Img

HTML img element or canvas element to draw from, sets the _tileSheetImage property of this class.

tw Number

tile width

th Number

tile height

layer Number

The layer that will be rendered directly from the map. 1 2 3 4 or 5.

vW Number <optional>

The view width for drawDirectlyFromMap calls, by default v.width will be used.

vH Number <optional>

The view height for drawDirectlyFromMap calls, by default v.height will be used.

indexXMethod function <optional>

A method that will take a tile value and return its x index. by default it is assumed each tile value is a [y,x] index array.

indexYMethod function <optional>

A method that will take a tile value and return its y index. by default it is assumed that each tile value is a [y,x] index array.

shake(time, container, exopt)

Setup to shake the given container, automatic execution of the shake happens within the tweenedBlitLayerRender method, or use executeShake to execute the shake manually after calling this method to set up the shake. If your using a GameSkeleton, by default all you would have to do is call this method of the camera in your loop to shake the given container. GameSkeleton also has a shake method.

Parameters:
Name Type Attributes Description
time Number
container HTMLElement

The HTML element to shake.

ex Boolean <optional>

Execute the shake once, causes executeShake to be called once, executeShake is also called by tweenedBlitLayerRender.

tweenedBlitLayerRender(cameraOffsetPosition, limitXopt, limitYopt, tweenTime, easeTypeopt, startingXopt, startingYopt)

Call during a loop to move the camera around cameraOffsetPosition with easing. this method renders each individual layer onto layerToRender based on the params given. if parallax layers have been defined they will be rendered first, underneath all other layers.

Parameters:
Name Type Attributes Default Description
cameraOffsetPosition MoverPoint

the point the camera should follow. The follow area is defined by v.width and v.height.

limitX Number <optional>

The number that v.x should not be greater than. The default is v.width

limitY Number <optional>

The number that v.y should not be greater than. The default is v.height

tweenTime Number

The millisecond amount of time each tween should take

easeType String <optional>
'Linear'

The easing function to use, default is 'Linear'.

startingX Number <optional>

denoting the number v.x should not be less than. default is 0.

startingY Number <optional>

denoting the number v.y should not be less than. default is 0.