Dart Documentationbox2d_browserCanvasViewportTransform

CanvasViewportTransform class

class CanvasViewportTransform extends ViewportTransform {
 static const int DEFAULT_DRAWING_SCALE = 20;

 /**
  * Constructs a new viewport transform with the default scale.
  */
 CanvasViewportTransform(vec2 _extents, vec2 _center) :
   super(_extents, _center, DEFAULT_DRAWING_SCALE) {
   yFlip = true;
 }

 /**
  * Sets the rendering context such that all drawing commands given in terms
  * of the world coordinate system will display correctly on the canvas screen.
  */
 void updateTransformation(CanvasRenderingContext2D ctx) {
   // Clear all previous transformation.
   ctx.setTransform(1,0,0,1,0,0);

   // Translate to the center of the canvas screen. This will be considered the
   // actual origin.
   ctx.translate(extents.x, extents.y);

   // Translate to account for the currently applied translation.
   ctx.translate(translation.x, translation.y);

   // Scale everything according to the current scale and mirror the y-axis.
   ctx.scale(scale, -scale);
 }
}

Extends

ViewportTransform > CanvasViewportTransform

Static Properties

const int DEFAULT_DRAWING_SCALE #

DEFAULT_DRAWING_SCALE = 20

Constructors

new CanvasViewportTransform(vec2 _extents, vec2 _center) #

Constructs a new viewport transform with the default scale.

CanvasViewportTransform(vec2 _extents, vec2 _center) :
 super(_extents, _center, DEFAULT_DRAWING_SCALE) {
 yFlip = true;
}

Properties

vec2 center #

inherited from ViewportTransform
center

vec2 extents #

inherited from ViewportTransform
extents

num scale #

inherited from ViewportTransform
scale

vec2 translation #

inherited from ViewportTransform

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 #

inherited from ViewportTransform
yFlip

Methods

void getScreenToWorld(vec2 argScreen, vec2 argWorld) #

inherited from ViewportTransform

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) #

inherited from ViewportTransform

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) #

inherited from ViewportTransform

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;
}

void updateTransformation(CanvasRenderingContext2D ctx) #

Sets the rendering context such that all drawing commands given in terms of the world coordinate system will display correctly on the canvas screen.

void updateTransformation(CanvasRenderingContext2D ctx) {
 // Clear all previous transformation.
 ctx.setTransform(1,0,0,1,0,0);

 // Translate to the center of the canvas screen. This will be considered the
 // actual origin.
 ctx.translate(extents.x, extents.y);

 // Translate to account for the currently applied translation.
 ctx.translate(translation.x, translation.y);

 // Scale everything according to the current scale and mirror the y-axis.
 ctx.scale(scale, -scale);
}