Dart Documentationbox2d_htmlIViewportTransform

IViewportTransform class

This is the viewport transform used from drawing. Use yFlip if you are drawing from the top-left corner.

class IViewportTransform {
  IViewportTransform(Vector e, Vector c, num s) :
    extents = new Vector.copy(e),
    center = new Vector.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.
   */
  Vector extents;

  /**
   * Returns the scaling factor used in converting from world sizes to rendering
   * sizes.
   */
  num scale;
  
  /**
   * center of the viewport.
   */
  Vector 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.setCoords(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).
   */
  Vector get translation() {
    Vector result = new Vector.copy(extents);
    result.subLocal(center);
    return result;
  }

  void set translation(Vector translation) {
    center.setFrom(extents);
    center.subLocal(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(Vector argWorld, Vector 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.setCoords(gridCorrectedX + translation.x, 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(Vector argScreen, Vector 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.setCoords(gridCorrectedX, gridCorrectedY);
  }
}

Subclasses

CanvasViewportTransform

Constructors

new IViewportTransform(Vector e, Vector c, num s) #

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

Properties

Vector center #

center of the viewport.

Vector center;

Vector extents #

This is the half-width and half-height. This should be the actual half-width and half-height, not anything transformed or scaled.

Vector extents;

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

num scale #

Returns the scaling factor used in converting from world sizes to rendering sizes.

num scale;

Vector 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).

Vector get translation() {
  Vector result = new Vector.copy(extents);
  result.subLocal(center);
  return result;
}
void set translation(Vector translation) {
  center.setFrom(extents);
  center.subLocal(translation);
}

bool yFlip #

if we flip the y axis when transforming.

bool yFlip;

Operators

bool operator ==(other) #

inherited from Object

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

bool operator ==(other) => identical(this, other);

Methods

void getScreenToWorld(Vector argScreen, Vector 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(Vector argScreen, Vector 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.setCoords(gridCorrectedX, gridCorrectedY);
}

void getWorldToScreen(Vector argWorld, Vector 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(Vector argWorld, Vector 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.setCoords(gridCorrectedX + translation.x, gridCorrectedY +
      -translation.y);
}

int hashCode() #

inherited from Object

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

external int hashCode();

new IViewportTransform(Vector e, Vector c, num s) #

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

noSuchMethod(String name, List args) #

inherited from Object

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

external Dynamic noSuchMethod(String name, List args);

const Object() #

inherited from Object

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

const Object();

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.setCoords(x, y);
  scale = s;
}

String toString() #

inherited from Object

Returns a string representation of this object.

external String toString();