Canvas Drawing Module Reference

Table of contents


Introduction

The Canvas drawing module contains the ctxDraw object that cointain methods for drawing on a 2D Canvas drawing context.


ctxDraw

Syntax

import {ctxDraw} from 'common/option/ctxDraw.js';

The ctxDraw object contains methods used to draw in a 2D Canvas drawing context.


ctxDraw methods

ctxDraw.arrow()

Syntax

ctxDraw.arrow(ctx, startX, startY, angle, length, width)

Draw an arrow from (startX, startY) at angle radians (from the right), length long, with an arrowhead width wide. The current line width and color will be used.

Parameters


ctxDraw.createPatternFromFn()

Syntax

ctxDraw.createPatternFromFn(width, height, fn)
ctxDraw.createPatternFromFn(width, height, fn, repeat)

Returns a pattern that is populated by fn. The method creates a new 2D drawing canvas of the required width and height, passes it to fn to draw the pattern in, and then calls the context's createPattern method with the new context as the first argument and repeat as the second.

Parameters

Return value

The pattern that was returned by the createPattern() method.


ctxDraw.dottedLineTo()

Syntax

ctxDraw.dottedLineTo(ctx, startX, startY, endX, endY)
ctxDraw.dottedLineTo(ctx, startX, startY, endX, endY, dot)
ctxDraw.dottedLineTo(ctx, startX, startY, endX, endY, dot, space)

Draw a dotted or dashed line from (startX, startY) to (endX, endY). If dot is specified, dots are dot px long. If dot is omitted, the current context lineWidth is used. If space is specified, dots have space px of space between them. If space is omitted, dots have dot px of space between them. The current line width and color are used to draw the dotted line.

Parameters


ctxDraw.imageCache()

Syntax

ctxDraw.imageCache(url)

Return a cached image associated with url. If there is no cached Image object associated with url, then a new Image object is created using the url, and the fetched result is saved.

Parameters


ctxDraw.json()

Syntax

ctxDraw.json(ctx, info)

Draw objects in ctx from the info array. Each array element is an object. An object may contain the following special properties:

Objects with other keys are assumed to be properties in the context, and those context properties are set from the key values. For example:

ctxDraw.json(ctx, [{lineWidth:3}, {line:[[10,10],[20,20]]}])

draws a 3 pixel wide line from (10,10) to (20,20).

Parameters


ctxDraw.text()

Syntax

ctxDraw.text(ctx, startX, startY, text, lineHeight)

Draw a text starting at (startX, startY) and going right and down. A \n in text signals a new line. lineHeight is the distance between lines. [c color]text[/c] in the string will draw that text in the specified color.

Parameters


ctxDraw.textWidth()

Syntax

ctxDraw.textWidth(text, fontProp)

Return the rendered width of text. fontProp is a CSS string speicifying the font and font attributes to be used.

Parameters

Return value

The rendered width of the text in pixels