An HTML5 canvas drawing library, with 150+ useful methods, jQuery-like fluent interface, and cross-browser compatibility enhancements to make your life easier. Includes methods for drawing shapes not supported by the standard canvas API, loops, intuitive font control, and a host of helper methods for creating colors, calculating distances, converting angles, and much more.
Canvasimo (canvas in my opinion) started off as a simple concept - create wrappers for the standard canvas API to create a fluent interface, and allow access to canvas attributes with getters and setters.
This quickly evolved into a project that not only wrapped the existing canvas API, but added some additional methods (e.g. for creating rounded rectangles), and ensured that experimental features (such as reset transform and ellipse) worked in all browsers.
Along the way, I realized that a lot of the canvas attributes & methods were not very idiomatic, and so I came up with some more suitable method / attribute names.
For example, shape drawing is broken into 3 types: plotShape
, fillShape
, and strokeShape
. You'll find these 3 methods for almost every shape, e.g. plotRect
, fillRect
and strokeRect
. All the original methods are still available as aliases, e.g. rect
.
Similarly attributes regarding lines and strokes had a mixed set of names, so to simplify this, any style related attributes are referred to as stroke, and line is now a shape. For example, where you previously had strokeStyle
and lineWidth
, you now have stroke
and strokeWidth
, both of which are available through getters and setters; setStroke
, setStrokeWidth
. You can, however, still use their aliases; setStrokeStyle
, setLineWidth
.
Additionally, any methods that previously relied on setting the stroke or fill color before-hand, can now optionally have a color passed as their final argument, e.g. fillRect(0, 0, 10, 10, 'black')
, strokeLine(0, 0, 0, 20, 'red')
. And similarly, the fill and stroke methods can now be passed a color; fill('red')
, stroke('green')
, fillCanvas('blue')
.
The most recent change in Canvasimo 0.8.0
is that Canvasimo saves and retains the context state when resizing or clearing the canvas. This means that it's not always necessary to explicitly set values like font size, line width, and color in every draw loop - if you only ever use one font you can simply set this once when you're initializing Canvasimo and calls to clearCanvas
,setSize
, etc will not reset these values.
import Canvasimo from 'canvasimo'; const element = document.getElementById('example-basic-shapes'); if (!element) { throw new Error('Could not find canvas element for basic shapes example'); } const canvas = new Canvasimo(element as HTMLCanvasElement); const rect = canvas.getBoundingClientRect(); const randoms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(() => Math.random()); canvas .setDensity(window.devicePixelRatio >= 2 ? 2 : 1) .setSize(rect.width, rect.height); const draw = () => { const { width } = canvas.getBoundingClientRect(); const height = canvas.getHeight(); canvas .clearCanvas() .setSize(width, height) .fillCanvas('#EEEEEE') .save() .translate(width / 2, height / 2) .rotate(canvas.getRadiansFromDegrees(-90)) .setOpacity(0.25) .fillStar(0, 0, 50, 5, false, 'red') .restore() .strokeCircle(width * 0.25, height / 2, 30, false, 'black') .strokeBurst(width * 0.25, height / 2, 40, 50, 8, false, 'black') .strokeRoundedRect(width * 0.75 - 20, height / 2 - 20, 40, 40, 10, 'black') .repeat(10, (index) => { const y = canvas.map(index, 0, 10, 0, height) + height / 10 / 2; canvas .beginPath() .strokeLine(10, y, width - 10, y, 'green'); }) .tap(() => { const path = randoms.map((value, index) => ({ x: canvas.getFractionOfWidth(1 / (randoms.length - 1)) * index, y: canvas.map(value, 0, 1, 0, height), })); canvas .beginPath() .setStrokeWidth(2) .strokePath(path, 'blue'); }); }; draw(); window.addEventListener('resize', draw);
import Canvasimo from 'canvasimo'; const element = document.getElementById('example-multiline-text'); if (!element) { throw new Error('Could not find canvas element for multiline text example'); } const MARGIN = 10; const FONT_SIZE = 14; const canvas = new Canvasimo(element as HTMLCanvasElement); const rect = canvas.getBoundingClientRect(); canvas .setDensity(window.devicePixelRatio >= 2 ? 2 : 1) .setSize(rect.width, rect.height); const draw = () => { const { width } = canvas.getBoundingClientRect(); const height = canvas.getHeight(); const multilineTextArea = Math.min(width / 3 * 2, 400); const multilineTextOffset = (width - multilineTextArea) / 2; const multilineTextWidth = (multilineTextArea - MARGIN * 6) / 3; canvas .clearCanvas() .setSize(width, height) .fillCanvas('#FFFFFF') .setTextBaseline('top') .setFontFamily('arial') .setTextAlign('center') .setFontSize(FONT_SIZE - 2) .fillTextMultiline( 'Try resizing the window! :D', width / 2, FONT_SIZE * 4, width - MARGIN * 2, undefined, undefined, '#555555' ) .setTextAlign('start') .setFontSize(FONT_SIZE) .fillText( 'Regular text that does not have newlines or automatic wrapping', MARGIN, MARGIN, null, 'black' ) .fillTextMultiline( 'Text with newline after this...\n...so this is on a newline', MARGIN, FONT_SIZE * 2 ) .translate(multilineTextOffset, 0) .beginPath() .strokeLine(0, 0, 0, height, '#AAAAAA') .setTextAlign('left') .fillTextMultiline( 'normal\nText that automatically wraps but never breaks words', MARGIN, MARGIN + FONT_SIZE * 5, multilineTextWidth, 'normal' ) .translate(MARGIN * 2 + multilineTextWidth, 0) .beginPath() .strokeLine(0, 0, 0, height, '#AAAAAA') .setTextAlign('center') .fillTextMultiline( 'break-word\nText that automatically wraps and breaks words if necessary', MARGIN + multilineTextWidth / 2, MARGIN + FONT_SIZE * 5, multilineTextWidth, 'break-word' ) .translate(MARGIN * 2 + multilineTextWidth, 0) .beginPath() .strokeLine(0, 0, 0, height, '#AAAAAA') .setTextAlign('right') .fillTextMultiline( 'break-all\nText that automatically wraps and always breaks words', MARGIN + multilineTextWidth, MARGIN + FONT_SIZE * 5, multilineTextWidth, 'break-all' ) .translate(MARGIN * 2 + multilineTextWidth, 0) .strokeLine(0, 0, 0, height, '#AAAAAA'); }; draw(); window.addEventListener('resize', draw);
A collection of methods for getting and setting various properties of the canvas element.
Set the canvas dimensions.
canvas.setSize(size: Size) => Canvasimo;canvas.setSize(width: number, height: number) => Canvasimo;
type Size = { width: number; height: number; };
Get the canvas dimensions.
canvas.getSize() => Size;
type Size = { width: number; height: number; };
Get the canvas size & position on screen.
canvas.getBoundingClientRect() => ClientRect;
'A collection of methods for retrieving a canvas context or information about the context.
Get the standard canvas context (used for drawing).
canvas.getContext(type: string, contextAttributes?: CanvasContextAttributes) => CanvasContext | null;
type CanvasContextAttributes = CanvasRenderingContext2DSettings | WebGLContextAttributes;type CanvasContext = CanvasRenderingContext2D | WebGLRenderingContext;
Get canvas context used by Canvasimo (2d).
canvas.getCurrentContext() => CanvasRenderingContext2D;
Get the context type used by Canvasimo ('2d', 'webgl', etc).
canvas.getCurrentContextType() => "2d";
Get the context attributes used.
canvas.getContextAttributes() => CanvasContextAttributes | null;
type CanvasContextAttributes = CanvasRenderingContext2DSettings | WebGLContextAttributes;
A collection of methods for plotting or drawing solid shapes - those that create a new shape when invoked, and are self closing.
Plot a rectangle that can then have a fill or stroke applied to it.
canvas.plotRect(x: number, y: number, width: number, height: number) => Canvasimo;
Plot a rectangle and apply a stroke to it.
canvas.strokeRect(x: number, y: number, width: number, height: number, color?: string) => Canvasimo;
Plot a rectangle and apply a fill to it.
canvas.fillRect(x: number, y: number, width: number, height: number, color?: string) => Canvasimo;
Plot a rounded rectangle that can then have a fill or stroke applied to it.
canvas.plotRoundedRect(x: number, y: number, width: number, height: number, radius: number) => Canvasimo;
Plot a rounded rectangle and apply a stroke to it.
canvas.strokeRoundedRect(x: number, y: number, width: number, height: number, radius: number, color?: string) => Canvasimo;
Plot a rounded rectangle and apply a fill to it.
canvas.fillRoundedRect(x: number, y: number, width: number, height: number, radius: number, color?: string) => Canvasimo;
Plot a circle that can then have a stroke or fill applied to it.
canvas.plotCircle(x: number, y: number, radius: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a circle and apply a stroke to it.
canvas.strokeCircle(x: number, y: number, radius: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a circle and apply a fill to it.
canvas.fillCircle(x: number, y: number, radius: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a polygon that can then have a stroke or fill applied to it.
canvas.plotPoly(x: number, y: number, radius: number, sides: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a polygon and apply a stoke to it.
canvas.strokePoly(x: number, y: number, radius: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a polygon and apply a fill to it.
canvas.fillPoly(x: number, y: number, radius: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a star that can then have a stroke or fill applied to it.
canvas.plotStar(x: number, y: number, radius1: number, sides: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a star and apply a stoke to it.
canvas.strokeStar(x: number, y: number, radius1: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a star and apply a fill to it.
canvas.fillStar(x: number, y: number, radius1: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a burst that can then have a stroke or fill applied to it.
canvas.plotBurst(x: number, y: number, radius1: number, radius2: number, sides: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a burst and apply a stoke to it.
canvas.strokeBurst(x: number, y: number, radius1: number, radius2: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a burst and apply a fill to it.
canvas.fillBurst(x: number, y: number, radius1: number, radius2: number, sides: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot a single pixel that can then have a stroke or fill applied to it.
canvas.plotPixel(x: number, y: number) => Canvasimo;
Plot a single pixel and apply a stroke to it.
canvas.strokePixel(x: number, y: number, color?: string) => Canvasimo;
Plot a single pixel and apply a fill to it.
canvas.fillPixel(x: number, y: number, color?: string) => Canvasimo;
Plot a closed path that can then have a stroke or fill applied to it.
canvas.plotClosedPath(points: Points) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
Plot a closed path and apply a stroke to it.
canvas.strokeClosedPath(points: Points, color?: string) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
Plot a closed path and apply a fill to it.
canvas.fillClosedPath(points: Points, color?: string) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
A collection of methods for plotting or drawing open shapes - those that create a new shape when invoked, but are not self closing.
Plot a line that can then have a stroke or fill applied to it.
canvas.plotLine(x1: number, y1: number, x2: number, y2: number) => Canvasimo;
Plot a line and apply a stroke to it.
canvas.strokeLine(x1: number, y1: number, x2: number, y2: number, color?: string) => Canvasimo;
Plot a line, by length & angle, that can then have a stroke or fill applied to it.
canvas.plotLength(x1: number, y1: number, length: number, angle: number) => Canvasimo;
Plot a line, by length & angle, and apply a stroke to it.
canvas.strokeLength(x1: number, y1: number, length: number, angle: number, color?: string) => Canvasimo;
Plot a path, that is not self closing, that can have a stroke or fill applied to it.
canvas.plotPath(points: Points) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
Plot a path, that is not self closing, and apply a stroke to it.
canvas.strokePath(points: Points, color?: string) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
Plot a path, that is not self closing, and apply a fill to it.
canvas.fillPath(points: Points, color?: string) => Canvasimo;
type Points = Array<{x: number, y: number}> | Array<[number, number]> | number[];
A collection of methods for plotting or drawing paths - shapes that can be connected to create more complex shapes.
Plot an arc that can have a stroke or fill applied to it.
canvas.plotArc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot an arc and apply a stroke to it.
canvas.strokeArc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot an arc and apply a fill to it.
canvas.fillArc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot an ellipse that can then have a stroke or fill applied to it.
canvas.plotEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot an ellipse and apply a stroke to it.
canvas.strokeEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Plot an ellipse and apply a fill to it.
canvas.fillEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: BooleanFalsy, color?: string) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
A collection of methods for drawing text, and getting and setting properties related to text rendering.
Draw a text with a stroke.
canvas.strokeText(text: string, x: number, y: number, maxWidth?: MaxWidth, color?: string) => Canvasimo;
type MaxWidth = number | undefined | null;
Draw a text with a fill.
canvas.fillText(text: string, x: number, y: number, maxWidth?: MaxWidth, color?: string) => Canvasimo;
type MaxWidth = number | undefined | null;
Draw text with a stroke, wrapped at newlines and automatically wrapped if the text exceeds the maxWidth. If no maxWidth is specified text will only wrap at newlines (wordBreak is ignore). Words will not break by default (normal) and therefore may overflow. break-all will break words wherever possible, and break-word will only break words if there is not enough room. The lineHeight parameter is a multiplier for the font size, and defaults to 1.
canvas.strokeTextMultiline(text: string, x: number, y: number, maxWidth?: MaxWidth, wordBreak?: WordBreak, lineHeight?: number, color?: string) => Canvasimo;
type MaxWidth = number | undefined | null;type WordBreak = 'normal' | 'break-word' | 'break-all';
Draw text with a fill, wrapped at newlines and automatically wrapped if the text exceeds the maxWidth. If no maxWidth is specified text will only wrap at newlines (wordBreak is ignore). Words will not break by default (normal) and therefore may overflow. break-all will break words wherever possible, and break-word will only break words if there is not enough room. The lineHeight parameter is a multiplier for the font size, and defaults to 1.
canvas.fillTextMultiline(text: string, x: number, y: number, maxWidth?: MaxWidth, wordBreak?: WordBreak, lineHeight?: number, color?: string) => Canvasimo;
type MaxWidth = number | undefined | null;type WordBreak = 'normal' | 'break-word' | 'break-all';
Get information about the size text will be drawn.
canvas.getTextSize(text: string) => LimitedTextMetrics;
type LimitedTextMetrics = { width: number; };
Set the horizontal text alignment.
canvas.setTextAlign(value: TextAlign) => Canvasimo;
type TextAlign = 'left' | 'right' | 'center' | 'start' | 'end';
Get the horizontal text alignment.
canvas.getTextAlign() => TextAlign;
type TextAlign = 'left' | 'right' | 'center' | 'start' | 'end';
Set the vertical text alignment.
canvas.setTextBaseline(value: TextBaseline) => Canvasimo;
type TextBaseline = 'top' | 'hanging' | 'middle' | 'alphabetic' | 'ideographic' | 'bottom';
Get the vertical text alignment.
canvas.getTextBaseline() => TextBaseline;
type TextBaseline = 'top' | 'hanging' | 'middle' | 'alphabetic' | 'ideographic' | 'bottom';
A collection of methods for getting and setting font styles and variations.
Get the font that is being used. This returns the exact CanvasRenderingContext2D.font string.
canvas.getFont() => string;
Get the font size that is being used. Returns null if using a special font e.g. caption, icon, menu.
canvas.getFontSize() => number | null;
Get the font style that is being used. Returns null if using a special font e.g. caption, icon, menu.
canvas.getFontStyle() => string | null;
Get the font variant that is being used. Returns null if using a special font e.g. caption, icon, menu.
canvas.getFontVariant() => string | null;
Get the font weight that is being used. Returns null if using a special font e.g. caption, icon, menu.
canvas.getFontWeight() => string | number | null;
A collection of methods for getting and setting stroke styles, and applying strokes to existing shapes.
Apply a stroke to the current shape.
canvas.stroke(color?: string) => Canvasimo;canvas.stroke(path?: Path2D) => Canvasimo;canvas.stroke(color: string, path: Path2D) => Canvasimo;
Set the stroke style to use.
canvas.setStroke(value: FillOrStrokeStyle) => Canvasimo;
type FillOrStrokeStyle = string | CanvasGradient | CanvasPattern;
Get the stroke style that is being used.
canvas.getStroke() => FillOrStrokeStyle;
type FillOrStrokeStyle = string | CanvasGradient | CanvasPattern;
Set the stroke cap to use.
canvas.setStrokeCap(value: LineCap) => Canvasimo;
type LineCap = 'butt' | 'round' | 'square';
Get the stroke cap that is being used.
canvas.getStrokeCap() => LineCap;
type LineCap = 'butt' | 'round' | 'square';
Set the stroke dash to use.
canvas.setStrokeDash(segments: number[]) => Canvasimo;
Get the stroke dash that is being used.
canvas.getStrokeDash() => number[];
Set the stroke dash offset to use.
canvas.setStrokeDashOffset(value: number) => Canvasimo;
Get the stroke dash offset that is being used.
canvas.getStrokeDashOffset() => number;
Set the stroke join to use.
canvas.setStrokeJoin(value: LineJoin) => Canvasimo;
type LineJoin = 'bevel' | 'round' | 'miter';
Get the stroke join that is being used.
canvas.getStrokeJoin() => LineJoin;
type LineJoin = 'bevel' | 'round' | 'miter';
Set the stroke width to use.
canvas.setStrokeWidth(value: number) => Canvasimo;
Get the stroke width that is being used.
canvas.getStrokeWidth() => number;
A collection of methods for getting and setting fill styles, and applying fills to existing shapes.
Apply a fill to the current shape.
canvas.fill(color?: string) => Canvasimo;canvas.fill(fillRule?: FillRule) => Canvasimo;canvas.fill(color: string, fillRule: FillRule) => Canvasimo;
type FillRule = 'nonzero' | 'evenodd';
Clear a rectangular area of the canvas.
canvas.clearRect(x: number, y: number, width: number, height: number) => Canvasimo;
Set the fill to use.
canvas.setFill(value: FillOrStrokeStyle) => Canvasimo;
type FillOrStrokeStyle = string | CanvasGradient | CanvasPattern;
Get the fill that is being used.
canvas.getFill() => FillOrStrokeStyle;
type FillOrStrokeStyle = string | CanvasGradient | CanvasPattern;
Create a linear gradient to use as a fill.
canvas.createLinearGradient(x0: number, y0: number, x1: number, y1: number) => CanvasGradient;
Create a radial gradient to use as a fill.
canvas.createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number) => CanvasGradient;
Create a pattern to be used as a fill.
canvas.createPattern(image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, repetition: string) => CanvasPattern | null;
Draw an image to the canvas. If the second position / size arguments are supplied, the first will be used for cropping the image, and the second for the position and size it will be drawn.
canvas.drawImage(image: ImageLike, dstX: number, dstY: number) => Canvasimo;canvas.drawImage(image: ImageLike, dstX: number, dstY: number, dstW: number, dstH: number) => Canvasimo;canvas.drawImage(image: ImageLike, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number) => Canvasimo;
type ImageLike = HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap;
A collection of methods for creating, putting, or getting image data about the canvas.
Get a data URL of the current canvas state.
canvas.getDataURL(type?: string, ...args: any[]) => string;
Create image data with either the width and height specified, or with the width and height of a the image data supplied.
canvas.createImageData(width: number, height: number) => ImageData;canvas.createImageData(ImageData: ImageData) => ImageData;
Get the image data from an area of the canvas.
canvas.getImageData(sx: number, sy: number, sw: number, sh: number) => ImageData;
Draw image data onto the canvas.
canvas.putImageData(imagedata: ImageData, dx: number, dy: number) => Canvasimo;canvas.putImageData(imagedata: ImageData, dx: number, dy: number, dirtyX: number, dirtyY: number, dirtyWidth: number, dirtyHeight: number) => Canvasimo;
Get image data about a specific pixel.
canvas.getPixelData(x: number, y: number) => Uint8ClampedArray;
Get the color of a specific pixel.
canvas.getPixelColor(x: number, y: number) => string;
A collection of methods to help with creating color strings.
Create an HSL color string from the given values.
canvas.createHSL(h: number, s: number, l: number) => string;
Create an HSLA color string from the given values.
canvas.createHSLA(h: number, s: number, l: number, a: number) => string;
Create an RGB color string from the given values.
canvas.createRGB(r: number, g: number, b: number) => string;
Create an RGBA color string from the given values.
canvas.createRGBA(r: number, g: number, b: number, a: number) => string;
Return an HSL color string from the given HSLA color string.
canvas.getHSLFromHSLA(color: string) => string;
Return an RGB color string from the given RGBA color string.
canvas.getRGBFromRGBA(color: string) => string;
A collection of methods to help with calculating and converting sizes, and distances.
Get a fraction from the provided percent value e.g. 80 returns 0.8.
canvas.getFractionFromPercent(percent: number) => number;
Get a percent from the provided fraction value e.g. 0.7 returns 70.
canvas.getPercentFromFraction(fraction: number) => number;
Returns the actual value of a fraction of the canvas width e.g. a canvas with a width of 200 returns 100 if the provided value is 0.5.
canvas.getFractionOfWidth(fraction: number) => number;
Returns the actual value of a fraction of the canvas height e.g. a canvas with a height of 100 returns 20 if the provided value is 0.2.
canvas.getFractionOfHeight(fraction: number) => number;
Returns the actual value of a percentage of the canvas width e.g. a canvas with a width of 200 returns 100 if the provided value is 50.
canvas.getPercentOfWidth(percent: number) => number;
Returns the actual value of a percentage of the canvas height e.g. a canvas with a height of 100 returns 20 if the provided value is 20.
canvas.getPercentOfHeight(percent: number) => number;
Returns the distance between 2 points.
canvas.getDistance(x1: number, y1: number, x2: number, y2: number) => number;
A collection of methods to help with calculating and converting angles.
Get a radian value from the provided degrees e.g. 90 returns 1.5708.
canvas.getRadiansFromDegrees(degrees: number) => number;
Get a degree value from the provided radians e.g. 3.14159 returns 180.
canvas.getDegreesFromRadians(radians: number) => number;
Get the angle (in radians) between 2 or 3 points.
canvas.getAngle(x1: number, y1: number, x2: number, y2: number) => number;canvas.getAngle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) => number;
A collection of methods for path drawing.
Move the starting point of a the next sub-path.
canvas.moveTo(x: number, y: number) => Canvasimo;
Connect the last point to the provided coordinates.
canvas.lineTo(x: number, y: number) => Canvasimo;
Arc from one point to another.
canvas.arcTo(x1: number, y1: number, x2: number, y2: number, radius: number) => Canvasimo;
Connect the last point to the provided coordinates with a bezier curve (2 control points).
canvas.bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) => Canvasimo;
Connect the last point to the provided coordinates with a quadratic curve (1 control point).
canvas.quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) => Canvasimo;
A collection of methods to save, restore, or transform the canvas state.
Push the current state of the canvas into a stack that can later be restored.
canvas.save() => Canvasimo;
Add rotation (in radians) to the transform matrix so that shapes can be drawn at an angle.
canvas.rotate(angle: number) => Canvasimo;
Scale the transform matrix so that shapes can be drawn at the provided scale.
canvas.scale(x: number, y: number) => Canvasimo;
Multiply the current transformation with the provided matrix.
canvas.transform(m11: number, m12: number, m21: number, m22: number, dx: number, dy: number) => Canvasimo;
Replace the current transformation with the provided matrix.
canvas.setTransform(m11: number, m12: number, m21: number, m22: number, dx: number, dy: number) => Canvasimo;
Replace the current transformation with the default matrix: [1, 0, 0, 1, 0, 0].
canvas.resetTransform() => Canvasimo;
Use the current path as a clipping path.
canvas.clip(fillRule?: FillRule) => Canvasimo;
type FillRule = 'nonzero' | 'evenodd';
Set the opacity to use for drawing.
canvas.setOpacity(value: number) => Canvasimo;
Set the composite operation to use for drawing.
canvas.setCompositeOperation(value: GlobalCompositeOperation) => Canvasimo;
type GlobalCompositeOperation = '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';
Get the composite operation that is being used.
canvas.getCompositeOperation() => GlobalCompositeOperation;
type GlobalCompositeOperation = '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';
Set whether image smoothing should be used.
canvas.setImageSmoothingEnabled(value: BooleanFalsy) => Canvasimo;
type BooleanFalsy = boolean | undefined | null;
Get whether image smoothing is being used.
canvas.getImageSmoothingEnabled() => boolean;
Set the image smoothing quality.
canvas.setImageSmoothingQuality(value: ImageSmoothingQuality) => Canvasimo;
type ImageSmoothingQuality = "low" | "medium" | "high";
Get the current image smoothing quality.
canvas.getImageSmoothingQuality() => ImageSmoothingQuality;
type ImageSmoothingQuality = "low" | "medium" | "high";
Set the color to be used for shadows.
canvas.setShadowColor(value: string) => Canvasimo;
Set how horizontally offset shadows should be.
canvas.setShadowOffsetX(value: number) => Canvasimo;
Get the value of how horizontally offset shadows should be.
canvas.getShadowOffsetX() => number;
Set how vertically offset shadows should be.
canvas.setShadowOffsetY(value: number) => Canvasimo;
Get the value of how vertically offset shadows should be.
canvas.getShadowOffsetY() => number;
Miscellaneous methods.
Break out of the method chain and execute a callback.
canvas.tap(callback: () => any) => Canvasimo;
Break out of the method chain and execute a callback with values between start and end, increasing / decreasing by step (start defaults to 0, step defaults to 1). You may return false from the callback at any point to stop at the current iteration.
canvas.repeat(end: number, callback: (i: number) => any) => Canvasimo;canvas.repeat(start: number, end: number, callback: (i: number) => any) => Canvasimo;canvas.repeat(start: number, end: number, step: number, callback: (i: number) => any) => Canvasimo;
Break out of the method chain and loop over the given array, object or string, calling the callback with the value & key / index. You may return false from the callback at any point to stop at the current iteration.
canvas.forEach(str: string, callback: (value: string, index: number) => any) => Canvasimo;canvas.forEach(obj: any[], callback: (value: any, index: number) => any) => Canvasimo;canvas.forEach(obj: { [i: string]: any; }, callback: (value: any, key: string) => any) => Canvasimo;
Constrain a number between a minimum and maximum value.
canvas.constrain(value: number, min: number, max: number) => number;
Map a value from one range to another e.g. mapping 0.5 from 0-1 to 0-10 returns 5.
canvas.map(value: number, fromStart: number, fromEnd: number, toStart: number, toEnd: number) => number;
Draw a focus ring around the current path, or the path supplied, if the element supplied has focus.
canvas.drawFocusIfNeeded(element: Element) => Canvasimo;
Returns whether the given point is within the current or given path.
canvas.isPointInPath(x: number, y: number, fillRule?: FillRule) => boolean;
type FillRule = 'nonzero' | 'evenodd';
Returns whether the given point is within the area contained by applying a stroke to the current or given path.
canvas.isPointInStroke() => boolean | null;
Return the current version of Canvasimo (and log to console if logInfo parameter is true)
canvas.getVersion(logInfo?: BooleanFalsy) => string;
type BooleanFalsy = boolean | undefined | null;