CanvasForm

CanvasForm is an implementation of abstract class VisualForm. It provide methods to express Pts on CanvasSpace. You may extend CanvasForm to implement your own expressions for CanvasSpace.

Class extends VisualForm in src/Canvas.ts

Constructor

new CanvasForm ( space )

Create a new CanvasForm. You may also use CanvasSpace.getForm() to get the default form.

parameters
space: CanvasSpace | CanvasRenderingContext2D

an instance of CanvasSpace

returns
An instance of CanvasForm
source

Accessors

GET ctx : PtsCanvasRenderingContext2D

Get the rendering context of canvas to perform other canvas functions.

GET space : CanvasSpace

get the CanvasSpace instance that this form is associated with

GET currentFont : Font

Get the current font in use in this form.

Inherited from VisualForm.currentFont in src/Form.ts

GET SET filled : boolean

Check whether this form currently has fill style.

Inherited from VisualForm.filled in src/Form.ts

GET ready : boolean

get whether the Form has received the Space's rendering context.

Inherited from Form.ready in src/Form.ts

GET SET stroked : boolean

Check whether this form currently has stroke style.

Inherited from VisualForm.stroked in src/Form.ts

Methods

alignText ( alignment, baseline )

Set text alignment and baseline (eg, vertical-align).

parameters
alignment: CanvasTextAlign (default value: "left")

HTML canvas' textAlign option: "left", "right", "center", "start", or "end"

baseline: CanvasTextBaseline (default value: "alphabetic")

HTML canvas' textBaseline option: "top", "hanging", "middle", "alphabetic", "ideographic", "bottom". For convenience, you can also use "center" (same as "middle"), and "baseline" (same as "alphabetic")

returns
this

source

alpha ( a )

Set current alpha value.

parameters
a: number

alpha value between 0 and 1

returns
this

example

form.alpha(0.6)

source
Overrides VisualForm.alpha in src/Canvas.ts

applyFillStroke ( filled, stroked, strokeWidth )

A convenient function to apply fill and/or stroke after custom drawings using canvas context (eg, form.ctx.ellipse(...)). You don't need to call this function if you're using Pts' drawing functions like form.point or form.rect

parameters
filled: boolean | string (default value: true)

apply fill when set to true

stroked: boolean | string (default value: true)

apply stroke when set to true

strokeWidth: number (default value: 1)

optionally set a stroke width

returns
this

example

form.ctx.beginPath(); form.ctx.ellipse(...); form.applyFillStroke();

source

arc ( pt, radius, startAngle, endAngle, cc )

Draw an arc.

parameters
pt: PtLike

center position

radius: number

radius of the arc circle

startAngle: number

start angle of the arc

endAngle: number

end angle of the arc

cc: boolean

an optional boolean value to specify if it should be drawn clockwise (false) or counter-clockwise (true). Default is clockwise.

returns
this

source
Overrides VisualForm.arc in src/Canvas.ts

STATIC arc ( ctx, pt, radius, startAngle, endAngle, cc )

A static function to draw an arc.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pt: PtLike

center position

radius: number

radius of the arc circle

startAngle: number

start angle of the arc

endAngle: number

end angle of the arc

cc: boolean

an optional boolean value to specify if it should be drawn clockwise (false) or counter-clockwise (true). Default is clockwise.

returns
void

source

circle ( pts )

Draw a circle. See also Circle.fromCenter

parameters
pts: PtLikeIterable

usually a Group or an Iterable with 2 Pt, but it can also take an array of two numeric arrays [ [position], [size] ]

returns
this

source
Overrides VisualForm.circle in src/Canvas.ts

STATIC circle ( ctx, pt, radius )

A static function to draw a circle.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pt: PtLike

center position of the circle

radius: number (default value: 10)

radius of the circle

returns
void

source

clip ( )

Create a clipping mask from the current path. See MDN documentation for details.

returns
this

source

composite ( mode )

Set composite operation (also known as blend mode). You can also call this function without parameters to get back to default 'source-over' mode. See MDN documentation for the full list of operations you can use.

parameters
mode: GlobalCompositeOperation (default value: "source-over")

a composite operation such as 'lighten', 'multiply', 'overlay', and 'color-burn'.

returns
this

source

dash ( segments, offset )

Activate dashed stroke and set dash style. You can customize the segments and offset.

parameters
segments: PtLike | boolean (default value: true)

Dash segments. Defaults to true which corresponds to [5, 5]. Pass false to deactivate dashes. (See canvas documentation)

offset: number (default value: 0)

Dash offset. Defaults to 0. (See canvas documentation

returns
this

example

form.dash(), form.dash([5, 10]), form.dash([5, 5], 5), form.dash(false)

source

ellipse ( pt, radius, rotation, startAngle, endAngle, cc )

Draw an ellipse.

parameters
pt: PtLike

center position

radius: PtLike

radius [x, y] of the ellipse

rotation: number (default value: 0)

rotation of the ellipse in radian. Default is 0.

startAngle: number (default value: 0)

start angle of the ellipse. Default is 0.

endAngle: number (default value: Const.two_pi)

end angle of the ellipse. Default is 2 PI.

cc: boolean (default value: false)

an optional boolean value to specify if it should be drawn clockwise (false) or counter-clockwise (true). Default is clockwise.

returns
this

source

STATIC ellipse ( ctx, pt, radius, rotation, startAngle, endAngle, cc )

A static function to draw an ellipse.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pt: PtLike

center position

radius: PtLike

radius [x, y] of the ellipse

rotation: number (default value: 0)

rotation of the ellipse in radian. Default is 0.

startAngle: number (default value: 0)

start angle of the ellipse. Default is 0.

endAngle: number (default value: Const.two_pi)

end angle of the ellipse. Default is 2 PI.

cc: boolean (default value: false)

an optional boolean value to specify if it should be drawn clockwise (false) or counter-clockwise (true). Default is clockwise.

returns
void

source

fill ( c )

Set current fill style. Provide a valid color string such as "#FFF" or "rgba(255,0,100,0.5)" or false to specify no fill color.

parameters
c: string | boolean | CanvasGradient | CanvasPattern

fill color which can be as color, gradient, or pattern. (See canvas documentation)

returns
this

example

form.fill("#F90"), form.fill("rgba(0,0,0,.5"), form.fill(false)

source
Overrides VisualForm.fill in src/Canvas.ts

font ( sizeOrFont, weight, style, lineHeight, family )

Set the current font.

parameters
sizeOrFont: number | Font

either a number to specify font-size, or a Font object to specify all font properties

weight: string

Optional font-weight string such as "bold"

style: string

Optional font-style string such as "italic"

lineHeight: number

Optional line-height number suchas 1.5

family: string

Optional font-family such as "Helvetica, sans-serif"

returns
this

example

form.font( myFont ), form.font(14, "bold")

source
Overrides VisualForm.font in src/Canvas.ts

fontWidthEstimate ( estimate )

Set whether to use html canvas' measureText function, or a faster but less accurate heuristic function.

parameters
estimate: boolean (default value: true)

true to use heuristic function, or false to use ctx.measureText

returns
this

source

getTextWidth ( c )

Get the width of this text. It will return an actual measurement or an estimate based on fontWidthEstimate setting. Default is an actual measurement using canvas context's measureText.

parameters
c: string

a string of text contents

returns
number

source

gradient ( stops )

This function takes an array of gradient colors, and returns a function to define the areas of the gradient fill. See demo code in CanvasForm.gradient.

parameters
stops: | string[]

an array of gradient stops. This can be an array of colors ["#f00", "#0f0", ...] for evenly distributed gradient, or an array of [stop, color] like [[0.1, "#f00"], [0.7, "#0f0"]]

returns
Fn(area1:GroupLike, area2:GroupLike)

a function that takes 1 or 2 Group as parameters. Use a single Group to specify a rectangular area for linear gradient, or use 2 Groups to specify 2 Circles for radial gradient.

example

c1 = Circle.fromCenter(...); grad = form.gradient(["#f00", "#00f"]); form.fill( grad( c1, c2 ) ).circle( c1 )

source

image ( ptOrRect, img, orig )

Draw an image.

parameters
ptOrRect: PtLike | PtLikeIterable

a target area to place the image. Either a PtLike specifying a position, or a Group or an Iterable with 2 Pt (top-left position, bottom-right position) that specifies a bounding box. Default is (0,0) at top-left.

img: CanvasImageSource | Img

either an Img instance or an CanvasImageSource instance (eg the image from <img>, <video> or <canvas>)

orig: PtLikeIterable

optionally a Group or an Iterable with 2 Pt (top-left position, bottom-right position) that specifies a cropping box in the original target.

returns
this

source

STATIC image ( ctx, ptOrRect, img, orig )

A static function to draw an image.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

ptOrRect: PtLike | PtLikeIterable

a target area to place the image. Either a Pt or numeric array specifying a position, or a Group or an Iterable with 2 Pt (top-left, bottom-right) that specifies a bounding box for resizing. Default is (0,0) at top-left.

img: CanvasImageSource | Img

either an Img instance or an CanvasImageSource instance (eg the image from <img>, <video> or <canvas>)

orig: PtLikeIterable

optionally a Group or an Iterable with 2 Pt (top-left, bottom-right) that specifies a cropping box in the original target.

returns
void

source

imageData ( ptOrRect, img )

Draw ImageData on canvas using ImageData

parameters
ptOrRect: PtLike | PtLikeIterable

a target area to place the image. Either a Pt or numeric array specifying a position, or a Group or an Iterable with 2 Pt (top-left, bottom-right) that specifies a bounding box for resizing. Default is (0,0) at top-left.

img: ImageData

an ImageData object

returns
this

source

STATIC imageData ( ctx, ptOrRect, img )

A static function to draw ImageData on canvas

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

ptOrRect: PtLike | PtLikeIterable

a target area to place the image. Either a Pt or numeric array specifying a position, or a Group or an Iterable with 2 Pt (top-left, bottom-right) that specifies a bounding box for resizing. Default is (0,0) at top-left.

img: ImageData

an ImageData object

returns
void

source

line ( pts )

Draw a line or polyline.

parameters
pts: PtLikeIterable

a Group or an Iterable representing a line

returns
this

source
Overrides VisualForm.line in src/Canvas.ts

STATIC line ( ctx, pts )

A static function to draw a line or polyline.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pts: PtLikeIterable

a Group or an Iterable representing a line

returns
void

source

log ( txt )

A convenient way to draw some text on canvas for logging or debugging. It'll be draw on the top-left of the canvas as an overlay.

parameters
txt: any

text

returns
this

source

paragraphBox ( box, txt, lineHeight, verticalAlign, crop )

Fit multi-line text in a rectangular box. Note that this will also set canvas context's textBaseline to "top".

parameters
box: PtLikeIterable

a Group or an Iterable with 2 Pt that represents a bounding box

txt: string

string of text

lineHeight: number (default value: 1.2)

line height as a ratio of font size. Default is 1.2.

verticalAlign: string (default value: "top")

"top", "middle", or "bottom" to specify vertical alignment inside the box

crop: boolean (default value: true)

a boolean to specify whether to crop text when overflowing

returns
this

source

point ( p, radius, shape )

Draws a point.

parameters
p: PtLike

a Pt object

radius: number (default value: 5)

radius of the point. Default is 5.

shape: string (default value: "square")

The shape of the point. Defaults to "square", but it can be "circle" or a custom shape function in your own implementation.

returns
this

example

form.point( p ), form.point( p, 10, "circle" )

source
Overrides VisualForm.point in src/Canvas.ts

STATIC point ( ctx, p, radius, shape )

A static function to draw a point.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

p: PtLike

a Pt object

radius: number (default value: 5)

radius of the point. Default is 5.

shape: string (default value: "square")

The shape of the point. Defaults to "square", but it can be "circle" or a custom shape function in your own implementation.

returns
void

example

form.point( p ), form.point( p, 10, "circle" )

source

polygon ( pts )

Draw a polygon.

parameters
pts: PtLikeIterable

a Group or an Iterable representingg a polygon

returns
this

source
Overrides VisualForm.polygon in src/Canvas.ts

STATIC polygon ( ctx, pts )

A static function to draw a polygon.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pts: PtLikeIterable

a Group or an Iterable representing a polygon

returns
void

source

rect ( pts )

Draw a rectangle.

parameters
pts: PtLikeIterable

a Group or an Iterable with 2 Pt specifying the top-left and bottom-right positions.

returns
this

source
Overrides VisualForm.rect in src/Canvas.ts

STATIC rect ( ctx, pts )

A static function to draw a rectangle.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pts: PtLikeIterable

a Group or an Iterable with 2 Pt specifying the top-left and bottom-right positions.

returns
void

source

renderOffscreen ( offset )

Render the offscreen canvas's content on the visible canvas

parameters
offset: PtLike (default value: [0,0])

Optional offset on the top-left position when drawing on the visible canvas

returns
void

source

reset ( )

Reset the rendering context's common styles to this form's styles. This supports using multiple forms on the same canvas context.

returns
this

source
Overrides VisualForm.reset in src/Canvas.ts

square ( pt, halfsize )

Draw a square, given a center and its half-size.

parameters
pt: PtLike

center Pt

halfsize: number

half-size

returns
this

source

STATIC square ( ctx, pt, halfsize )

A static function to draw a square.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pt: PtLike

center position of the square

halfsize: number

half size of the square

returns
void

source

stroke ( c, width, linejoin, linecap )

Set current stroke style. Provide a valid color string or false to specify no stroke color.

parameters
c: string | boolean | CanvasGradient | CanvasPattern

stroke color which can be as color, gradient, or pattern. (See canvas documentation)

width: number

Optional value (can be floating point) to set line width

linejoin: CanvasLineJoin

Optional string to set line joint style. Can be "miter", "bevel", or "round".

linecap: CanvasLineCap

Optional string to set line cap style. Can be "butt", "round", or "square".

returns
this

example

form.stroke("#F90"), form.stroke("rgba(0,0,0,.5"), form.stroke(false), form.stroke("#000", 0.5, 'round', 'square')

source
Overrides VisualForm.stroke in src/Canvas.ts

text ( pt, txt, maxWidth )

Draw text on canvas.

parameters
pt: PtLike

txt: string

maxWidth: number

returns
this

source
Overrides VisualForm.text in src/Canvas.ts

STATIC text ( ctx, pt, txt, maxWidth )

A static function to draw text.

parameters
ctx: CanvasRenderingContext2D

canvas rendering context

pt: PtLike

txt: string

maxWidth: number

returns
void

source

textBox ( box, txt, verticalAlign, tail, overrideBaseline )

Fit a single-line text in a rectangular box.

parameters
box: PtIterable

a rectangle box defined by a Group or an Iterable

txt: string

string of text

verticalAlign: string (default value: "middle")

"top", "middle", or "bottom" to specify vertical alignment inside the box

tail: string (default value: "")

text to indicate overflow such as "...". Default is empty "".

overrideBaseline: boolean (default value: true)

If true, use the corresponding baseline as verticalAlign. If false, use the current canvas context's textBaseline setting. Default is true.

returns
this

source

useOffscreen ( off, clear )

Toggle whether to draw on offscreen canvas (if offscreen is set in CanvasSpace)

parameters
off: boolean (default value: true)

if true, draw on offscreen canvas instead of the visible canvas. Default is true

clear: boolean | string (default value: false)

optionally provide a valid color string to fill a bg color. see CanvasSpace's clearOffscreen function.

returns
this

source

circles ( groups )

Draw multiple circles at once.

parameters
groups: GroupLike[]

an array of Groups that defines multiple circles

returns
this

source
Inherited from VisualForm.circles in src/Form.ts

fillOnly ( c )

Set current fill style and remove stroke style. (not implemented here -- to be implemented in subclasses).

parameters
c: string | boolean

fill color as string or false to specify transparent.

returns
this

source
Inherited from VisualForm.fillOnly in src/Form.ts

lines ( groups )

Draw multiple lines at once.

parameters
groups: GroupLike[]

An array of Groups of Pts

returns
this

source
Inherited from VisualForm.lines in src/Form.ts

points ( pts, radius, shape )

Draw multiple points at once.

parameters
pts: GroupLike | number[][]

an array of Pt or an array of number arrays

radius: number

radius of the point. Default is 5.

shape: string

The shape of the point. Defaults to "square", but it can be "circle" or a custom shape function in your own implementation.

returns
this

source
Inherited from VisualForm.points in src/Form.ts

polygons ( groups )

Draw multiple polygons at once.

parameters
groups: GroupLike[]

An array of Groups of Pts

returns
this

source
Inherited from VisualForm.polygons in src/Form.ts

rects ( groups )

Draw multiple rectangles at once.

parameters
groups: GroupLike[]

An array of Groups of Pts

returns
this

source
Inherited from VisualForm.rects in src/Form.ts

squares ( groups )

Draw multiple squares at once.

parameters
groups: GroupLike[]

an array of Groups that defines multiple circles

returns
this

source
Inherited from VisualForm.squares in src/Form.ts

strokeOnly ( c, width, linejoin, linecap )

Set stroke style and remove fill style. (not implemented here -- to be implemented in subclasses).

parameters
c: string | boolean

stroke color as string or false to specify transparent.

width: number

Optional value (can be floating point) to set line width

linejoin: string

Optional string to set line joint style. Can be "miter", "bevel", or "round".

linecap: string

Optional string to set line cap style. Can be "butt", "round", or "square".

returns
this

source
Inherited from VisualForm.strokeOnly in src/Form.ts