CanvasViewportTransform class
class CanvasViewportTransform extends ViewportTransform { static const double DEFAULT_DRAWING_SCALE = 20.0; /** * Constructs a new viewport transform with the default scale. */ CanvasViewportTransform(Vector2 _extents, Vector2 _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
Constructors
Properties
Vector2 extents #
This is the half-width and half-height. This should be the actual half-width and half-height, not anything transformed or scaled.
Vector2 extents
double scale #
Returns the scaling factor used in converting from world sizes to rendering sizes.
double scale
Vector2 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).
Vector2 get translation { Vector2 result = new Vector2.copy(extents); result.sub(center); return result; }
void set translation(Vector2 translation) { center.setFrom(extents); center.sub(translation); }
Methods
void getScreenToWorld(Vector2 argScreen, Vector2 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(Vector2 argScreen, Vector2 argWorld) { double translationCorrectedX = argScreen.x - translation.x; double translationCorrectedY = argScreen.y + translation.y; double gridCorrectedX = (translationCorrectedX - extents.x) / scale; double gridCorrectedY = ((translationCorrectedY - extents.y) * -1) / scale; argWorld.setValues(gridCorrectedX, gridCorrectedY); }
void getWorldToScreen(Vector2 argWorld, Vector2 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(Vector2 argWorld, Vector2 argScreen) { // Correct for canvas considering the upper-left corner, rather than the // center, to be the origin. double gridCorrectedX = (argWorld.x * scale) + extents.x; double gridCorrectedY = extents.y - (argWorld.y * scale); argScreen.setValues(gridCorrectedX + translation.x, gridCorrectedY + -translation.y); }
void setCamera(double x, double y, double s) #
Sets the transform's center to the given x and y coordinates, and using the given scale.
void setCamera(double x, double y, double s) { center.setValues(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); }