Dart API Referencedart:htmlCanvasRenderingContext2D

CanvasRenderingContext2D class

The bulk of the operations available at present with <canvas> are available through this interface, returned by a call to getContext() on the <canvas> element, with "2d" as its argument.
@DomName('CanvasRenderingContext2D')
class CanvasRenderingContext2D extends CanvasRenderingContext native "CanvasRenderingContext2D" {

 @DomName('CanvasRenderingContext2D.currentPath')
 @DocsEditable
 Path currentPath;

 @DomName('CanvasRenderingContext2D.fillStyle')
 @DocsEditable
 @Creates('String|CanvasGradient|CanvasPattern')
 @Returns('String|CanvasGradient|CanvasPattern')
 dynamic fillStyle;

 @DomName('CanvasRenderingContext2D.font')
 @DocsEditable
 String font;

 @DomName('CanvasRenderingContext2D.globalAlpha')
 @DocsEditable
 num globalAlpha;

 @DomName('CanvasRenderingContext2D.globalCompositeOperation')
 @DocsEditable
 String globalCompositeOperation;

 @DomName('CanvasRenderingContext2D.lineCap')
 @DocsEditable
 String lineCap;

 @DomName('CanvasRenderingContext2D.lineJoin')
 @DocsEditable
 String lineJoin;

 @DomName('CanvasRenderingContext2D.lineWidth')
 @DocsEditable
 num lineWidth;

 @DomName('CanvasRenderingContext2D.miterLimit')
 @DocsEditable
 num miterLimit;

 @DomName('CanvasRenderingContext2D.shadowBlur')
 @DocsEditable
 num shadowBlur;

 @DomName('CanvasRenderingContext2D.shadowColor')
 @DocsEditable
 String shadowColor;

 @DomName('CanvasRenderingContext2D.shadowOffsetX')
 @DocsEditable
 num shadowOffsetX;

 @DomName('CanvasRenderingContext2D.shadowOffsetY')
 @DocsEditable
 num shadowOffsetY;

 @DomName('CanvasRenderingContext2D.strokeStyle')
 @DocsEditable
 @Creates('String|CanvasGradient|CanvasPattern')
 @Returns('String|CanvasGradient|CanvasPattern')
 dynamic strokeStyle;

 @DomName('CanvasRenderingContext2D.textAlign')
 @DocsEditable
 String textAlign;

 @DomName('CanvasRenderingContext2D.textBaseline')
 @DocsEditable
 String textBaseline;

 @JSName('webkitBackingStorePixelRatio')
 @DomName('CanvasRenderingContext2D.webkitBackingStorePixelRatio')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 final num backingStorePixelRatio;

 @JSName('webkitImageSmoothingEnabled')
 @DomName('CanvasRenderingContext2D.webkitImageSmoothingEnabled')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 bool imageSmoothingEnabled;

 @JSName('arc')
 @DomName('CanvasRenderingContext2D.arc')
 @DocsEditable
 void $dom_arc(num x, num y, num radius, num startAngle, num endAngle, bool anticlockwise) native;

 @DomName('CanvasRenderingContext2D.arcTo')
 @DocsEditable
 void arcTo(num x1, num y1, num x2, num y2, num radius) native;

 @DomName('CanvasRenderingContext2D.beginPath')
 @DocsEditable
 void beginPath() native;

 @DomName('CanvasRenderingContext2D.bezierCurveTo')
 @DocsEditable
 void bezierCurveTo(num cp1x, num cp1y, num cp2x, num cp2y, num x, num y) native;

 @DomName('CanvasRenderingContext2D.clearRect')
 @DocsEditable
 void clearRect(num x, num y, num width, num height) native;

 @DomName('CanvasRenderingContext2D.clip')
 @DocsEditable
 void clip([String winding]) native;

 @DomName('CanvasRenderingContext2D.closePath')
 @DocsEditable
 void closePath() native;

 @DomName('CanvasRenderingContext2D.createImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 ImageData createImageData(num sw, num sh) {
   return _convertNativeToDart_ImageData(_createImageData_1(sw, sh));
 }
 @JSName('createImageData')
 @DomName('CanvasRenderingContext2D.createImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 _createImageData_1(sw, sh) native;

 @DomName('CanvasRenderingContext2D.createImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 ImageData createImageDataFromImageData(ImageData imagedata) {
   var imagedata_1 = _convertDartToNative_ImageData(imagedata);
   return _convertNativeToDart_ImageData(_createImageDataFromImageData_1(imagedata_1));
 }
 @JSName('createImageData')
 @DomName('CanvasRenderingContext2D.createImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 _createImageDataFromImageData_1(imagedata) native;

 @DomName('CanvasRenderingContext2D.createLinearGradient')
 @DocsEditable
 CanvasGradient createLinearGradient(num x0, num y0, num x1, num y1) native;

 @DomName('CanvasRenderingContext2D.createPattern')
 @DocsEditable
 CanvasPattern createPattern(canvas_OR_image, String repetitionType) native;

 @DomName('CanvasRenderingContext2D.createRadialGradient')
 @DocsEditable
 CanvasGradient createRadialGradient(num x0, num y0, num r0, num x1, num y1, num r1) native;

 @DomName('CanvasRenderingContext2D.fill')
 @DocsEditable
 void fill([String winding]) native;

 @DomName('CanvasRenderingContext2D.fillRect')
 @DocsEditable
 void fillRect(num x, num y, num width, num height) native;

 @DomName('CanvasRenderingContext2D.fillText')
 @DocsEditable
 void fillText(String text, num x, num y, [num maxWidth]) native;

 @DomName('CanvasRenderingContext2D.getContextAttributes')
 @DocsEditable
 Canvas2DContextAttributes getContextAttributes() native;

 @DomName('CanvasRenderingContext2D.getImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 ImageData getImageData(num sx, num sy, num sw, num sh) {
   return _convertNativeToDart_ImageData(_getImageData_1(sx, sy, sw, sh));
 }
 @JSName('getImageData')
 @DomName('CanvasRenderingContext2D.getImageData')
 @DocsEditable
 @Creates('ImageData|=Object')
 _getImageData_1(sx, sy, sw, sh) native;

 @DomName('CanvasRenderingContext2D.getLineDash')
 @DocsEditable
 List<num> getLineDash() native;

 @DomName('CanvasRenderingContext2D.isPointInPath')
 @DocsEditable
 bool isPointInPath(num x, num y, [String winding]) native;

 @DomName('CanvasRenderingContext2D.isPointInStroke')
 @DocsEditable
 bool isPointInStroke(num x, num y) native;

 @DomName('CanvasRenderingContext2D.lineTo')
 @DocsEditable
 void lineTo(num x, num y) native;

 @DomName('CanvasRenderingContext2D.measureText')
 @DocsEditable
 TextMetrics measureText(String text) native;

 @DomName('CanvasRenderingContext2D.moveTo')
 @DocsEditable
 void moveTo(num x, num y) native;

 @DomName('CanvasRenderingContext2D.putImageData')
 @DocsEditable
 void putImageData(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) {
   if (!?dirtyX && !?dirtyY && !?dirtyWidth && !?dirtyHeight) {
     var imagedata_1 = _convertDartToNative_ImageData(imagedata);
     _putImageData_1(imagedata_1, dx, dy);
     return;
   }
   var imagedata_2 = _convertDartToNative_ImageData(imagedata);
   _putImageData_2(imagedata_2, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
   return;
   throw new ArgumentError("Incorrect number or type of arguments");
 }
 @JSName('putImageData')
 @DomName('CanvasRenderingContext2D.putImageData')
 @DocsEditable
 void _putImageData_1(imagedata, dx, dy) native;
 @JSName('putImageData')
 @DomName('CanvasRenderingContext2D.putImageData')
 @DocsEditable
 void _putImageData_2(imagedata, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight) native;

 @DomName('CanvasRenderingContext2D.quadraticCurveTo')
 @DocsEditable
 void quadraticCurveTo(num cpx, num cpy, num x, num y) native;

 @DomName('CanvasRenderingContext2D.rect')
 @DocsEditable
 void rect(num x, num y, num width, num height) native;

 @DomName('CanvasRenderingContext2D.restore')
 @DocsEditable
 void restore() native;

 @DomName('CanvasRenderingContext2D.rotate')
 @DocsEditable
 void rotate(num angle) native;

 @DomName('CanvasRenderingContext2D.save')
 @DocsEditable
 void save() native;

 @DomName('CanvasRenderingContext2D.scale')
 @DocsEditable
 void scale(num sx, num sy) native;

 @DomName('CanvasRenderingContext2D.setLineDash')
 @DocsEditable
 void setLineDash(List<num> dash) native;

 @DomName('CanvasRenderingContext2D.setTransform')
 @DocsEditable
 void setTransform(num m11, num m12, num m21, num m22, num dx, num dy) native;

 @DomName('CanvasRenderingContext2D.stroke')
 @DocsEditable
 void stroke() native;

 @DomName('CanvasRenderingContext2D.strokeRect')
 @DocsEditable
 void strokeRect(num x, num y, num width, num height, [num lineWidth]) native;

 @DomName('CanvasRenderingContext2D.strokeText')
 @DocsEditable
 void strokeText(String text, num x, num y, [num maxWidth]) native;

 @DomName('CanvasRenderingContext2D.transform')
 @DocsEditable
 void transform(num m11, num m12, num m21, num m22, num dx, num dy) native;

 @DomName('CanvasRenderingContext2D.translate')
 @DocsEditable
 void translate(num tx, num ty) native;

 @DomName('CanvasRenderingContext2D.webkitGetImageDataHD')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 @Creates('ImageData|=Object')
 ImageData getImageDataHD(num sx, num sy, num sw, num sh) {
   return _convertNativeToDart_ImageData(_getImageDataHD_1(sx, sy, sw, sh));
 }
 @JSName('webkitGetImageDataHD')
 @DomName('CanvasRenderingContext2D.webkitGetImageDataHD')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 @Creates('ImageData|=Object')
 _getImageDataHD_1(sx, sy, sw, sh) native;

 @DomName('CanvasRenderingContext2D.webkitPutImageDataHD')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 void putImageDataHD(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) {
   if (!?dirtyX && !?dirtyY && !?dirtyWidth && !?dirtyHeight) {
     var imagedata_1 = _convertDartToNative_ImageData(imagedata);
     _putImageDataHD_1(imagedata_1, dx, dy);
     return;
   }
   var imagedata_2 = _convertDartToNative_ImageData(imagedata);
   _putImageDataHD_2(imagedata_2, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
   return;
   throw new ArgumentError("Incorrect number or type of arguments");
 }
 @JSName('webkitPutImageDataHD')
 @DomName('CanvasRenderingContext2D.webkitPutImageDataHD')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 void _putImageDataHD_1(imagedata, dx, dy) native;
 @JSName('webkitPutImageDataHD')
 @DomName('CanvasRenderingContext2D.webkitPutImageDataHD')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 void _putImageDataHD_2(imagedata, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight) native;


 /**
  * Sets the color used inside shapes.
  * [r], [g], [b] are 0-255, [a] is 0-1.
  */
 void setFillColorRgb(int r, int g, int b, [num a = 1]) {
   this.fillStyle = 'rgba($r, $g, $b, $a)';
 }

 /**
  * Sets the color used inside shapes.
  * [h] is in degrees, 0-360.
  * [s], [l] are in percent, 0-100.
  * [a] is 0-1.
  */
 void setFillColorHsl(int h, num s, num l, [num a = 1]) {
   this.fillStyle = 'hsla($h, $s%, $l%, $a)';
 }

 /**
  * Sets the color used for stroking shapes.
  * [r], [g], [b] are 0-255, [a] is 0-1.
  */
 void setStrokeColorRgb(int r, int g, int b, [num a = 1]) {
   this.strokeStyle = 'rgba($r, $g, $b, $a)';
 }

 /**
  * Sets the color used for stroking shapes.
  * [h] is in degrees, 0-360.
  * [s], [l] are in percent, 0-100.
  * [a] is 0-1.
  */
 void setStrokeColorHsl(int h, num s, num l, [num a = 1]) {
   this.strokeStyle = 'hsla($h, $s%, $l%, $a)';
 }

 @DomName('CanvasRenderingContext2D.arc')
 void arc(num x,  num y,  num radius,  num startAngle, num endAngle,
     [bool anticlockwise = false]) {
   $dom_arc(x, y, radius, startAngle, endAngle, anticlockwise);
 }

 /**
  * Draws an image from a CanvasImageSource to an area of this canvas.
  *
  * The image will be drawn to an area of this canvas defined by
  * [destRect]. [sourceRect] defines the region of the source image that is
  * drawn.
  * If [sourceRect] is not provided, then
  * the entire rectangular image from [source] will be drawn to this context.
  *
  * If the image is larger than canvas
  * will allow, the image will be clipped to fit the available space.
  *
  *     CanvasElement canvas = new CanvasElement(width: 600, height: 600);
  *     CanvasRenderingContext2D ctx = canvas.context2D;
  *     ImageElement img = document.query('img');
  *     img.width = 100;
  *     img.height = 100;
  *
  *     // Scale the image to 20x20.
  *     ctx.drawImageToRect(img, new Rect(50, 50, 20, 20));
  *
  *     VideoElement video = document.query('video');
  *     video.width = 100;
  *     video.height = 100;
  *     // Take the middle 20x20 pixels from the video and stretch them.
  *     ctx.drawImageToRect(video, new Rect(50, 50, 100, 100),
  *         sourceRect: new Rect(40, 40, 20, 20));
  *
  *     // Draw the top 100x20 pixels from the otherCanvas.
  *     CanvasElement otherCanvas = document.query('canvas');
  *     ctx.drawImageToRect(otherCanvas, new Rect(0, 0, 100, 20),
  *         sourceRect: new Rect(0, 0, 100, 20));
  *
  * See also:
  *
  *   * [CanvasImageSource] for more information on what data is retrieved
  * from [source].
  *   * [drawImage](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage)
  * from the WHATWG.
  */
 @DomName('CanvasRenderingContext2D.drawImage')
 void drawImageToRect(CanvasImageSource source, Rect destRect,
     {Rect sourceRect}) {
   if (sourceRect == null) {
     drawImageScaled(source,
         destRect.left,
         destRect.top,
         destRect.width,
         destRect.height);
   } else {
     drawImageScaledFromSource(source,
         sourceRect.left,
         sourceRect.top,
         sourceRect.width,
         sourceRect.height,
         destRect.left,
         destRect.top,
         destRect.width,
         destRect.height);
   }
 }

 /**
  * Draws an image from a CanvasImageSource to this canvas.
  *
  * The entire image from [source] will be drawn to this context with its top
  * left corner at the point ([destX], [destY]). If the image is
  * larger than canvas will allow, the image will be clipped to fit the
  * available space.
  *
  *     CanvasElement canvas = new CanvasElement(width: 600, height: 600);
  *     CanvasRenderingContext2D ctx = canvas.context2D;
  *     ImageElement img = document.query('img');
  *
  *     ctx.drawImage(img, 100, 100);
  *
  *     VideoElement video = document.query('video');
  *     ctx.drawImage(video, 0, 0);
  *
  *     CanvasElement otherCanvas = document.query('canvas');
  *     otherCanvas.width = 100;
  *     otherCanvas.height = 100;
  *     ctx.drawImage(otherCanvas, 590, 590); // will get clipped
  *
  * See also:
  *
  *   * [CanvasImageSource] for more information on what data is retrieved
  * from [source].
  *   * [drawImage](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage)
  * from the WHATWG.
  */
 @DomName('CanvasRenderingContext2D.drawImage')
 @JSName('drawImage')
 void drawImage(CanvasImageSource source, num destX, num destY) native;

 /**
  * Draws an image from a CanvasImageSource to an area of this canvas.
  *
  * The image will be drawn to this context with its top left corner at the
  * point ([destX], [destY]) and will be scaled to be [destWidth] wide and
  * [destHeight] tall.
  *
  * If the image is larger than canvas
  * will allow, the image will be clipped to fit the available space.
  *
  *     CanvasElement canvas = new CanvasElement(width: 600, height: 600);
  *     CanvasRenderingContext2D ctx = canvas.context2D;
  *     ImageElement img = document.query('img');
  *     img.width = 100;
  *     img.height = 100;
  *
  *     // Scale the image to 300x50 at the point (20, 20)
  *     ctx.drawImageScaled(img, 20, 20, 300, 50);
  *
  * See also:
  *
  *   * [CanvasImageSource] for more information on what data is retrieved
  * from [source].
  *   * [drawImage](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage)
  * from the WHATWG.
  */
 @DomName('CanvasRenderingContext2D.drawImage')
 @JSName('drawImage')
 void drawImageScaled(CanvasImageSource source,
     num destX, num destY, num destWidth, num destHeight) native;

 /**
  * Draws an image from a CanvasImageSource to an area of this canvas.
  *
  * The image is a region of [source] that is [sourceWidth] wide and
  * [destHeight] tall with top left corner at ([sourceX], [sourceY]).
  * The image will be drawn to this context with its top left corner at the
  * point ([destX], [destY]) and will be scaled to be [destWidth] wide and
  * [destHeight] tall.
  *
  * If the image is larger than canvas
  * will allow, the image will be clipped to fit the available space.
  *
  *     VideoElement video = document.query('video');
  *     video.width = 100;
  *     video.height = 100;
  *     // Take the middle 20x20 pixels from the video and stretch them.
  *     ctx.drawImageScaledFromSource(video, 40, 40, 20, 20, 50, 50, 100, 100);
  *
  *     // Draw the top 100x20 pixels from the otherCanvas to this one.
  *     CanvasElement otherCanvas = document.query('canvas');
  *     ctx.drawImageScaledFromSource(otherCanvas, 0, 0, 100, 20, 0, 0, 100, 20);
  *
  * See also:
  *
  *   * [CanvasImageSource] for more information on what data is retrieved
  * from [source].
  *   * [drawImage](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage)
  * from the WHATWG.
  */
 @DomName('CanvasRenderingContext2D.drawImage')
 @JSName('drawImage')
 void drawImageScaledFromSource(CanvasImageSource source,
     num sourceX, num sourceY, num sourceWidth, num sourceHeight,
     num destX, num destY, num destWidth, num destHeight) native;

 @DomName('CanvasRenderingContext2D.lineDashOffset')
 num get lineDashOffset => JS('num',
     '#.lineDashOffset || #.webkitLineDashOffset', this, this);

 @DomName('CanvasRenderingContext2D.lineDashOffset')
 void set lineDashOffset(num value) => JS('void', 
     'typeof #.lineDashOffset != "undefined" ? #.lineDashOffset = # : '
     '#.webkitLineDashOffset = #', this, this, value, this, value);
}

Extends

Interceptor > CanvasRenderingContext > CanvasRenderingContext2D

Properties

final num backingStorePixelRatio #

final num backingStorePixelRatio

final CanvasElement canvas #

inherited from CanvasRenderingContext

Reference to the canvas element to which this context belongs.

final CanvasElement canvas

Path currentPath #

Path currentPath

var fillStyle #

Color or style to use inside shapes. Default #000 (black).
dynamic fillStyle

String font #

Default value 10px sans-serif.
String font

num globalAlpha #

Alpha value that is applied to shapes and images before they are composited onto the canvas. Default 1.0 (opaque).
num globalAlpha

String globalCompositeOperation #

With globalAplpha applied this sets how shapes and images are drawn onto the existing bitmap. Possible values:
  • source-atop
  • source-in
  • source-out
  • source-over (default)
  • destination-atop
  • destination-in
  • destination-out
  • destination-over
  • lighter
  • xor
String globalCompositeOperation

final int hashCode #

inherited from Interceptor

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.

docs inherited from Object
int get hashCode => Primitives.objectHashCode(this);

bool imageSmoothingEnabled #

Image smoothing mode; if disabled, images will not be smoothed if scaled. Requires Gecko 1.9.2
bool imageSmoothingEnabled

String lineCap #

Type of endings on the end of lines. Possible values: butt (default), round, square
String lineCap

num lineDashOffset #

@DomName('CanvasRenderingContext2D.lineDashOffset')
num get lineDashOffset => JS('num',
   '#.lineDashOffset || #.webkitLineDashOffset', this, this);
@DomName('CanvasRenderingContext2D.lineDashOffset')
void set lineDashOffset(num value) => JS('void', 
   'typeof #.lineDashOffset != "undefined" ? #.lineDashOffset = # : '
   '#.webkitLineDashOffset = #', this, this, value, this, value);

String lineJoin #

Defines the type of corners where two lines meet. Possible values: round, bevel, miter (default)
String lineJoin

num lineWidth #

Width of lines. Default 1.0
num lineWidth

num miterLimit #

Default 10.
num miterLimit

final Type runtimeType #

inherited from Interceptor

A representation of the runtime type of the object.

docs inherited from Object
Type get runtimeType => getRuntimeType(this);

num shadowBlur #

Specifies the blurring effect. Default 0
num shadowBlur

String shadowColor #

Color of the shadow. Default fully-transparent black.
String shadowColor

num shadowOffsetX #

Horizontal distance the shadow will be offset. Default 0.
num shadowOffsetX

num shadowOffsetY #

Vertical distance the shadow will be offset. Default 0.
num shadowOffsetY

var strokeStyle #

Color or style to use for the lines around shapes. Default #000 (black).
dynamic strokeStyle

String textAlign #

Possible values: start (default), end, left, right or center.
String textAlign

String textBaseline #

String textBaseline

Operators

bool operator ==(other) #

inherited from Interceptor

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.

docs inherited from Object
bool operator ==(other) => identical(this, other);

Methods

void arc(num x, num y, num radius, num startAngle, num endAngle, [bool anticlockwise = false]) #

Adds an arc to the path which it center is at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).

Parameters
x
The x axis of the coordinate for the arc's center
y
The y axis of the coordinate for the arc's center.
radius
The arc's radius
startAngle
The starting point, measured from the x axis , from which it will be drawed expressed as radians.
endAngle
The end arc's angle to which it will be drawed expressed as radians.
anticlockwise Optional from Gecko 2.0
When true draws the arc anticlockwise, otherwise in a clockwise direction.
@DomName('CanvasRenderingContext2D.arc')
void arc(num x,  num y,  num radius,  num startAngle, num endAngle,
   [bool anticlockwise = false]) {
 $dom_arc(x, y, radius, startAngle, endAngle, anticlockwise);
}

void arcTo(num x1, num y1, num x2, num y2, num radius) #

Adds an arc with the given control points and radius, connected to the previous point by a straight line.

Parameters
x1
y1
x2
y2
radius
The arc's radius.
@DomName('CanvasRenderingContext2D.arcTo')
@DocsEditable
void arcTo(num x1, num y1, num x2, num y2, num radius) native;

void beginPath() #

@DomName('CanvasRenderingContext2D.beginPath')
@DocsEditable
void beginPath() native;

void bezierCurveTo(num cp1x, num cp1y, num cp2x, num cp2y, num x, num y) #

@DomName('CanvasRenderingContext2D.bezierCurveTo')
@DocsEditable
void bezierCurveTo(num cp1x, num cp1y, num cp2x, num cp2y, num x, num y) native;

void clearRect(num x, num y, num width, num height) #

Clears the rectangle defined by it starting point at (x, y) and has a w width and a h height.

Parameters
x
The x axis of the coordinate for the rectangle starting point.
y
The y axis of the coordinate for the rectangle starting point.
width
The rectangle's width.
height
The rectangle's height.
@DomName('CanvasRenderingContext2D.clearRect')
@DocsEditable
void clearRect(num x, num y, num width, num height) native;

void clip([String winding]) #

@DomName('CanvasRenderingContext2D.clip')
@DocsEditable
void clip([String winding]) native;

void closePath() #

@DomName('CanvasRenderingContext2D.closePath')
@DocsEditable
void closePath() native;

ImageData createImageData(num sw, num sh) #

@DomName('CanvasRenderingContext2D.createImageData')
@DocsEditable
@Creates('ImageData|=Object')
ImageData createImageData(num sw, num sh) {
 return _convertNativeToDart_ImageData(_createImageData_1(sw, sh));
}

ImageData createImageDataFromImageData(ImageData imagedata) #

@DomName('CanvasRenderingContext2D.createImageData')
@DocsEditable
@Creates('ImageData|=Object')
ImageData createImageDataFromImageData(ImageData imagedata) {
 var imagedata_1 = _convertDartToNative_ImageData(imagedata);
 return _convertNativeToDart_ImageData(_createImageDataFromImageData_1(imagedata_1));
}

CanvasGradient createLinearGradient(num x0, num y0, num x1, num y1) #

@DomName('CanvasRenderingContext2D.createLinearGradient')
@DocsEditable
CanvasGradient createLinearGradient(num x0, num y0, num x1, num y1) native;

CanvasPattern createPattern(canvas_OR_image, String repetitionType) #

Parameters
image
A DOM element to use as the source image for the pattern. This can be any element, although typically you'll use an Image or <canvas> .
repetition
?
Return value

A new DOM canvas pattern object for use in pattern-based operations.

Exceptions thrown
NS_ERROR_DOM_INVALID_STATE_ERR Requires Gecko 10.0
The specified <canvas> element for the image parameter is zero-sized (that is, one or both of its dimensions are 0 pixels).
@DomName('CanvasRenderingContext2D.createPattern')
@DocsEditable
CanvasPattern createPattern(canvas_OR_image, String repetitionType) native;

CanvasGradient createRadialGradient(num x0, num y0, num r0, num x1, num y1, num r1) #

@DomName('CanvasRenderingContext2D.createRadialGradient')
@DocsEditable
CanvasGradient createRadialGradient(num x0, num y0, num r0, num x1, num y1, num r1) native;

void drawImage(CanvasImageSource source, num destX, num destY) #

Draws an image from a CanvasImageSource to this canvas.

The entire image from source will be drawn to this context with its top left corner at the point ( destX, destY). If the image is larger than canvas will allow, the image will be clipped to fit the available space.

CanvasElement canvas = new CanvasElement(width: 600, height: 600);
CanvasRenderingContext2D ctx = canvas.context2D;
ImageElement img = document.query('img');

ctx.drawImage(img, 100, 100);

VideoElement video = document.query('video');
ctx.drawImage(video, 0, 0);

CanvasElement otherCanvas = document.query('canvas');
otherCanvas.width = 100;
otherCanvas.height = 100;
ctx.drawImage(otherCanvas, 590, 590); // will get clipped

See also:

@DomName('CanvasRenderingContext2D.drawImage')
@JSName('drawImage')
void drawImage(CanvasImageSource source, num destX, num destY) native;

void drawImageScaled(CanvasImageSource source, num destX, num destY, num destWidth, num destHeight) #

Draws an image from a CanvasImageSource to an area of this canvas.

The image will be drawn to this context with its top left corner at the point ( destX, destY) and will be scaled to be destWidth wide and destHeight tall.

If the image is larger than canvas will allow, the image will be clipped to fit the available space.

CanvasElement canvas = new CanvasElement(width: 600, height: 600);
CanvasRenderingContext2D ctx = canvas.context2D;
ImageElement img = document.query('img');
img.width = 100;
img.height = 100;

// Scale the image to 300x50 at the point (20, 20)
ctx.drawImageScaled(img, 20, 20, 300, 50);

See also:

@DomName('CanvasRenderingContext2D.drawImage')
@JSName('drawImage')
void drawImageScaled(CanvasImageSource source,
   num destX, num destY, num destWidth, num destHeight) native;

void drawImageScaledFromSource(CanvasImageSource source, num sourceX, num sourceY, num sourceWidth, num sourceHeight, num destX, num destY, num destWidth, num destHeight) #

Draws an image from a CanvasImageSource to an area of this canvas.

The image is a region of source that is sourceWidth wide and destHeight tall with top left corner at ( sourceX, sourceY). The image will be drawn to this context with its top left corner at the point ( destX, destY) and will be scaled to be destWidth wide and destHeight tall.

If the image is larger than canvas will allow, the image will be clipped to fit the available space.

VideoElement video = document.query('video');
video.width = 100;
video.height = 100;
// Take the middle 20x20 pixels from the video and stretch them.
ctx.drawImageScaledFromSource(video, 40, 40, 20, 20, 50, 50, 100, 100);

// Draw the top 100x20 pixels from the otherCanvas to this one.
CanvasElement otherCanvas = document.query('canvas');
ctx.drawImageScaledFromSource(otherCanvas, 0, 0, 100, 20, 0, 0, 100, 20);

See also:

@DomName('CanvasRenderingContext2D.drawImage')
@JSName('drawImage')
void drawImageScaledFromSource(CanvasImageSource source,
   num sourceX, num sourceY, num sourceWidth, num sourceHeight,
   num destX, num destY, num destWidth, num destHeight) native;

void drawImageToRect(CanvasImageSource source, Rect destRect, {Rect sourceRect}) #

Draws an image from a CanvasImageSource to an area of this canvas.

The image will be drawn to an area of this canvas defined by destRect. sourceRect defines the region of the source image that is drawn. If sourceRect is not provided, then the entire rectangular image from source will be drawn to this context.

If the image is larger than canvas will allow, the image will be clipped to fit the available space.

CanvasElement canvas = new CanvasElement(width: 600, height: 600);
CanvasRenderingContext2D ctx = canvas.context2D;
ImageElement img = document.query('img');
img.width = 100;
img.height = 100;

// Scale the image to 20x20.
ctx.drawImageToRect(img, new Rect(50, 50, 20, 20));

VideoElement video = document.query('video');
video.width = 100;
video.height = 100;
// Take the middle 20x20 pixels from the video and stretch them.
ctx.drawImageToRect(video, new Rect(50, 50, 100, 100),
    sourceRect: new Rect(40, 40, 20, 20));

// Draw the top 100x20 pixels from the otherCanvas.
CanvasElement otherCanvas = document.query('canvas');
ctx.drawImageToRect(otherCanvas, new Rect(0, 0, 100, 20),
    sourceRect: new Rect(0, 0, 100, 20));

See also:

@DomName('CanvasRenderingContext2D.drawImage')
void drawImageToRect(CanvasImageSource source, Rect destRect,
   {Rect sourceRect}) {
 if (sourceRect == null) {
   drawImageScaled(source,
       destRect.left,
       destRect.top,
       destRect.width,
       destRect.height);
 } else {
   drawImageScaledFromSource(source,
       sourceRect.left,
       sourceRect.top,
       sourceRect.width,
       sourceRect.height,
       destRect.left,
       destRect.top,
       destRect.width,
       destRect.height);
 }
}

void fill([String winding]) #

Fills the subpaths with the current fill style.
@DomName('CanvasRenderingContext2D.fill')
@DocsEditable
void fill([String winding]) native;

void fillRect(num x, num y, num width, num height) #

Draws a filled rectangle at (x, y) position whose size is determined by width and height.

Parameters
x
The x axis of the coordinate for the rectangle starting point.
y
The y axis of the coordinate for the rectangle starting point.
width
The rectangle's width.
height
The rectangle's height.
@DomName('CanvasRenderingContext2D.fillRect')
@DocsEditable
void fillRect(num x, num y, num width, num height) native;

void fillText(String text, num x, num y, [num maxWidth]) #

@DomName('CanvasRenderingContext2D.fillText')
@DocsEditable
void fillText(String text, num x, num y, [num maxWidth]) native;

Canvas2DContextAttributes getContextAttributes() #

@DomName('CanvasRenderingContext2D.getContextAttributes')
@DocsEditable
Canvas2DContextAttributes getContextAttributes() native;

ImageData getImageData(num sx, num sy, num sw, num sh) #

Returns an ImageData object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has a sw width and sh height.

Parameters
sx
The x axis of the coordinate for the rectangle startpoint from which the ImageData will be extracted.
sy
The x axis of the coordinate for the rectangle endpoint from which the ImageData will be extracted.
sw
The width of the rectangle from which the ImageData will be extracted.
sh
The height of the rectangle from which the ImageData will be extracted.
Return value

Returns an ImageData object containing the image data for the given rectangle of the canvas.

@DomName('CanvasRenderingContext2D.getImageData')
@DocsEditable
@Creates('ImageData|=Object')
ImageData getImageData(num sx, num sy, num sw, num sh) {
 return _convertNativeToDart_ImageData(_getImageData_1(sx, sy, sw, sh));
}

ImageData getImageDataHD(num sx, num sy, num sw, num sh) #

@DomName('CanvasRenderingContext2D.webkitGetImageDataHD')
@DocsEditable
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.SAFARI)
@Experimental
@Creates('ImageData|=Object')
ImageData getImageDataHD(num sx, num sy, num sw, num sh) {
 return _convertNativeToDart_ImageData(_getImageDataHD_1(sx, sy, sw, sh));
}

List<num> getLineDash() #

@DomName('CanvasRenderingContext2D.getLineDash')
@DocsEditable
List<num> getLineDash() native;

bool isPointInPath(num x, num y, [String winding]) #

Reports whether or not the specified point is contained in the current path.

Parameters
x
The X coordinate of the point to check.
y
The Y coordinate of the point to check.
Return value

true if the specified point is contained in the current path; otherwise false.

Compatibility notes
  • Prior to Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4) , this method incorrectly failed to multiply the specified point's coordinates by the current transformation matrix before comparing it to the path. Now this method works correctly even if the context is rotated, scaled, or otherwise transformed.
@DomName('CanvasRenderingContext2D.isPointInPath')
@DocsEditable
bool isPointInPath(num x, num y, [String winding]) native;

bool isPointInStroke(num x, num y) #

@DomName('CanvasRenderingContext2D.isPointInStroke')
@DocsEditable
bool isPointInStroke(num x, num y) native;

void lineTo(num x, num y) #

Connects the last point in the subpath to the x, y coordinates with a straight line.

Parameters
x
The x axis of the coordinate for the end of the line.
y
The y axis of the coordinate for the end of the line.
@DomName('CanvasRenderingContext2D.lineTo')
@DocsEditable
void lineTo(num x, num y) native;

TextMetrics measureText(String text) #

@DomName('CanvasRenderingContext2D.measureText')
@DocsEditable
TextMetrics measureText(String text) native;

void moveTo(num x, num y) #

Moves the starting point of a new subpath to the (x, y) coordinates.

Parameters
x
The x axis of the point.
y
The y axis of the point.
@DomName('CanvasRenderingContext2D.moveTo')
@DocsEditable
void moveTo(num x, num y) native;

dynamic noSuchMethod(Invocation invocation) #

inherited from Interceptor

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 in an Invocation. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

docs inherited from Object
dynamic noSuchMethod(Invocation invocation) {
 throw new NoSuchMethodError(
     this,
     _symbolToString(invocation.memberName),
     invocation.positionalArguments,
     _symbolMapToStringMap(invocation.namedArguments));
}

void putImageData(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) #

Compatibility notes
  • Starting in Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0) , non-finite values to any of these parameters causes the call to putImageData() to be silently ignored, rather than throwing an exception.
@DomName('CanvasRenderingContext2D.putImageData')
@DocsEditable
void putImageData(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) {
 if (!?dirtyX && !?dirtyY && !?dirtyWidth && !?dirtyHeight) {
   var imagedata_1 = _convertDartToNative_ImageData(imagedata);
   _putImageData_1(imagedata_1, dx, dy);
   return;
 }
 var imagedata_2 = _convertDartToNative_ImageData(imagedata);
 _putImageData_2(imagedata_2, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
 return;
 throw new ArgumentError("Incorrect number or type of arguments");
}

void putImageDataHD(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) #

@DomName('CanvasRenderingContext2D.webkitPutImageDataHD')
@DocsEditable
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.SAFARI)
@Experimental
void putImageDataHD(ImageData imagedata, num dx, num dy, [num dirtyX, num dirtyY, num dirtyWidth, num dirtyHeight]) {
 if (!?dirtyX && !?dirtyY && !?dirtyWidth && !?dirtyHeight) {
   var imagedata_1 = _convertDartToNative_ImageData(imagedata);
   _putImageDataHD_1(imagedata_1, dx, dy);
   return;
 }
 var imagedata_2 = _convertDartToNative_ImageData(imagedata);
 _putImageDataHD_2(imagedata_2, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
 return;
 throw new ArgumentError("Incorrect number or type of arguments");
}

void quadraticCurveTo(num cpx, num cpy, num x, num y) #

@DomName('CanvasRenderingContext2D.quadraticCurveTo')
@DocsEditable
void quadraticCurveTo(num cpx, num cpy, num x, num y) native;

void rect(num x, num y, num width, num height) #

@DomName('CanvasRenderingContext2D.rect')
@DocsEditable
void rect(num x, num y, num width, num height) native;

void restore() #

Restores the drawing style state to the last element on the 'state stack' saved by save()
@DomName('CanvasRenderingContext2D.restore')
@DocsEditable
void restore() native;

void rotate(num angle) #

@DomName('CanvasRenderingContext2D.rotate')
@DocsEditable
void rotate(num angle) native;

void save() #

Saves the current drawing style state using a stack so you can revert any change you make to it using restore().
@DomName('CanvasRenderingContext2D.save')
@DocsEditable
void save() native;

void scale(num sx, num sy) #

@DomName('CanvasRenderingContext2D.scale')
@DocsEditable
void scale(num sx, num sy) native;

void setFillColorHsl(int h, num s, num l, [num a = 1]) #

Sets the color used inside shapes. h is in degrees, 0-360. s, l are in percent, 0-100. a is 0-1.

void setFillColorHsl(int h, num s, num l, [num a = 1]) {
 this.fillStyle = 'hsla($h, $s%, $l%, $a)';
}

void setFillColorRgb(int r, int g, int b, [num a = 1]) #

Sets the color used inside shapes. r, g, b are 0-255, a is 0-1.

void setFillColorRgb(int r, int g, int b, [num a = 1]) {
 this.fillStyle = 'rgba($r, $g, $b, $a)';
}

void setLineDash(List<num> dash) #

@DomName('CanvasRenderingContext2D.setLineDash')
@DocsEditable
void setLineDash(List<num> dash) native;

void setStrokeColorHsl(int h, num s, num l, [num a = 1]) #

Sets the color used for stroking shapes. h is in degrees, 0-360. s, l are in percent, 0-100. a is 0-1.

void setStrokeColorHsl(int h, num s, num l, [num a = 1]) {
 this.strokeStyle = 'hsla($h, $s%, $l%, $a)';
}

void setStrokeColorRgb(int r, int g, int b, [num a = 1]) #

Sets the color used for stroking shapes. r, g, b are 0-255, a is 0-1.

void setStrokeColorRgb(int r, int g, int b, [num a = 1]) {
 this.strokeStyle = 'rgba($r, $g, $b, $a)';
}

void setTransform(num m11, num m12, num m21, num m22, num dx, num dy) #

@DomName('CanvasRenderingContext2D.setTransform')
@DocsEditable
void setTransform(num m11, num m12, num m21, num m22, num dx, num dy) native;

void stroke() #

Strokes the subpaths with the current stroke style.
@DomName('CanvasRenderingContext2D.stroke')
@DocsEditable
void stroke() native;

void strokeRect(num x, num y, num width, num height, [num lineWidth]) #

Paints a rectangle which it starting point is at (x, y) and has a w width and a h height onto the canvas, using the current stroke style.

Parameters
x
The x axis for the starting point of the rectangle.
y
The y axis for the starting point of the rectangle.
w
The rectangle's width.
h
The rectangle's height.
@DomName('CanvasRenderingContext2D.strokeRect')
@DocsEditable
void strokeRect(num x, num y, num width, num height, [num lineWidth]) native;

void strokeText(String text, num x, num y, [num maxWidth]) #

@DomName('CanvasRenderingContext2D.strokeText')
@DocsEditable
void strokeText(String text, num x, num y, [num maxWidth]) native;

String toString() #

inherited from Interceptor

Returns a string representation of this object.

docs inherited from Object
String toString() => Primitives.objectToString(this);

void transform(num m11, num m12, num m21, num m22, num dx, num dy) #

@DomName('CanvasRenderingContext2D.transform')
@DocsEditable
void transform(num m11, num m12, num m21, num m22, num dx, num dy) native;

void translate(num tx, num ty) #

Moves the origin point of the context to (x, y).

Parameters
x
The x axis for the point to be considered as the origin.
y
The x axis for the point to be considered as the origin.
@DomName('CanvasRenderingContext2D.translate')
@DocsEditable
void translate(num tx, num ty) native;

void $dom_arc(num x, num y, num radius, num startAngle, num endAngle, bool anticlockwise) #

Adds an arc to the path which it center is at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).

Parameters
x
The x axis of the coordinate for the arc's center
y
The y axis of the coordinate for the arc's center.
radius
The arc's radius
startAngle
The starting point, measured from the x axis , from which it will be drawed expressed as radians.
endAngle
The end arc's angle to which it will be drawed expressed as radians.
anticlockwise Optional from Gecko 2.0
When true draws the arc anticlockwise, otherwise in a clockwise direction.
@JSName('arc')
@DomName('CanvasRenderingContext2D.arc')
@DocsEditable
void $dom_arc(num x, num y, num radius, num startAngle, num endAngle, bool anticlockwise) native;

This page includes content from the Mozilla Foundation that is graciously licensed under a Creative Commons: Attribution-Sharealike license. Mozilla has no other association with Dart or dartlang.org. We encourage you to improve the web by contributing to The Mozilla Developer Network.