Documentation generator: JsDoc Toolkit 2.4.0
Template: Codeview 1.2
Generated on: 2014-7-16 16:20

Class jaws.Viewport

A window (Rect) into a bigger canvas/image. Viewport is always contained within that given image (called the game world). "Field Summary" contains options for the Viewport()-constructor.

Class Summary
Constructor Attributes Constructor Name and Description
 
jaws.Viewport(options)
Field Summary
Field Attributes Field Name and Description
 
Height of viewport, defaults to canvas height
 
Maximum x-position for viewport, defaults to canvas width
 
Maximum y-position for viewport, defaults to canvas height
 
Width of viewport, defaults to canvas width
 
x
X-position for the upper left corner of the viewport
 
y
Y-position for the upper left corner of the viewport

Method Summary

Method Attributes Method Name and Description
 
apply(func)
executes given draw-callback with a translated canvas which will draw items relative to the viewport
 
center the viewport around item.
 
draw(obj)
if obj is an array-like object, iterate through it and call draw() on each item if it's partly inside the viewport
 
draws 'item' if it's partly inside the viewport
 
drawTileMap(tile_map)
draws all items of 'tile_map' that's lies inside the viewport this is simular to viewport.draw( tile_map.all() ) but optmized for Huge game worlds (tile maps)
 
forceInside(item, buffer)
force 'item' inside the limits of the viewport using 'buffer' as indicator how close to the 'item' is allowed to go
 
forceInsideVisibleArea(item, buffer)
force 'item' inside current viewports visible area using 'buffer' as indicator how close to the 'item' is allowed to go
 
isAbove(item)
Returns true of item is above viewport
 
isBelow(item)
Returns true of item is above viewport
 
isInside(item)
Returns true if item is inside viewport
 
isLeftOf(item)
Returns true of item is left of viewport
 
isOutside(item)
Returns true if item is outside viewport
 
Returns true if item is partly (down to 1 pixel) inside viewport
 
isRightOf(item)
Returns true of item is right of viewport
 
move(x, y)
Move viewport x pixels horizontally and y pixels vertically
 
moveTo(x, y)
Move viewport to given x/y

Class Detail

jaws.Viewport(options)
// Center viewport around players position (player needs to have x/y attributes)
// Usefull for sidescrollers
viewport.centerAround(player)

// Common viewport usage. max_x/max_y could be said to set the "game world size"
viewport = viewport = new jaws.Viewport({max_x: 400, max_y: 3000})
player = new jaws.Sprite({x:100, y:400})
viewport.centerAround(player)

// Draw player relative to the viewport. If viewport is way off, player won't even show up.
viewport.apply( function() {
 player.draw()
});
Parameters:
options

Field Detail

{int} height
Height of viewport, defaults to canvas height
{int} max_x
Maximum x-position for viewport, defaults to canvas width
{int} max_y
Maximum y-position for viewport, defaults to canvas height
{int} width
Width of viewport, defaults to canvas width
{int} x
X-position for the upper left corner of the viewport
{int} y
Y-position for the upper left corner of the viewport

Method Detail

  • apply(func)
    executes given draw-callback with a translated canvas which will draw items relative to the viewport
    viewport.apply( function() {
      player.draw();
      foo.draw();
    });
    Parameters:
    func
  • centerAround(item)
    center the viewport around item. item must respond to x and y for this to work. Usefull for sidescrollers when you wan't to keep the player in the center of the screen no matter how he moves.
    Parameters:
    item
  • draw(obj)
    if obj is an array-like object, iterate through it and call draw() on each item if it's partly inside the viewport
    Parameters:
    obj
  • drawIfPartlyInside(item)
    draws 'item' if it's partly inside the viewport
    Parameters:
    item
  • drawTileMap(tile_map)
    draws all items of 'tile_map' that's lies inside the viewport this is simular to viewport.draw( tile_map.all() ) but optmized for Huge game worlds (tile maps)
    Parameters:
    tile_map
  • forceInside(item, buffer)
    force 'item' inside the limits of the viewport using 'buffer' as indicator how close to the 'item' is allowed to go
    viewport.forceInside(player, 10)
    Parameters:
    item
    buffer
  • forceInsideVisibleArea(item, buffer)
    force 'item' inside current viewports visible area using 'buffer' as indicator how close to the 'item' is allowed to go
    viewport.move(10,0)                          // scroll forward
    viewport.forceInsideVisibleArea(player, 20)  // make sure player doesn't get left behind
    Parameters:
    item
    buffer
  • isAbove(item)
    Returns true of item is above viewport
    Parameters:
    item
  • isBelow(item)
    Returns true of item is above viewport
    Parameters:
    item
  • isInside(item)
    Returns true if item is inside viewport
    Parameters:
    item
  • isLeftOf(item)
    Returns true of item is left of viewport
    Parameters:
    item
  • isOutside(item)
    Returns true if item is outside viewport
    if( viewport.isOutside(player)) player.die();
    
    // ... or the more advanced:
    bullets = new SpriteList()
    bullets.push( bullet )
    bullets.removeIf( viewport.isOutside )
    Parameters:
    item
  • isPartlyInside(item)
    Returns true if item is partly (down to 1 pixel) inside viewport
    Parameters:
    item
  • isRightOf(item)
    Returns true of item is right of viewport
    Parameters:
    item
  • move(x, y)
    Move viewport x pixels horizontally and y pixels vertically
    Parameters:
    x
    y
  • moveTo(x, y)
    Move viewport to given x/y
    Parameters:
    x
    y