Canvas
Canvas is a widget that implements part of the CanvasRenderingContext2D interface and can be used to draw graphics via scripting.
Inherits from Widget.
Notes:
- Repeated property assignments or drawing operations may reduce performance.
- Clear the full Canvas to remove previous property assignments and drawings with
canvas.clearRect(0, 0, canvas.width, canvas.height).
Example: Draw a house
var canvas = new Canvas();
this.widget.add(canvas);
canvas.lineWidth = 10;
// Wall
canvas.strokeRect(75, 140, 150, 110);
// Door
canvas.fillRect(130, 190, 40, 60);
// Roof
canvas.beginPath();
canvas.moveTo(50, 140);
canvas.lineTo(150, 60);
canvas.lineTo(250, 140);
canvas.closePath();
canvas.stroke();Properties
Inherits height, opacity, rotation, width, x, y from Widget.
| Property | Type | Description |
|---|---|---|
globalAlpha | Number | Alpha/transparency value for the drawing. MDN |
globalCompositeOperation | String | How a new image is drawn onto an existing image. MDN |
fillStyle | color | CanvasGradient | Color or gradient used to fill the drawing. MDN |
strokeStyle | color | CanvasGradient | Color or gradient used for strokes. MDN |
lineCap | String | Style of end caps for a line. MDN |
lineJoin | String | Type of corner when two lines meet. MDN |
lineWidth | Number | Current line width. MDN |
miterLimit | Number | Maximum miter length. MDN |
lineDashOffset | Number | Line dash offset. MDN |
font | String | Current font properties for text content. MDN |
textAlign | String | Current alignment for text content. MDN |
textBaseline | String | Current text baseline used when drawing text. MDN |
Gradient Methods
createLinearGradient(x0, y0, x1, y1)
Creates a linear gradient. See MDN.
Parameters
| Name | Type | Description |
|---|---|---|
x0 | Number | X coordinate of the start point. |
y0 | Number | Y coordinate of the start point. |
x1 | Number | X coordinate of the end point. |
y1 | Number | Y coordinate of the end point. |
Returns: CanvasGradient
Example
var ctx = new Canvas();
this.widget.add(ctx);
var gradient = ctx.createLinearGradient(20, 0, 220, 0);
gradient.addColorStop(0, 'green');
gradient.addColorStop(.5, 'cyan');
gradient.addColorStop(1, 'green');
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 200, 100);createRadialGradient(x0, y0, r0, x1, y1, r1)
Creates a radial/circular gradient. See MDN.
Parameters
| Name | Type | Description |
|---|---|---|
x0 | Number | X coordinate of the start circle center. |
y0 | Number | Y coordinate of the start circle center. |
r0 | Number | Radius of the start circle (non-negative). |
x1 | Number | X coordinate of the end circle center. |
y1 | Number | Y coordinate of the end circle center. |
r1 | Number | Radius of the end circle (non-negative). |
Returns: CanvasGradient
Line Dash Methods
getLineDash()
Returns the current line dash pattern. See MDN.
Returns: Number[]
setLineDash(segments)
Sets the line dash pattern used when stroking lines. See MDN.
Parameters
| Name | Type | Description |
|---|---|---|
segments | Number[] | Array of distances to alternately draw a line and a gap. An odd-length array gets copied and concatenated. An empty array restores solid lines. |
Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.beginPath();
ctx.setLineDash([5, 15]);
ctx.moveTo(0, 50);
ctx.lineTo(300, 50);
ctx.stroke();Transform Methods
scale(x, y)
Scales the current drawing. See MDN.
| Name | Type | Description |
|---|---|---|
x | Number | Horizontal scaling factor. Negative flips across vertical axis. |
y | Number | Vertical scaling factor. Negative flips across horizontal axis. |
rotate(angle)
Rotates the current drawing. See MDN.
| Name | Type | Description |
|---|---|---|
angle | Number | Rotation angle in radians, clockwise. Use deg * Math.PI / 180 to convert degrees. |
translate(x, y)
Remaps the (0, 0) position on the canvas. See MDN.
| Name | Type | Description |
|---|---|---|
x | Number | Horizontal distance. Positive = right. |
y | Number | Vertical distance. Positive = down. |
transform(a, b, c, d, e, f)
Multiplies the current transformation matrix. See MDN.
| Name | Type | Description |
|---|---|---|
a | Number | (m11) Horizontal scaling. |
b | Number | (m12) Vertical skewing. |
c | Number | (m21) Horizontal skewing. |
d | Number | (m22) Vertical scaling. |
e | Number | (dx) Horizontal translation. |
f | Number | (dy) Vertical translation. |
setTransform(a, b, c, d, e, f)
Resets to identity matrix then runs transform(). See MDN. Same parameters as transform().
Rectangle Methods
fillRect(x, y, width, height)
Draws a filled rectangle. See MDN.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.fillStyle = 'green';
ctx.fillRect(20, 10, 150, 100);strokeRect(x, y, width, height)
Draws a rectangle outline (no fill). See MDN.
clearRect(x, y, width, height)
Clears the specified pixels within a rectangle. See MDN.
All rectangle methods share these parameters:
| Name | Type | Description |
|---|---|---|
x | Number | X coordinate of the rectangle's starting point. |
y | Number | Y coordinate of the rectangle's starting point. |
width | Number | Rectangle width. Positive = right, negative = left. |
height | Number | Rectangle height. Positive = down, negative = up. |
Path Methods
beginPath()
Begins a new path, or resets the current path. See MDN.
closePath()
Creates a path from the current point back to the starting point. See MDN.
moveTo(x, y)
Moves the path to the specified point without creating a line. See MDN.
lineTo(x, y)
Adds a straight line to the path from the current point. See MDN.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.beginPath();
ctx.moveTo(30, 50);
ctx.lineTo(150, 100);
ctx.stroke();bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
Creates a cubic Bézier curve. See MDN.
| Name | Type | Description |
|---|---|---|
cp1x | Number | X coordinate of the first control point. |
cp1y | Number | Y coordinate of the first control point. |
cp2x | Number | X coordinate of the second control point. |
cp2y | Number | Y coordinate of the second control point. |
x | Number | X coordinate of the end point. |
y | Number | Y coordinate of the end point. |
quadraticCurveTo(cpx, cpy, x, y)
Creates a quadratic Bézier curve. See MDN.
| Name | Type | Description |
|---|---|---|
cpx | Number | X coordinate of the control point. |
cpy | Number | Y coordinate of the control point. |
x | Number | X coordinate of the end point. |
y | Number | Y coordinate of the end point. |
arcTo(x1, y1, x2, y2, radius)
Creates an arc/curve between two tangents. See MDN.
| Name | Type | Description |
|---|---|---|
x1 | Number | X coordinate of the first control point. |
y1 | Number | Y coordinate of the first control point. |
x2 | Number | X coordinate of the second control point. |
y2 | Number | Y coordinate of the second control point. |
radius | Number | Arc radius (non-negative). |
arc(x, y, radius, startAngle, endAngle, anticlockwise?)
Creates an arc/curve for circles or parts of circles. See MDN.
| Name | Type | Description |
|---|---|---|
x | Number | X coordinate of the arc's center. |
y | Number | Y coordinate of the arc's center. |
radius | Number | Arc radius (positive). |
startAngle | Number | Start angle in radians from the positive x-axis. |
endAngle | Number | End angle in radians from the positive x-axis. |
anticlockwise | Boolean | Optional. If true, draws counter-clockwise. Default: false. |
var ctx = new Canvas();
this.widget.add(ctx);
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.stroke();rect(x, y, width, height)
Adds a rectangle to the current path. See MDN.
isPointInPath(x, y)
Returns true if the specified point is inside the current path. See MDN.
Returns: Boolean
fill()
Fills the current path. See MDN.
stroke()
Draws (strokes) the current path. See MDN.
clip()
Clips a region from the canvas. Subsequent drawing is limited to this region. See MDN.
Text Methods
fillText(text, x, y)
Draws filled text on the canvas. See MDN.
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);strokeText(text, x, y)
Draws text outline (no fill). See MDN.
Text method parameters:
| Name | Type | Description |
|---|---|---|
text | String | Text to render. Respects font, textAlign, textBaseline. |
x | Number | X coordinate of the text start point. |
y | Number | Y coordinate of the text start point. |
measureText(text)
Returns an object containing the width of the specified text.
Returns: Object with a width property.
let text = canvas.measureText('Hello world');
console.log(text.width);ImageData Methods
createImageData(width, height)
Creates a new blank ImageData object filled with transparent black pixels.
Returns: ImageData
const imageData = canvas.createImageData(100, 50);getImageData(sx, sy, sw, sh)
Returns an ImageData representing the pixel data for a portion of the canvas. Not affected by the canvas transformation matrix.
| Name | Type | Description |
|---|---|---|
sx | Number | X coordinate of the top-left corner of the extraction rectangle. |
sy | Number | Y coordinate of the top-left corner. |
sw | Number | Width of the extraction rectangle. |
sh | Number | Height of the extraction rectangle. |
Returns: ImageData
putImageData(imageData, dx, dy)
Paints pixel data from an ImageData object onto the canvas. Not affected by the canvas transformation matrix.
| Name | Type | Description |
|---|---|---|
imageData | ImageData | The pixel data to paint. |
dx | Number | Horizontal position (x) in the destination canvas. |
dy | Number | Vertical position (y) in the destination canvas. |
State Methods
save()
Saves the state of the current context. See MDN.
restore()
Restores the previously saved path state and attributes. See MDN.