Move Along the Z Axis

Download the code for Movement

Click to start the sprites moving along the z axis; click and drag right or left to alter the sprites' horizontal position.

You'll need a contemporary browser with javascript turned on to be able to see this page

MoveZAxis and MoveForward Behaviors

MoveZAxis creates the optical illusion of moving in 3D space. MoveForward increases the movement of a sprite on the z-axis and shifts the sprite's position from left to right. Click down on the screen to increase the speed of movement along the z axis. Click down and move right to move the sprites to the right; click down and move left to move the sprites to the left.

MoveZAxis accepts the following arguments:

Setup Script

The MoveZAxis behavior is attached to each moving sprite. Here's the setup code for a single moving sprite:

this.catAnim1 = {
	spriteProps:{name:'catAnim', x:0, y:0, w:400, h:220, XYscale: [0.5, 0.5], rotation: 0, graphics:['img/cat-sprite-sheet.png'], sheets:[this.catSheet], relative2VP:{x:300,y:200,z:4000}, visible:true}, 
	painters:[new BitmapPainter()], 
	behaviors:[new MoveZAxis({myMain: this.myMain}), new Cycling({cycleTime: 60})]
  };
  
  this.myCatAnim1 = new BitmapGraphic(this.catAnim1);
	

Notice that the sprite has relative2VP (relative to vanishing point) on its spriteProps. Use the relative2VP property to set a sprite's z position and offset its x and y position relative to the vanishing point.

MoveForward is attached to a SpriteTroupe sprite, a sprite without a visual display that groups together the moving sprites on the screen (see js/scripts/all-sprites.js to review the code for SpriteTroupe). The SpriteTroupe uses the MoveForward behavior to alter the moving sprites' x, y, and z velocities, calling MoveZAxis to update those velocities.

Here's the setup code for our SpriteTroupe sprite:

this.movingSprites = [this.myCatAnim1, this.myCatAnim2, this.myCatAnim3];
  
  this.movingTroupe = {
	spriteProps:{name:'movingTroupe', x:600, y:400, w:1200, h:800, sprites: this.movingSprites, visible:false}, 
	painters:null, 
	behaviors:[new MoveForward({sprites: this.movingSprites}), new MouseOver()]
  };
  this.movingTroupSprite =  new SpriteTroupe(this.movingTroupe);
	

Notice the SpriteTroupe's spriteProps contains a new property called sprites. sprites is set to an array that houses all the moving sprites. Also note that the sprite's visible is set to false and its painters is set to null. We do that because this sprite doesn't display on the screen, instead it paints and updates the sprites contained within its sprites array property.

See the whole setup script at js/projects/movement/move-z-axis-setup.js.

The basic algorithm for this project comes from Billy Lamberta and Keith Peters' Foundational HTML5 Animation with JavaScript, a great book to check out if you want to learn more about interaction and animation using JavaScript.