Class: Canvas

Canvas(options)

new Canvas(options)

Class representing a canvas
Parameters:
Name Type Description
options Object Options for the canvas creation
Properties
Name Type Attributes Description
width Number <optional>
The width of the canvas
height Number <optional>
The height of the canvas
container HTMLElement <optional>
The parent element of the canvas
preset "fullscreen" | "small" <optional>
If preset is present, it will bypass all other options and initialize the canvas with the preset
Source:

Classes

Canvas

Members

canvas :HTMLCanvasElement

The canvas element object
Type:
  • HTMLCanvasElement
Source:

context :CanvasRenderingContext2D

Context of the canvas
Type:
  • CanvasRenderingContext2D
Source:

filters :FilterManager

Filters for the canvas
Type:
Source:

Methods

applyFilter(filter) → {void}

Applies a filter to the canvas
Parameters:
Name Type Description
filter Filter Filter to be applied
Source:
Returns:
Type
void

clear() → {void}

Clears the canvas
Source:
Returns:
Type
void

clearFilters() → {void}

Clears all filters
Source:
Returns:
Type
void

clearRect(x, y, width, height) → {void}

Clears a rectangle
Parameters:
Name Type Description
x Number The x coordinate of the upper left corner of the rectangle
y Number The y coordinate of the upper left corner of the rectangle
width Number The width of the rectangle
height Number The height of the rectangle
Source:
Returns:
Type
void

createPath() → {Path}

Creates a path
Source:
Returns:
Type
Path

drawGrid(width)

Draws a grid
Parameters:
Name Type Default Description
width Number 50 How far apart should the lines of the grid be
Source:

drawImage(image, x, y) → {void}

Draws an image to the canvas
Parameters:
Name Type Description
image Image The image to be drawn
x Number The X coordinate of the upper left corner
y Number The Y coordinate of the upper left corner
Source:
Returns:
Type
void

drawLine(A, B) → {void}

Parameters:
Name Type Description
A Object Point A
Properties
Name Type Attributes Description
x Number <optional>
The x coordinate
y Number <optional>
The y coordinate
B Object Point B
Properties
Name Type Attributes Description
x Number <optional>
The x coordinate
y Number <optional>
The y coordinate
Source:
Returns:
Type
void

fillRect(x, y, width, height) → {void}

Fills a rectangle
Parameters:
Name Type Description
x Number The x coordinate of the upper left corner of the rectangle
y Number The y coordinate of the upper left corner of the rectangle
width Number The width of the rectangle
height Number The height of the rectangle
Source:
Returns:
Type
void

getAllFilters() → {FilterManager}

Returns all the filters
Source:
Returns:
Type
FilterManager

getImageData(rectangle) → {ImageData}

Returns ImageData object with pixels from the rectangle from the canvas
Parameters:
Name Type Description
rectangle Object Object represnting the rectangle
Properties
Name Type Description
x Number The x coordinate of the upper left corner of the rectangle
y Number The y coordinate of the upper left corner of the rectangle
width Number The width of the rectangle
height Number The height of the rectangle
Source:
Returns:
Type
ImageData

getLineDash() → {LineDashPattern}

Gets the line dashing pattern
Source:
Returns:
Type
LineDashPattern

getTransform() → {DOMMatrix2DInit}

Gets the transform matrix
Source:
Returns:
Type
DOMMatrix2DInit

load() → {void}

Loads the last saved canvas from the stack
Source:
Returns:
Type
void

rect(x, y, width, height) → {void}

Draws a rectangle outline
Parameters:
Name Type Description
x Number The x coordinate of the upper left corner of the rectangle
y Number The y coordinate of the upper left corner of the rectangle
width Number The width of the rectangle
height Number The height of the rectangle
Source:
Returns:
Type
void

removeFilter(filter)

Removes a filter
Parameters:
Name Type Description
filter Number | Filter
Source:

rotate(angle) → {void}

Rotates the canvas
Parameters:
Name Type Description
angle Number Angle of rotation in radians
Source:
Returns:
Type
void

save() → {void}

Saves the currect state of the canvas to stack. Can be restored using Canvas.load()
Source:
Returns:
Type
void

scale(x, y) → {void}

Scales the canvas
Parameters:
Name Type Default Description
x Number 1 X axis scale
y Number 1 Y axis scale
Source:
Returns:
Type
void

setDirection(direction) → {void}

Sets text direction
Parameters:
Name Type Default Description
direction "ltr" | "rtl" | "inherit" inherit Direction of the text
Source:
Returns:
Type
void

setFill(style) → {void}

Sets the fill style
Parameters:
Name Type Description
style String | CanvasGradient | CanvasPattern Syle of fill (color | gradient | image)
Source:
Returns:
Type
void

setFont(font) → {void}

Sets the font style
Parameters:
Name Type Description
font String String represnting the css style font
Source:
Returns:
Type
void

setGlobalAlpha(alpha) → {void}

Sets global alpha for all drawn objects
Parameters:
Name Type Description
alpha Number Number between 0 and 1 representing the global alpha value
Source:
Returns:
Type
void

setGlobalCompositeOperation(operation)

Sets the global composite operation. For more information about operations visit offical MDN documentation
Parameters:
Name Type Description
operation "source-over" | "source-in" | "source-out" | "source-atop" | "destination-over" | "destination-in" | "destination-out" | "destination-atop" | "lighter" | "copy" | "xor" | "multiply" | "screen" | "overlay" | "darken" | "lighten" | "color-dodge" | "color-burn" | "hard-light" | "soft-light" | "difference" | "exclusion" | "hue" | "saturation" | "color" | "luminosity" The operation
Source:

setImageSmooth(imageSmoothingMode) → {void}

Sets the image smoothing
Parameters:
Name Type Description
imageSmoothingMode "disabled" | "low" | "medium" | "high"
Source:
Returns:
Type
void

setLineCap(cap) → {void}

Sets the type of line endings
Parameters:
Name Type Default Description
cap "butt" | "round" | "square" butt Type of cap
Source:
Returns:
Type
void

setLineDash(lineWidth) → {void}

Sets the spacing of a line
Parameters:
Name Type Description
lineWidth Array.<Number>
Source:
Returns:
Type
void

setLineDashOffset(offset) → {void}

Sets the line dash offset
Parameters:
Name Type Description
offset Number The offset of the line dash
Source:
Returns:
Type
void

setLineJoin(join) → {void}

Sets the type of corners where two lines meet
Parameters:
Name Type Default Description
join "round" | "bevel" | "miter" miter Type of line join
Source:
Returns:
Type
void

setLineWidth(width) → {void}

Sets line width
Parameters:
Name Type Description
width Number
Source:
Returns:
Type
void

setMiterLimit(limit) → {void}

Sets the miter limit
Parameters:
Name Type Description
limit Number
Source:
Returns:
Type
void

setShadowBlur(level) → {void}

Sets the shadow blur. Default is 0, higher level is more blur
Parameters:
Name Type Description
level Number Blur level
Source:
Returns:
Type
void

setShadowColor(color) → {void}

Sets the shadow color
Parameters:
Name Type Description
color String CSS style color
Source:
Returns:
Type
void

setShadowOffset(xnullable, ynullable) → {void}

Sets shadow offset
Parameters:
Name Type Attributes Description
x Number <nullable>
X shadow offset
y Number <nullable>
Y shadow offset
Source:
Returns:
Type
void

setStroke(style) → {void}

Sets the stroke style
Parameters:
Name Type Description
style String | CanvasGradient | CanvasPattern Syle of stroke (color | gradient | image)
Source:
Returns:
Type
void

setTextAlign(textAlign) → {void}

Sets the text align
Parameters:
Name Type Default Description
textAlign "start" | "end" | "left" | "right" | "center" start Text align type
Source:
Returns:
Type
void

setTextBaseline(textBaseline) → {void}

Sets the test baseline
Parameters:
Name Type Default Description
textBaseline "top" | "hanging" | "middle" | "alphabetic" | "ideographic" | "bottom" alphabetic Text baseline style
Source:
Returns:
Type
void

setTransform(transform) → {void}

Sets the transform matrix
Parameters:
Name Type Description
transform DOMMatrix2DInit
Source:
Returns:
Type
void

text(text, x, y, maxWidthnullable) → {void}

Draws a text on the canvas
Parameters:
Name Type Attributes Description
text String Text to be drawn on canvas
x Number The x coordinate of the upper left corner of the text
y Number The y coordinate of the upper left corner of the text
maxWidth Number <nullable>
Max width of the text
Source:
Returns:
Type
void

textOutline(text, x, y, maxWidthnullable) → {void}

Draws a text outline on the canvas
Parameters:
Name Type Attributes Description
text String Text to be drawn on canvas
x Number The x coordinate of the upper left corner of the text
y Number The y coordinate of the upper left corner of the text
maxWidth Number <nullable>
Max width of the text
Source:
Returns:
Type
void

transform(optionsnullable) → {void}

Transforms the canvas according to the provided properties. If no options argument provided, this function will reset the transformation.
Parameters:
Name Type Attributes Description
options Object <nullable>
Properties
Name Type Attributes Description
x Number <optional>
<nullable>
The x coordinate of the [0, 0] point
y Number <optional>
<nullable>
The y coordinate of the [0, 0] point
xScale Number <optional>
<nullable>
The scaling factor of the x axis
yScale Number <optional>
<nullable>
The scaling factor of the y axis
rotation Number <optional>
<nullable>
The rotation of the plane in radians
Source:
Returns:
Type
void

translate(x, y) → {void}

Moves the zero point of the canvas
Parameters:
Name Type Default Description
x Number 0 X axis move
y Number 0 Y axis move
Source:
Returns:
Type
void