Dart Documentationbox2d_browserCanvasDraw

CanvasDraw class

class CanvasDraw extends DebugDraw {
 /** The canvas rendering context with which to draw. */
 CanvasRenderingContext2D ctx;

 CanvasDraw(ViewportTransform viewport, this.ctx) : super(viewport) {
   assert (null != viewport && null != ctx);
 }

 /**
  * Draw a closed polygon provided in CCW order. WARNING: This mutates
  * [vertices].
  */
 void drawPolygon(List<vec2> vertices, int vertexCount, Color3 color) {
   _pathPolygon(vertices, vertexCount, color);
   ctx.stroke();
 }

 /**
  * Draw a solid closed polygon provided in CCW order. WARNING: This mutates
  * [vertices].
  */
 void drawSolidPolygon(List<vec2> vertices, int vertexCount, Color3 color) {
   _pathPolygon(vertices, vertexCount, color);
   ctx.fill();
 }

 void _pathPolygon(List<vec2> vertices, int vertexCount, Color3 color) {
   // Set the color and convert to screen coordinates.
   _color = color;
   // TODO(gregbglw): Do a single ctx transform rather than convert all of
   // these vectors.
   for (int i = 0; i < vertexCount; ++i)
     getWorldToScreenToOut(vertices[i], vertices[i]);

   ctx.beginPath();
   ctx.moveTo(vertices[0].x, vertices[0].y);

   // Draw lines to all of the remaining points.
   for (int i = 1; i < vertexCount; ++i)
     ctx.lineTo(vertices[i].x, vertices[i].y);

   // Draw a line back to the starting point.
   ctx.lineTo(vertices[0].x, vertices[0].y);

   // Close the drawn polygon ready for fill/stroke
   ctx.closePath();
 }

 /** Draw a line segment. WARNING: This mutates [p1] and [p2]. */
 void drawSegment(vec2 p1, vec2 p2, Color3 color) {
   _color = color;
   getWorldToScreenToOut(p1, p1);
   getWorldToScreenToOut(p2, p2);

   ctx.beginPath();
   ctx.moveTo(p1.x, p1.y);
   ctx.lineTo(p2.x, p2.y);
   ctx.closePath();
   ctx.stroke();
 }

 /** Draw a circle. WARNING: This mutates [center]. */
 void drawCircle(vec2 center, num radius, Color3 color, [vec2 axis]) {
   radius *= viewportTransform.scale;
   _pathCircle(center, radius, color);
   ctx.stroke();
 }

 /** Draw a solid circle. WARNING: This mutates [center]. */
 void drawSolidCircle(vec2 center, num radius, Color3 color, [vec2 axis]) {
   radius *= viewportTransform.scale;
   drawPoint(center, radius, color);
 }

 /**
  * Draws the given point with the given *unscaled* radius, in the given color.
  * WARNING: This mutates [center].
  */
 void drawPoint(vec2 point, num radiusOnScreen, Color3 color) {
   _pathCircle(point, radiusOnScreen, color);
   ctx.fill();
 }

 void _pathCircle(vec2 center, num radius, Color3 color) {
   _color = color;
   getWorldToScreenToOut(center, center);

   ctx.beginPath();
   ctx.arc(center.x, center.y, radius, 0, MathBox.TWO_PI, true);
   ctx.closePath();
 }

 /**
  * Draw a transform. Choose your own length scale. WARNING: This mutates
  * [xf.position].
  */
 void drawTransform(Transform xf, Color3 color) {
   drawCircle(xf.position, 0.1, color);
   // TODO(rupertk): Draw rotation representation (drawCircle axis parameter?)
 }

 /** Draw a string. */
 void drawString(num x, num y, String s, Color3 color) {
   _color = color;
   ctx.strokeText(s, x, y);
 }

 /** Sets the rendering context stroke and fill color to [color]. */
 void set _color(Color3 color) {
   ctx.setStrokeColorRgb(color.x, color.y, color.z, 0.9);
   ctx.setFillColorRgb(color.x, color.y, color.z, 0.8);
 }
}

Extends

DebugDraw > CanvasDraw

Constructors

new CanvasDraw(ViewportTransform viewport, CanvasRenderingContext2D ctx) #

CanvasDraw(ViewportTransform viewport, this.ctx) : super(viewport) {
 assert (null != viewport && null != ctx);
}

Properties

CanvasRenderingContext2D ctx #

ctx

int flags #

inherited from DebugDraw
flags

ViewportTransform viewportTransform #

inherited from DebugDraw
viewportTransform

Methods

void appendFlags(int value) #

inherited from DebugDraw
void appendFlags(int value) { flags |= value; }

void clearFlags(int value) #

inherited from DebugDraw
void clearFlags(int value) { flags &= ~value; }

void drawCircle(vec2 center, num radius, Color3 color, [vec2 axis]) #

Draw a circle. WARNING: This mutates center.

void drawCircle(vec2 center, num radius, Color3 color, [vec2 axis]) {
 radius *= viewportTransform.scale;
 _pathCircle(center, radius, color);
 ctx.stroke();
}

void drawPoint(vec2 point, num radiusOnScreen, Color3 color) #

Draws the given point with the given unscaled radius, in the given color. WARNING: This mutates center.

void drawPoint(vec2 point, num radiusOnScreen, Color3 color) {
 _pathCircle(point, radiusOnScreen, color);
 ctx.fill();
}

void drawPolygon(List<vec2> vertices, int vertexCount, Color3 color) #

Draw a closed polygon provided in CCW order. WARNING: This mutates vertices.

void drawPolygon(List<vec2> vertices, int vertexCount, Color3 color) {
 _pathPolygon(vertices, vertexCount, color);
 ctx.stroke();
}

void drawSegment(vec2 p1, vec2 p2, Color3 color) #

Draw a line segment. WARNING: This mutates p1 and p2.

void drawSegment(vec2 p1, vec2 p2, Color3 color) {
 _color = color;
 getWorldToScreenToOut(p1, p1);
 getWorldToScreenToOut(p2, p2);

 ctx.beginPath();
 ctx.moveTo(p1.x, p1.y);
 ctx.lineTo(p2.x, p2.y);
 ctx.closePath();
 ctx.stroke();
}

void drawSolidCircle(vec2 center, num radius, Color3 color, [vec2 axis]) #

Draw a solid circle. WARNING: This mutates center.

void drawSolidCircle(vec2 center, num radius, Color3 color, [vec2 axis]) {
 radius *= viewportTransform.scale;
 drawPoint(center, radius, color);
}

void drawSolidPolygon(List<vec2> vertices, int vertexCount, Color3 color) #

Draw a solid closed polygon provided in CCW order. WARNING: This mutates vertices.

void drawSolidPolygon(List<vec2> vertices, int vertexCount, Color3 color) {
 _pathPolygon(vertices, vertexCount, color);
 ctx.fill();
}

void drawString(num x, num y, String s, Color3 color) #

Draw a string.

void drawString(num x, num y, String s, Color3 color) {
 _color = color;
 ctx.strokeText(s, x, y);
}

void drawTransform(Transform xf, Color3 color) #

Draw a transform. Choose your own length scale. WARNING: This mutates xf.position.

void drawTransform(Transform xf, Color3 color) {
 drawCircle(xf.position, 0.1, color);
 // TODO(rupertk): Draw rotation representation (drawCircle axis parameter?)
}

void getScreenToWorldToOut(vec2 argScreen, vec2 argWorld) #

inherited from DebugDraw

Screen coordinates are specified in argScreen. These coordinates are converted to World coordinates and placed in the argWorld return vector.

void getScreenToWorldToOut(vec2 argScreen, vec2 argWorld) {
 viewportTransform.getScreenToWorld(argScreen, argWorld);
}

void getWorldToScreenToOut(vec2 argWorld, vec2 argScreen) #

inherited from DebugDraw

World coordinates are specified in argWorld. These coordinates are converted to screen coordinates and placed in the argScreen return vector.

void getWorldToScreenToOut(vec2 argWorld, vec2 argScreen) {
 viewportTransform.getWorldToScreen(argWorld, argScreen);
}

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

inherited from DebugDraw

Sets the center of the viewport to the given x and y values and the viewport scale to the given scale.

void setCamera(num x, num y, num scale) {
 viewportTransform.setCamera(x,y,scale);
}