GSDP:GAM100/CProcessing/translate()

From Inside
Jump to: navigation, search

translate()

Description

The function translate() moves the canvas (x,y) from it's current center.

Parameters

translate(float x, float y)

  • x - (float) the new x-coordinate to move towards
  • y - (float) the new y-coordinate to move towards

Example

// Create a variable to keep track of iterations
static float counter = 0;

void update() 
{
  // Expected behavior: Camera will pan upwards
  
  // Clear screen every frame
  background(color(255, 0, 0, 255));
  
  // Move the canvas downwards by 1
  translate(0, counter);
  
  // Fixed points for reference
  // Note: some parameters are multiplied by 1.0f to convert int to float
  rect(0, 0, 100, 50);				   // Draw rect clouds at the top left corner
  rect(width * 1.0f - 100, 0, 100, 50);		   // Draw rect clouds at the top right corner
  rect(0, height * 1.0f - 200, width * 1.0f, 200); // Draw the ground
  ellipse(width / 2.0f, height / 2.0f, 20, 20);	   // Draw the sun in the middle of the screen
  
  // Increment the counter
  ++counter;
  
  // Reset the counter after it reaches the bottom
  if (counter == height)
  {
    counter = 0;
  }
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>