Canvas

Canvas

Canvas is a widget which implements part of the CanvasRenderingContext2D interface and can be used to draw graphics via scripting.

Note:

  • Excessive value-assignment (e.g. canvas.lineWidth = 10) or drawing (e.g. canvas.stroke()) operations may cause system performance decrease.
  • Users can clear the entire canvas to erase the value-assignment or drawing operations made previously.

Constructor

new Canvas()

Initializes a new instance of Canvas

Examples

To clear the entire canvas:

canvas.clearRect(0, 0, canvas.width, canvas.height);

To draw a house:

var canvas = new Canvas();
this.widget.add(canvas);

// Set line width
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();

Extends

Members

fillStyle :color|gradient

See:

Sets or returns the color or gradient used to fill the drawing.

Type:
  • color | gradient

font :font

See:

Sets or returns the current font properties for text content.

Type:
  • font

globalAlpha :Number

See:

Sets or returns the current alpha or transparency value of the drawing.

Type:
  • Number

globalCompositeOperation :String

See:

Sets or returns how a new image is drawn onto an existing image.

Type:
  • String

height :Number

Overrides:

This property holds the height of the widget.

Type:
  • Number

lineCap :String

See:

Sets or returns the style of the end caps for a line.

Type:
  • String

lineDashOffset :Number

See:

Sets or returns the line dash offset.

Type:
  • Number

lineJoin :String

See:

Sets or returns the type of corner created, when two lines meet.

Type:
  • String

lineWidth :Number

See:

Sets or returns the current line width.

Type:
  • Number

miterLimit :Number

See:

Sets or returns the maximum miter length.

Type:
  • Number

opacity :Number

Overrides:

This property holds the opacity of the widget. The valid range of opacity is from 1.0 (completely opaque) to 0.0 (completely transparent).

Type:
  • Number

rotation :Number

Overrides:

This property holds the rotation of the widget in degrees clockwise.

Type:
  • Number

strokeStyle :color|gradient

See:

Sets or returns the color or gradient used for strokes.

Type:
  • color | gradient

textAlign :String

See:

Sets or returns the current alignment for text content.

Type:
  • String

textBaseline :String

See:

Sets or returns the current text baseline used when drawing text.

Type:
  • String

width :Number

Overrides:

This property holds the width of the widget.

Type:
  • Number

x :Number

Overrides:

This property holds the x coordinate of the widget relative to its parent.

Type:
  • Number

y :Number

Overrides:

This property holds the y coordinate of the widget relative to its parent.

Type:
  • Number

Methods

arc(x, y, radius, startAngle, endAngle, anticlockwiseopt)

See:

Creates an arc/curve (used to create circles, or parts of circles).

Example
// The arc is given an x-coordinate of 100, a y-coordinate of 75, and a radius of 50. 
// To make a full circle, the arc begins at an angle of 0 radians (0°), and ends at an angle of 2π radians (360°).
var ctx = new Canvas();
this.widget.add(ctx);
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.stroke();
Parameters:
Name Type Attributes Description
x Number

The horizontal coordinate of the arc's center.

y Number

The vertical coordinate of the arc's center.

radius Number

The arc's radius. Must be positive.

startAngle Number

The angle at which the arc starts in radians, measured from the positive x-axis.

endAngle Number

The angle at which the arc ends in radians, measured from the positive x-axis.

anticlockwise Number <optional>

An optional Boolean. If true, draws the arc counter-clockwise between the start and end angles. The default is false (clockwise).

arcTo(x1, y1, x2, y2, radius)

See:

Creates an arc/curve between two tangents.

Example
// In this example, the path created by arcTo() is thick and black. Tangent lines are gray, control points are red, and the start point is blue.
var ctx = new Canvas();
this.widget.add(ctx);
// Tangential lines
ctx.beginPath();
ctx.strokeStyle = 'gray';
ctx.moveTo(200, 20);
ctx.lineTo(200, 130);
ctx.lineTo(50, 20);
ctx.stroke();

// Arc
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.lineWidth = 5;
ctx.moveTo(200, 20);
ctx.arcTo(200,130, 50,20, 40);
ctx.stroke();

// Start point
ctx.beginPath();
ctx.fillStyle = 'blue';
ctx.arc(200, 20, 5, 0, 2 * Math.PI);
ctx.fill();

// Control points
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.arc(200, 130, 5, 0, 2 * Math.PI); // Control point one
ctx.arc(50, 20, 5, 0, 2 * Math.PI);   // Control point two
ctx.fill();
Parameters:
Name Type Description
x1 Number

The x-axis coordinate of the first control point.

y1 Number

The y-axis coordinate of the first control point.

x2 Number

The x-axis coordinate of the second control point.

y2 Number

The y-axis coordinate of the second control point.

radius Number

The arc's radius. Must be non-negative.

beginPath()

See:

Begins a path, or resets the current path.

Example
var ctx = new Canvas();
this.widget.add(ctx);
// First path
ctx.beginPath();
ctx.strokeStyle = 'blue';
ctx.moveTo(20, 20);
ctx.lineTo(200, 20);
ctx.stroke();

// Second path
ctx.beginPath();
ctx.strokeStyle = 'green';
ctx.moveTo(20, 20);
ctx.lineTo(120, 120);
ctx.stroke();

bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)

See:

Creates a cubic Bézier curve.

Example
// This example shows how a cubic Bézier curve is drawn.
var ctx = new Canvas();
this.widget.add(ctx);
// Define the points as {x, y}
let start = { x: 50,    y: 20  };
let cp1 =   { x: 230,   y: 30  };
let cp2 =   { x: 150,   y: 80  };
let end =   { x: 250,   y: 100 };

// Cubic Bézier curve
ctx.beginPath();
ctx.moveTo(start.x, start.y);
ctx.bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, end.x, end.y);
ctx.stroke();

// Start and end points
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.arc(start.x, start.y, 5, 0, 2 * Math.PI);  // Start point
ctx.arc(end.x, end.y, 5, 0, 2 * Math.PI);      // End point
ctx.fill();

// Control points
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(cp1.x, cp1.y, 5, 0, 2 * Math.PI);  // Control point one
ctx.arc(cp2.x, cp2.y, 5, 0, 2 * Math.PI);  // Control point two
ctx.fill();
Parameters:
Name Type Description
cp1x Number

The x-axis coordinate of the first control point.

cp1y Number

The y-axis coordinate of the first control point.

cp2x Number

The x-axis coordinate of the second control point.

cp2y Number

The y-axis coordinate of the second control point.

x Number

The x-axis coordinate of the end point.

y Number

The y-axis coordinate of the end point.

clearRect(x, y, width, height)

See:

Clears the specified pixels within a given rectangle.

Parameters:
Name Type Description
x Number

The x-axis coordinate of the rectangle's starting point.

y Number

The y-axis coordinate of the rectangle's starting point.

width Number

The rectangle's width. Positive values are to the right, and negative to the left.

height Number

The rectangle's height. Positive values are down, and negative are up.

clip()

See:

Clips a region of any shape and size from the original canvas.

Example
var ctx = new Canvas();
this.widget.add(ctx);
// Create circular clipping region
ctx.beginPath();
ctx.arc(100, 75, 50, 0, Math.PI * 2);
ctx.clip();
// Draw stuff that gets clipped
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, ctx.width, ctx.height);
ctx.fillStyle = 'orange';
ctx.fillRect(0, 0, 100, 100);

closePath()

See:

Creates a path from the current point back to the starting point.

Example
var ctx = new Canvas();
this.widget.add(ctx);
// Draw a triangle
ctx.beginPath();
ctx.moveTo(20, 140);   // Move pen to bottom-left corner
ctx.lineTo(120, 10);   // Line to top corner
ctx.lineTo(220, 140);  // Line to bottom-right corner
ctx.closePath();       // Line to bottom-left corner
ctx.stroke();

createImageData(width, height) → {ImageData}

Since:
  • EasyBuilder Pro V6.06.02

Creates a new, blank ImageData object with the specified dimensions. All of the pixels in the new object are transparent black.

Example
const imageData = canvas.createImageData(100, 50);
Parameters:
Name Type Description
width Number

The width to give the new ImageData object.

height Number

The height to give the new ImageData object.

Returns:
  • A new ImageData object with the specified width and height. The new object is filled with transparent black pixels.
Type
ImageData

createLinearGradient(x0, y0, x1, y1) → {CanvasGradient}

See:

Creates a linear gradient (to use on canvas content).

Example
var ctx = new Canvas();
this.widget.add(ctx);
// Create a linear gradient
// The start gradient point is at x=20, y=0
// The end gradient point is at x=220, y=0
var gradient = ctx.createLinearGradient(20,0, 220,0);

// Add three color stops
gradient.addColorStop(0, 'green');
gradient.addColorStop(.5, 'cyan');
gradient.addColorStop(1, 'green');

// Set the fill style and draw a rectangle
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 200, 100);
Parameters:
Name Type Description
x0 Number

The x-axis coordinate of the start point.

y0 Number

The y-axis coordinate of the start point.

x1 Number

The x-axis coordinate of the end point.

y1 Number

The y-axis coordinate of the end point.

Returns:
  • A linear gradient initialized with the specified line.
Type
CanvasGradient

createRadialGradient(x0, y0, r0, x1, y1, r1) → {CanvasGradient}

See:

Creates a radial/circular gradient (to use on canvas content).

Example
// This example initializes a radial gradient using the createRadialGradient() method. Three color stops between the gradient's two circles are then created. Finally, the gradient is assigned to the canvas context, and is rendered to a filled rectangle.
var ctx = new Canvas();
this.widget.add(ctx);
// Create a radial gradient
// The inner circle is at x=110, y=90, with radius=30
// The outer circle is at x=100, y=100, with radius=70
var gradient = ctx.createRadialGradient(110,90,30, 100,100,70);

// Add three color stops
gradient.addColorStop(0, 'pink');
gradient.addColorStop(.9, 'white');
gradient.addColorStop(1, 'green');

// Set the fill style and draw a rectangle
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 160, 160);
Parameters:
Name Type Description
x0 Number

The x-axis coordinate of the start circle.

y0 Number

The y-axis coordinate of the start circle.

r0 Number

The radius of the start circle. Must be non-negative and finite.

x1 Number

The x-axis coordinate of the end circle.

y1 Number

The y-axis coordinate of the end circle.

r1 Number

The radius of the end circle. Must be non-negative and finite.

Returns:
  • A radial gradient initialized with the two specified circles.
Type
CanvasGradient

fill()

See:

Fills the current drawing (path).

Example
// Fills a rectangle with the fill() method.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.rect(10, 10, 150, 100);
ctx.stroke();

fillRect(x, y, width, height)

See:

Draws a "filled" rectangle.

Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.fillStyle = 'green';
ctx.fillRect(20, 10, 150, 100);
Parameters:
Name Type Description
x Number

The x-axis coordinate of the rectangle's starting point.

y Number

The y-axis coordinate of the rectangle's starting point.

width Number

The rectangle's width. Positive values are to the right, and negative to the left.

height Number

The rectangle's height. Positive values are down, and negative are up.

fillText(text, x, y)

See:

Draws "filled" text on the canvas.

Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);
Parameters:
Name Type Description
text String

A text string to render into the context. The text is rendered using the settings specified by font, textAlign, textBaseline, and direction.

x Number

The x-axis coordinate of the point at which to begin drawing the text.

y Number

The y-axis coordinate of the point at which to begin drawing the text.

getImageData(sx, sy, sw, sh) → {ImageData}

Since:
  • EasyBuilder Pro V6.06.02

Returns an ImageData object representing the underlying pixel data for a specified portion of the canvas.

Note:

  • This method is not affected by the canvas transformation matrix.
Example
canvas.rect(10, 10, 100, 100);
canvas.fill();

let imageData = canvas.getImageData(0, 0, 50, 50);
canvas.putImageData(imageData, 150, 10);
Parameters:
Name Type Description
sx Number

The x-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.

sy Number

The y-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.

sw Number

The width of the rectangle from which the ImageData will be extracted.

sh Number

The height of the rectangle from which the ImageData will be extracted.

Returns:
  • An ImageData object containing the image data for the rectangle of the canvas specified.
Type
ImageData

getLineDash() → {Array.Number}

See:

Gets the current line dash pattern.

Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.setLineDash([10, 20]);
console.log(ctx.getLineDash());  // [10, 20]
Returns:
Type
Array.Number

isPointInPath(x, y) → {Boolean}

See:

Returns true if the specified point is in the current path, otherwise false.

Example
// Check if a point is within the current path.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.rect(10, 10, 100, 100);
ctx.fill();
console.log(ctx.isPointInPath(30, 70));
Parameters:
Name Type Description
x Number

The x-axis coordinate of the point to check, unaffected by the current transformation of the context.

y Number

The y-axis coordinate of the point to check, unaffected by the current transformation of the context.

Returns:
Type
Boolean

lineTo(x, y)

See:

Adds a new point and creates a line to that point from the last specified point in the canvas.

Example
var ctx = new Canvas();
this.widget.add(ctx);
// A line begins at (30, 50) and ends at (150, 100).
ctx.beginPath();       // Start a new path
ctx.moveTo(30, 50);    // Move the pen to (30, 50)
ctx.lineTo(150, 100);  // Draw a line to (150, 100)
ctx.stroke();          // Render the path
Parameters:
Name Type Description
x Number

The x-axis coordinate of the line's end point.

y Number

The y-axis coordinate of the line's end point.

measureText(text) → {Object}

Returns an object that contains the width of the specified text.

Example
let text = canvas.measureText('Hello world');
console.log(text.width);
Parameters:
Name Type Description
text String

The text String to measure.

Returns:
Type
Object

moveTo(x, y)

See:

Moves the path to the specified point in the canvas, without creating a line.

Parameters:
Name Type Description
x Number

The x-axis (horizontal) coordinate of the point.

y Number

The y-axis (vertical) coordinate of the point.

putImageData(imageData, dx, dy)

Since:
  • EasyBuilder Pro V6.06.02

Paints data from the given ImageData object onto the canvas.

Note:

  • This method is not affected by the canvas transformation matrix.
Example
let canvas = new Canvas();
let mouseArea = new MouseArea();

this.widget.add(canvas);
this.widget.add(mouseArea);

// Create gradient
var grd = canvas.createLinearGradient(0, 0, canvas.width - 20, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

// Fill with gradient
canvas.fillStyle = grd;
canvas.fillRect(10, 10, canvas.width - 20, canvas.height - 20);

mouseArea.on('mousedown', (mouseEvent) => {
  const rvalue = getRandomInt(255);
  const gvalue = getRandomInt(255);
  const bvalue = getRandomInt(255);

  let imageData = canvas.createImageData(10, 10);
  for (let i = 0; i < imageData.data.length; i += 4) {
    imageData.data[i + 0] = rvalue;
    imageData.data[i + 1] = gvalue;
    imageData.data[i + 2] = bvalue;
    imageData.data[i + 3] = 255;
  }
  canvas.putImageData(imageData, mouseEvent.x - 5, mouseEvent.y - 5);
});

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}
Parameters:
Name Type Description
imageData ImageData

An ImageData object containing the array of pixel values.

dx Number

Horizontal position (x coordinate) at which to place the image data in the destination canvas.

dy Number

Vertical position (y coordinate) at which to place the image data in the destination canvas.

quadraticCurveTo(cpx, cpy, x, y)

See:

Creates a quadratic Bézier curve.

Example
// This example shows how a quadratic Bézier curve is drawn.
var ctx = new Canvas();
this.widget.add(ctx);
// Quadratic Bézier curve
ctx.beginPath();
ctx.moveTo(50, 20);
ctx.quadraticCurveTo(230, 30, 50, 100);
ctx.stroke();

// Start and end points
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.arc(50, 20, 5, 0, 2 * Math.PI);   // Start point
ctx.arc(50, 100, 5, 0, 2 * Math.PI);  // End point
ctx.fill();

// Control point
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(230, 30, 5, 0, 2 * Math.PI);
ctx.fill();
Parameters:
Name Type Description
cpx Number

The x-axis coordinate of the control point.

cpy Number

The y-axis coordinate of the control point.

x Number

The x-axis coordinate of the end point.

y Number

The y-axis coordinate of the end point.

rect(x, y, width, height)

See:

Creates a rectangle.

Example
// The rectangle's corner is located at (10, 20). It has a width of 150 and a height of 100.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.rect(10, 20, 150, 100);
ctx.fill();
Parameters:
Name Type Description
x Number

The x-axis coordinate of the rectangle's starting point.

y Number

The y-axis coordinate of the rectangle's starting point.

width Number

The rectangle's width. Positive values are to the right, and negative to the left.

height Number

The rectangle's height. Positive values are down, and negative are up.

restore()

See:

Returns previously saved path state and attributes.

rotate(angle)

See:

Rotates the current drawing.

Parameters:
Name Type Description
angle Number

The rotation angle, clockwise in radians. You can use degree * Math.PI / 180 to calculate a radian from a degree.

save()

See:

Saves the state of the current context.

scale(x, y)

See:

Scales the current drawing bigger or smaller.

Parameters:
Name Type Description
x Number

Scaling factor in the horizontal direction. A negative value flips pixels across the vertical axis. A value of 1 results in no horizontal scaling.

y Number

Scaling factor in the vertical direction. A negative value flips pixels across the horizontal axis. A value of 1 results in no vertical scaling.

setLineDash(segments)

See:

Sets the line dash pattern used when stroking lines.

Example
// This example uses the setLineDash() method to draw a dashed line above a solid line.
var ctx = new Canvas();
this.widget.add(ctx);
// Dashed line
ctx.beginPath();
ctx.setLineDash([5, 15]);
ctx.moveTo(0, 50);
ctx.lineTo(300, 50);
ctx.stroke();

// Solid line
ctx.beginPath();
ctx.setLineDash([]);
ctx.moveTo(0, 100);
ctx.lineTo(300, 100);
ctx.stroke();
Parameters:
Name Type Description
segments Array.Number

An Array of numbers that specify distances to alternately draw a line and a gap (in coordinate space units). If the number of elements in the array is odd, the elements of the array get copied and concatenated. For example, [5, 15, 25] will become [5, 15, 25, 5, 15, 25]. If the array is empty, the line dash list is cleared and line strokes return to being solid.

setTransform(a, b, c, d, e, f)

See:
  • MDN web docs for details. // This example skews a rectangle both vertically (.2) and horizontally (.8). Scaling and translation remain unchanged. var ctx = new Canvas(); this.widget.add(ctx); ctx.transform(1, .2, .8, 1, 0, 0); ctx.fillRect(0, 0, 100, 100);

Resets the current transform to the identity matrix. Then runs transform().

Parameters:
Name Type Description
a Number

(m11) Horizontal scaling. A value of 1 results in no scaling.

b Number

(m12) Vertical skewing.

c Number

(m21) Horizontal skewing.

d Number

(m22) Vertical scaling. A value of 1 results in no scaling.

e Number

(dx) Horizontal translation (moving).

f Number

(dy) Vertical translation (moving).

stroke()

See:

Actually draws the path you have defined.

Example
// Creates a rectangle using the rect() method, and then draws it to the canvas using stroke().
var ctx = new Canvas();
this.widget.add(ctx);
ctx.rect(10, 10, 150, 100);
ctx.stroke();

strokeRect(x, y, width, height)

See:

Draws a rectangle (no fill).

Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.strokeStyle = 'green';
ctx.strokeRect(20, 10, 160, 100);
Parameters:
Name Type Description
x Number

The x-axis coordinate of the rectangle's starting point.

y Number

The y-axis coordinate of the rectangle's starting point.

width Number

The rectangle's width. Positive values are to the right, and negative to the left.

height Number

The rectangle's height. Positive values are down, and negative are up.

strokeText(text, x, y)

See:

Draws text on the canvas (no fill).

Example
var ctx = new Canvas();
this.widget.add(ctx);
ctx.font = '50px serif';
ctx.strokeText('Hello world', 50, 90);
Parameters:
Name Type Description
text String

A text string to render into the context. The text is rendered using the settings specified by font, textAlign, textBaseline, and direction.

x Number

The x-axis coordinate of the point at which to begin drawing the text.

y Number

The y-axis coordinate of the point at which to begin drawing the text.

transform(a, b, c, d, e, f)

See:

Replaces the current transformation matrix for the drawing.

Example
// This example skews a rectangle both vertically (.2) and horizontally (.8). Scaling and translation remain unchanged.
var ctx = new Canvas();
this.widget.add(ctx);
ctx.transform(1, .2, .8, 1, 0, 0);
ctx.fillRect(0, 0, 100, 100);
Parameters:
Name Type Description
a Number

(m11) Horizontal scaling. A value of 1 results in no scaling.

b Number

(m12) Vertical skewing.

c Number

(m21) Horizontal skewing.

d Number

(m22) Vertical scaling. A value of 1 results in no scaling.

e Number

(dx) Horizontal translation (moving).

f Number

(dy) Vertical translation (moving).

translate(x, y)

See:

Remaps the (0,0) position on the canvas.

Example
// The translate() method translates the context by 110 horizontally and 30 vertically. The first square is shifted by those amounts from its default position.
var ctx = new Canvas();
this.widget.add(ctx);
// Moved square
ctx.translate(110, 30);
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 80, 80);

// Reset current transformation matrix to the identity matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);

// Unmoved square
ctx.fillStyle = 'gray';
ctx.fillRect(0, 0, 80, 80);
Parameters:
Name Type Description
x Number

Distance to move in the horizontal direction. Positive values are to the right, and negative to the left.

y Number

Distance to move in the vertical direction. Positive values are down, and negative are up.