Dart Documentationbox2d_htmlCanvasDraw

CanvasDraw class

Draws using the given canvas context for debugging purposes. WARNING: This implementation modifies its arguments (e.g. Vectors) to save garbage.

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

  CanvasDraw(IViewportTransform 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<Vector> 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<Vector> vertices, int vertexCount, Color3 color) {
    _pathPolygon(vertices, vertexCount, color);
    ctx.fill();
  }

  void _pathPolygon(List<Vector> 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(Vector p1, Vector 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(Vector center, num radius, Color3 color, [Vector axis]) {
    radius *= viewportTransform.scale;
    _pathCircle(center, radius, color);
    ctx.stroke();
  }

  /** Draw a solid circle. WARNING: This mutates [center]. */
  void drawSolidCircle(Vector center, num radius, Color3 color, [Vector 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(Vector point, num radiusOnScreen, Color3 color) {
    _pathCircle(point, radiusOnScreen, color);
    ctx.fill();
  }

  void _pathCircle(Vector 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(IViewportTransform viewport, CanvasRenderingContext2D ctx) #

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

Properties

CanvasRenderingContext2D ctx #

The canvas rendering context with which to draw.

CanvasRenderingContext2D ctx;

int flags #

inherited from DebugDraw
int flags;

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

IViewportTransform viewportTransform #

inherited from DebugDraw
IViewportTransform viewportTransform;

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 appendFlags(int value) #

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

new CanvasDraw(IViewportTransform viewport, CanvasRenderingContext2D ctx) #

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

void clearFlags(int value) #

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

new DebugDraw(IViewportTransform viewport) #

inherited from DebugDraw
DebugDraw(IViewportTransform viewport)
    : flags = e_shapeBit,
      viewportTransform = viewport;

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

Draw a circle. WARNING: This mutates center.

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

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

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

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

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

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

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

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

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

void drawSegment(Vector p1, Vector 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(Vector center, num radius, Color3 color, [Vector axis]) #

Draw a solid circle. WARNING: This mutates center.

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

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

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

void drawSolidPolygon(List<Vector> 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(Vector argScreen, Vector 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(Vector argScreen, Vector argWorld) {
  viewportTransform.getScreenToWorld(argScreen, argWorld);
}

void getWorldToScreenToOut(Vector argWorld, Vector 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(Vector argWorld, Vector argScreen) {
  viewportTransform.getWorldToScreen(argWorld, argScreen);
}

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();

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

String toString() #

inherited from Object

Returns a string representation of this object.

external String toString();