Dart Documentationbot_retainedCanvasUtil

CanvasUtil class

class CanvasUtil {
  /**
   * (√2 - 1) * 4 / 3;
   */
  static final num kappa = 0.55228474983079339840225163227959743809289583383593;

  static Size getCanvasSize(CanvasElement canvasElement) {
    return new Size(canvasElement.width, canvasElement.height);
  }

  static void transform(CanvasRenderingContext2D ctx, AffineTransform tx){
    requireArgumentNotNull(ctx, 'ctx');
    requireArgumentNotNull(tx, 'tx');

    ctx.transform(tx.scaleX, tx.shearY, tx.shearX,
      tx.scaleY, tx.translateX, tx.translateY);
  }

  static void centeredCircle(CanvasRenderingContext2D ctx,
                             num x, num y, num radius) {
    ellipse(ctx, x - radius, y - radius, radius * 2, radius * 2);
  }

  static void star(CanvasRenderingContext2D ctx, num x, num y,
                   num outterRadius, [int pointCount = 5]) {
    requireArgumentNotNull(ctx, 'ctx');
    requireArgument(isValidNumber(x), 'x');
    requireArgument(isValidNumber(y), 'y');
    requireArgument(isValidNumber(outterRadius), 'outterRadius');
    requireArgument(outterRadius >= 0, 'outterRadius');
    requireArgument(isValidNumber(pointCount), 'pointCount');
    requireArgument(pointCount >= 5, 'pointCount');

    final sliceSize = math.PI / pointCount;

    // some day I'll document how I found this
    // It was a lot of paper + WolframAlpha
    // value is the ratio of the inner radius to the outter radius
    // to give the star 'clean lines'
    final innerRatio = math.cos(2 * sliceSize) / math.cos(sliceSize);

    final center = new Coordinate(x, y);
    final tx = new AffineTransform();
    final outterVect = new Vector(0, -outterRadius);
    final innerVect = outterVect.scale(innerRatio);

    ctx.beginPath();

    for(var i = 0; i < pointCount; i++) {
      // outter point
      var radians = i * 2 * sliceSize;
      tx.setToRotation(radians, x, y);
      var point = outterVect + center;
      point = tx.transformCoordinate(point);
      if(i == 0) {
        ctx.moveTo(point.x, point.y);
      }
      else {
        ctx.lineTo(point.x, point.y);
      }

      // inner point
      radians = radians + sliceSize;
      tx.setToRotation(radians, x, y);
      point = innerVect + center;
      point = tx.transformCoordinate(point);
      ctx.lineTo(point.x, point.y);
    }
    ctx.closePath();
  }

  static void drawImage(CanvasRenderingContext2D ctx, ImageElement img,
                        Box sourceBox, [Box targetBox = null]) {

    if(targetBox == null) {
      targetBox = new Box(0, 0, sourceBox.width, sourceBox.height);
    }

    ctx.drawImage(img,
        sourceBox.left, sourceBox.top, sourceBox.width, sourceBox.height,
        targetBox.left, targetBox.top, targetBox.width, targetBox.height);
  }

  static void ellipse(CanvasRenderingContext2D ctx,
                      num x, num y, num width, num height) {
    var hB = (width / 2) * kappa,
      vB = (height / 2) * kappa,
      eX = x + width,
      eY = y + height,
      mX = x + width / 2,
      mY = y + height / 2;
    ctx.beginPath();
    ctx.moveTo(x, mY);
    ctx.bezierCurveTo(x, mY - vB, mX - hB, y, mX, y);
    ctx.bezierCurveTo(mX + hB, y, eX, mY - vB, eX, mY);
    ctx.bezierCurveTo(eX, mY + vB, mX + hB, eY, mX, eY);
    ctx.bezierCurveTo(mX - hB, eY, x, mY + vB, x, mY);
    ctx.closePath();
  }
}

Static Properties

final num kappa #

(√2 - 1) * 4 / 3;

static final num kappa = 0.55228474983079339840225163227959743809289583383593;

Static Methods

Size getCanvasSize(CanvasElement canvasElement) #

static Size getCanvasSize(CanvasElement canvasElement) {
  return new Size(canvasElement.width, canvasElement.height);
}

void transform(CanvasRenderingContext2D ctx, AffineTransform tx) #

static void transform(CanvasRenderingContext2D ctx, AffineTransform tx){
  requireArgumentNotNull(ctx, 'ctx');
  requireArgumentNotNull(tx, 'tx');

  ctx.transform(tx.scaleX, tx.shearY, tx.shearX,
    tx.scaleY, tx.translateX, tx.translateY);
}

void centeredCircle(CanvasRenderingContext2D ctx, num x, num y, num radius) #

static void centeredCircle(CanvasRenderingContext2D ctx,
                           num x, num y, num radius) {
  ellipse(ctx, x - radius, y - radius, radius * 2, radius * 2);
}

void star(CanvasRenderingContext2D ctx, num x, num y, num outterRadius, [int pointCount = 5]) #

static void star(CanvasRenderingContext2D ctx, num x, num y,
                 num outterRadius, [int pointCount = 5]) {
  requireArgumentNotNull(ctx, 'ctx');
  requireArgument(isValidNumber(x), 'x');
  requireArgument(isValidNumber(y), 'y');
  requireArgument(isValidNumber(outterRadius), 'outterRadius');
  requireArgument(outterRadius >= 0, 'outterRadius');
  requireArgument(isValidNumber(pointCount), 'pointCount');
  requireArgument(pointCount >= 5, 'pointCount');

  final sliceSize = math.PI / pointCount;

  // some day I'll document how I found this
  // It was a lot of paper + WolframAlpha
  // value is the ratio of the inner radius to the outter radius
  // to give the star 'clean lines'
  final innerRatio = math.cos(2 * sliceSize) / math.cos(sliceSize);

  final center = new Coordinate(x, y);
  final tx = new AffineTransform();
  final outterVect = new Vector(0, -outterRadius);
  final innerVect = outterVect.scale(innerRatio);

  ctx.beginPath();

  for(var i = 0; i < pointCount; i++) {
    // outter point
    var radians = i * 2 * sliceSize;
    tx.setToRotation(radians, x, y);
    var point = outterVect + center;
    point = tx.transformCoordinate(point);
    if(i == 0) {
      ctx.moveTo(point.x, point.y);
    }
    else {
      ctx.lineTo(point.x, point.y);
    }

    // inner point
    radians = radians + sliceSize;
    tx.setToRotation(radians, x, y);
    point = innerVect + center;
    point = tx.transformCoordinate(point);
    ctx.lineTo(point.x, point.y);
  }
  ctx.closePath();
}

void drawImage(CanvasRenderingContext2D ctx, ImageElement img, Box sourceBox, [Box targetBox = null]) #

static void drawImage(CanvasRenderingContext2D ctx, ImageElement img,
                      Box sourceBox, [Box targetBox = null]) {

  if(targetBox == null) {
    targetBox = new Box(0, 0, sourceBox.width, sourceBox.height);
  }

  ctx.drawImage(img,
      sourceBox.left, sourceBox.top, sourceBox.width, sourceBox.height,
      targetBox.left, targetBox.top, targetBox.width, targetBox.height);
}

void ellipse(CanvasRenderingContext2D ctx, num x, num y, num width, num height) #

static void ellipse(CanvasRenderingContext2D ctx,
                    num x, num y, num width, num height) {
  var hB = (width / 2) * kappa,
    vB = (height / 2) * kappa,
    eX = x + width,
    eY = y + height,
    mX = x + width / 2,
    mY = y + height / 2;
  ctx.beginPath();
  ctx.moveTo(x, mY);
  ctx.bezierCurveTo(x, mY - vB, mX - hB, y, mX, y);
  ctx.bezierCurveTo(mX + hB, y, eX, mY - vB, eX, mY);
  ctx.bezierCurveTo(eX, mY + vB, mX + hB, eY, mX, eY);
  ctx.bezierCurveTo(mX - hB, eY, x, mY + vB, x, mY);
  ctx.closePath();
}

Properties

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

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

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

String toString() #

inherited from Object

Returns a string representation of this object.

external String toString();