Context
Used for drawing rectangles, text, images and other objects onto the canvas element. It provides the 2D rendering context for a drawing surface.
It has the same API as CanvasRenderingContext2D from the HTML5 canvas spec
Static Method Summary
Static Public Methods | ||
public static |
colorStringToUint32(str: string): number Color String To Unint32 |
Constructor Summary
Public Constructor | ||
public |
constructor(bitmap: Bitmap) Creates a new pure image Context |
Member Summary
Public Members | ||
public |
An instance of the Bitmap class. |
|
public |
The color or style to use inside shapes. |
|
public |
The current text style being used when drawing text. |
|
public |
The alpha value that is applied to shapes and images before they are drawn onto the canvas. |
|
public |
|
|
public |
The thickness of lines in space units. |
|
public |
|
|
public |
Set the starting co-ordinates for the path starting point |
|
public |
The color or style to use for the lines around shapes. |
|
public |
|
|
public |
|
|
public |
transform: Transform |
Method Summary
Public Methods | ||
public |
Adds an arc to the path which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise). |
|
public |
beginPath(): void Starts a new path by emptying the list of sub-paths. |
|
public |
Adds a cubic Bézier curve to the path. |
|
public |
Sets all pixels in the rectangle defined by starting point |
|
public |
clip(): void Turns the path currently being built into the current clipping path. |
|
public |
closePath(): void Causes the point of the pen to move back to the start of the current sub-path. |
|
public |
createLinearGradient(x0: *, y0: *, x1: *, y1: *): * |
|
public |
createRadialGradient(x0: *, y0: *): * |
|
public |
drawImage(bitmap: Bitmap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void Provides different ways to draw an image onto the canvas. |
|
public |
Draw a line using the correct anti-aliased, or non-anti-aliased line drawing function based on the value of imageSmoothingEnabled |
|
public |
drawLine_aa(line: Line) Draw Line Anti-aliased |
|
public |
drawLine_noaa(line: Line): void Draw a line without anti-aliasing using Bresenham's algorithm |
|
public |
fill(): void Fills the current or given path with the current fill style. |
|
public |
Set the background colour of a single pixel denoted by the |
|
public |
Draws a filled rectangle whose starting point is at the coordinates |
|
public |
Draws a text string at the specified coordinates, filling the string's characters with the current foreground color |
|
public |
fill_aa(): void Fill Anti-aliased |
|
public |
fill_noaa(): void Fill No Anti-aliased |
|
public |
Connects the last point in the sub-path to the x, y coordinates with a straight line (but does not actually draw it). |
|
public |
Moves the starting point of a new sub-path to the (x, y) coordinates. |
|
public |
pixelInsideClip(x: number, y: number): void Pixel Inside Clip |
|
public |
quadraticCurveTo(cp1x: number, cp1y: number, x: number, y: number): void Adds a quadratic Bézier curve to the path. |
|
public |
restore(): void Restores the most recently saved canvas state by popping the top entry in the drawing state stack. |
|
public |
Add a rotation to the transformation matrix. |
|
public |
save(): void Saves the entire state of the canvas by pushing the current state onto a stack |
|
public |
Adds a scaling transformation to the canvas units by |
|
public |
stroke(): void Strokes the current or given path with the current stroke style |
|
public |
strokePixel(x: number, y: number): void Paints a pixel which has an x axis position of |
|
public |
strokeRect(x: number, y: number, w: number, h: number): void Paints a rectangle which has a starting point at |
|
public |
strokeText(text: string, x: number, y: number): void Draws the outlines of the characters of a specified text string at the given (x, y) position. |
|
public |
Adds a translation transformation by moving the canvas and its origin |
Static Public Methods
Public Constructors
Public Members
public bitmap: Bitmap source
An instance of the Bitmap class. Used for direct pixel manipulation(for example setting pixel colours)
public fillStyle: string source
The color or style to use inside shapes. The default is #000 (black).
public font: object source
The current text style being used when drawing text. This string uses the same syntax as the CSS font specifier
public globalAlpha: Boolean source
The alpha value that is applied to shapes and images before they are drawn onto the canvas. The value is in the range from 0.0 (fully transparent) to 1.0 (fully opaque).
public lineWidth: number source
The thickness of lines in space units. When getting, it returns the current value (1.0 by default). When setting, zero, negative, Infinity
and NaN
values are ignored; otherwise the current value is set to the new value.
public strokeStyle: string source
The color or style to use for the lines around shapes. The default is #000 (black).
public transform: Transform source
Public Methods
public arc(x: number, y: number, rad: number, start: number, end: number, anticlockwise: boolean): void source
Adds an arc to the path which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).
Params:
Name | Type | Attribute | Description |
x | number | The x coordinate of the arc's center |
|
y | number | The y coordinate of the arc's center |
|
rad | number | The arc's radius |
|
start | number | The angle at which the arc starts, measured clockwise from the positive x axis and expressed in radians |
|
end | number | The angle at which the arc ends, measured clockwise from the positive x axis and expressed in radians |
|
anticlockwise | boolean | A boolean which, if true, causes the arc to be drawn anticlockwise between the two angles. |
Return:
void |
public beginPath(): void source
Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.
Return:
void |
public bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void source
Adds a cubic Bézier curve to the path. It requires three points. The first two points are control points and the third one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the Bézier curve.
Params:
Name | Type | Attribute | Description |
cp1x | number | The x axis of the coordinate for the first control point. |
|
cp1y | number | The y axis of the coordinate for first control point. |
|
cp2x | number | The x axis of the coordinate for the second control point. |
|
cp2y | number | The y axis of the coordinate for the second control point. |
|
x | number | The x axis of the coordinate for the end point. |
|
y | number | The y axis of the coordinate for the end point. |
Return:
void |
public clearRect(x: number, y: number, w: number, h: number): void source
Sets all pixels in the rectangle defined by starting point (x, y)
and size (width, height)
to transparent black, erasing any previously drawn content.
Return:
void |
public clip(): void source
Turns the path currently being built into the current clipping path.
Return:
void |
public closePath(): void source
Causes the point of the pen to move back to the start of the current sub-path. It tries to add a straight line (but does not actually draw it) from the current point to the start. If the shape has already been closed or has only one point, this function does nothing.
Return:
void |
public createLinearGradient(x0: *, y0: *, x1: *, y1: *): * source
Params:
Name | Type | Attribute | Description |
x0 | * | ||
y0 | * | ||
x1 | * | ||
y1 | * |
Return:
* |
public createRadialGradient(x0: *, y0: *): * source
Params:
Name | Type | Attribute | Description |
x0 | * | ||
y0 | * |
Return:
* |
public drawImage(bitmap: Bitmap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void source
Provides different ways to draw an image onto the canvas.
Params:
Name | Type | Attribute | Description |
bitmap | Bitmap | An instance of the Bitmap class to use for drawing |
|
sx | number | The X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. |
|
sy | number | The Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. |
|
sw | number | The width of the sub-rectangle of the source Bitmap to draw into the destination context. If not specified, the entire rectangle from the coordinates specified by |
|
sh | number | The height of the sub-rectangle of the source Bitmap to draw into the destination context. |
|
dx | number | The X coordinate in the destination canvas at which to place the top-left corner of the source Bitmap |
|
dy | number | The Y coordinate in the destination canvas at which to place the top-left corner of the source Bitmap |
|
dw | number | The width to draw the Bitmap in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in width when drawn |
|
dh | number | The height to draw the Bitmap in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in height when drawn |
Return:
void |
public drawLine(line: Line): void source
Draw a line using the correct anti-aliased, or non-anti-aliased line drawing function based on the value of imageSmoothingEnabled
Params:
Name | Type | Attribute | Description |
line | Line | A set of co-ordinates representing the start and end of the line. You can also pass a plain js object if you wish |
Return:
void |
Example:
//All of the following are valid:
ctx.drawLine({start: {x: 20, y:42}, end: {x: 20, y:90}})
ctx.drawLine(new Line(new Point(20, 42), new Point(20, 90)))
ctx.drawLine(new Line(20, 42, 20, 90))
public drawLine_aa(line: Line) source
Draw Line Anti-aliased
Draw anti-aliased line using Bresenham's algorithm
Params:
Name | Type | Attribute | Description |
line | Line | A set of co-ordinates representing the start and end of the line. You can also pass a plain js object if you wish |
Example:
//All of the following are valid:
ctx.drawLine({start: {x: 20, y:42}, end: {x: 20, y:90}})
ctx.drawLine(new Line(new Point(20, 42), new Point(20, 90)))
ctx.drawLine(new Line(20, 42, 20, 90))
public drawLine_noaa(line: Line): void source
Draw a line without anti-aliasing using Bresenham's algorithm
Params:
Name | Type | Attribute | Description |
line | Line | A set of co-ordinates representing the start and end of the line. You can also pass a plain js object if you wish |
Return:
void |
Example:
//All of the following are valid:
ctx.drawLine({start: {x: 20, y:42}, end: {x: 20, y:90}})
ctx.drawLine(new Line(new Point(20, 42), new Point(20, 90)))
ctx.drawLine(new Line(20, 42, 20, 90))
public fill(): void source
Fills the current or given path with the current fill style. Uses fill_aa and fill_noaa depending on the the value of imageSmoothingEnabled
Return:
void |
public fillPixel(x: number, y: number): void source
Set the background colour of a single pixel denoted by the x
and y
co-ordinates
Return:
void |
public fillRect(x: number, y: number, w: number, h: number): void source
Draws a filled rectangle whose starting point is at the coordinates (x, y)
with the specified width and height and whose style is determined by the fillStyle attribute.
Return:
void |
public fillText(text: string, x: number, y: number): void source
Draws a text string at the specified coordinates, filling the string's characters with the current foreground color
Params:
Name | Type | Attribute | Description |
text | string | A string specifying the text string to render into the context. The text is rendered using the settings specified by font. |
|
x | number | The x -coordinate of the point at which to begin drawing the text, in pixels. |
|
y | number | The y-coordinate of the point at which to begin drawing the text, in pixels. |
Return:
void |
public lineTo(x: number, y: number): void source
Connects the last point in the sub-path to the x, y coordinates with a straight line (but does not actually draw it).
Return:
void |
public moveTo(x: number, y: number): void source
Moves the starting point of a new sub-path to the (x, y) coordinates.
Return:
void |
public pixelInsideClip(x: number, y: number): void source
Pixel Inside Clip
Even/odd rule. https://en.wikipedia.org/wiki/Point_in_polygon technically this is not correct as the default algorithm for html canvas is supposed to be the non-zero winding rule instead
Return:
void |
public quadraticCurveTo(cp1x: number, cp1y: number, x: number, y: number): void source
Adds a quadratic Bézier curve to the path. It requires two points. The first point is a control point and the second one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the quadratic Bézier curve.
Return:
void |
public restore(): void source
Restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing.
Return:
void |
public rotate(angle: number): void source
Add a rotation to the transformation matrix. The angle argument represents a clockwise rotation angle and is expressed in adians
Params:
Name | Type | Attribute | Description |
angle | number | Degrees of rotation (in radians) |
Return:
void |
public save(): void source
Saves the entire state of the canvas by pushing the current state onto a stack
Return:
void |
public scale(sx: number, sy: number): void source
Adds a scaling transformation to the canvas units by x
horizontally and by y
vertically
Return:
void |
public stroke(): void source
Strokes the current or given path with the current stroke style
Return:
void |
public strokePixel(x: number, y: number): void source
Paints a pixel which has an x axis position of x
and a y axis position of y
Return:
void |
public strokeRect(x: number, y: number, w: number, h: number): void source
Paints a rectangle which has a starting point at (x, y)
and has a w
width and an h
height onto the canvas, using the current stroke style.
Return:
void |
public strokeText(text: string, x: number, y: number): void source
Draws the outlines of the characters of a specified text string at the given (x, y) position.
Return:
void |