Dart Documentationbox2dViewportTransform

ViewportTransform class

class ViewportTransform {
 ViewportTransform(vec2 e, vec2 c, num s) :
   extents = new vec2.copy(e),
   center = new vec2.copy(c),
   scale = s;

 /**
  * if we flip the y axis when transforming.
  */
 bool yFlip;

 /**
  * This is the half-width and half-height.
  * This should be the actual half-width and 
  * half-height, not anything transformed or scaled.
  */
 vec2 extents;

 /**
  * Returns the scaling factor used in converting from world sizes to rendering
  * sizes.
  */
 num scale;
 
 /**
  * center of the viewport.
  */
 vec2 center;

 /**
  * Sets the transform's center to the given x and y coordinates,
  * and using the given scale.
  */
 void setCamera(num x, num y, num s) {
   center.setComponents(x,y);
   scale = s;
 }

 /**
  * The current translation is the difference in canvas units between the
  * actual center of the canvas and the currently specified center. For
  * example, if the actual canvas center is (5, 5) but the current center is
  * (6, 6), the translation is (1, 1).
  */
 vec2 get translation => extents - center;
 void set translation(vec2 translation) {
   center.copyFrom(extents).sub(translation);
 }

 /**
  * Takes the world coordinate (argWorld) puts the corresponding
  * screen coordinate in argScreen.  It should be safe to give the
  * same object as both parameters.
  */
 void getWorldToScreen(vec2 argWorld, vec2 argScreen) {
   // Correct for canvas considering the upper-left corner, rather than the
   // center, to be the origin.
   num gridCorrectedX = (argWorld.x * scale) + extents.x;
   num gridCorrectedY = extents.y - (argWorld.y * scale);

   argScreen.x = gridCorrectedX + translation.x;
   argScreen.y = gridCorrectedY - translation.y;
 }

 /**
  * Takes the screen coordinates (argScreen) and puts the
  * corresponding world coordinates in argWorld. It should be safe
  * to give the same object as both parameters.
  */
 void getScreenToWorld(vec2 argScreen, vec2 argWorld) {
   num translationCorrectedX = argScreen.x - translation.x;
   num translationCorrectedY = argScreen.y + translation.y;

   num gridCorrectedX = (translationCorrectedX - extents.x) / scale;
   num gridCorrectedY = ((translationCorrectedY - extents.y) * -1) / scale;
   argWorld.x = gridCorrectedX;
   argWorld.y = gridCorrectedY;
 }
}

Subclasses

CanvasViewportTransform

Constructors

new ViewportTransform(vec2 e, vec2 c, num s) #

ViewportTransform(vec2 e, vec2 c, num s) :
 extents = new vec2.copy(e),
 center = new vec2.copy(c),
 scale = s;

Properties

vec2 center #

center

vec2 extents #

extents

num scale #

scale

vec2 translation #

The current translation is the difference in canvas units between the actual center of the canvas and the currently specified center. For example, if the actual canvas center is (5, 5) but the current center is (6, 6), the translation is (1, 1).

vec2 get translation => extents - center;
void set translation(vec2 translation) {
 center.copyFrom(extents).sub(translation);
}

bool yFlip #

yFlip

Methods

void getScreenToWorld(vec2 argScreen, vec2 argWorld) #

Takes the screen coordinates (argScreen) and puts the corresponding world coordinates in argWorld. It should be safe to give the same object as both parameters.

void getScreenToWorld(vec2 argScreen, vec2 argWorld) {
 num translationCorrectedX = argScreen.x - translation.x;
 num translationCorrectedY = argScreen.y + translation.y;

 num gridCorrectedX = (translationCorrectedX - extents.x) / scale;
 num gridCorrectedY = ((translationCorrectedY - extents.y) * -1) / scale;
 argWorld.x = gridCorrectedX;
 argWorld.y = gridCorrectedY;
}

void getWorldToScreen(vec2 argWorld, vec2 argScreen) #

Takes the world coordinate (argWorld) puts the corresponding screen coordinate in argScreen. It should be safe to give the same object as both parameters.

void getWorldToScreen(vec2 argWorld, vec2 argScreen) {
 // Correct for canvas considering the upper-left corner, rather than the
 // center, to be the origin.
 num gridCorrectedX = (argWorld.x * scale) + extents.x;
 num gridCorrectedY = extents.y - (argWorld.y * scale);

 argScreen.x = gridCorrectedX + translation.x;
 argScreen.y = gridCorrectedY - translation.y;
}

void setCamera(num x, num y, num s) #

Sets the transform's center to the given x and y coordinates, and using the given scale.

void setCamera(num x, num y, num s) {
 center.setComponents(x,y);
 scale = s;
}