CanvasCommand

Description

Supported Script Types: Interface Scripts • Client Entity Scripts

Examples

Create a canvas entity and draw "Hello, world!" into it as text.

const CanvasCommand = Script.require("canvasCommand");

const canvas = Entities.addEntity({
    type: "Canvas",
    position: Vec3.sum(MyAvatar.position, Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 })),
    dimensions: { x: 1, y: 0.5, z: 0.01 },
    lifetime: 30,  // Delete after 30 seconds.
    width: 256,
    height: 128,
    unlit: true,
    transparent: true,
}, "local");

Entities.canvasPushCommands(canvas, [
    CanvasCommand.color([255, 255, 255, 255]),
    CanvasCommand.font("sans-serif", 20),
    CanvasCommand.fillText(
        "Hello, world!",
        0, 0,
        256, 128,
        CanvasCommand.TEXT_ALIGN_CENTER
    ),
]);

Entities.canvasCommit(canvas);

Create a canvas entity and draw an XOR pattern into it.

const CanvasCommand = Script.require("canvasCommand");

const canvas = Entities.addEntity({
    type: "Canvas",
    position: Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiplyQbyV(MyAvatar.orientation, [0, 0, -2])),
    rotation: MyAvatar.orientation,
    dimensions: [2, 2, 0.1],
    width: 256,
    height: 256,
    unlit: true,
    collisionless: true,
}, "local");

const buffer = new Uint8Array(256 * 256 * 4);
const img = {data: buffer.buffer, width: 256, height: 256};

for (let x = 0; x < 256; x++) {
    for (let y = 0; y < 256; y++) {
        let color = x ^ y;
        buffer[(y * 256 * 4) + (x * 4) + 0] = color;
        buffer[(y * 256 * 4) + (x * 4) + 1] = color;
        buffer[(y * 256 * 4) + (x * 4) + 2] = color;
        buffer[(y * 256 * 4) + (x * 4) + 3] = 255;
    }
}

Entities.canvasPushPixels(canvas, img);
Entities.canvasCommit(canvas);

Script.scriptEnding.connect(() => Entities.deleteEntity(canvas));

// delete after 10 seconds
Script.setTimeout(() => Script.stop(), 1000 * 10);

Properties

Name Type Summary
HINT_NO_ANTIALIASING number

If set, drawn shapes will have pixelated edges.

Default Value: 1

HINT_NO_TEXT_ANTIALIASING number

If set, text will have pixelated edges.

Default Value: 2

HINT_NEAREST_SCALING number

If set, drawn images will be scaled using pixelated, nearest-neighbor scaling.

Default Value: 4

TEXT_ALIGN_LEFT number

Align drawn text to the left side of its bounding box.

Default Value: 1

TEXT_ALIGN_RIGHT number

Align drawn text to the right side of its bounding box.

Default Value: 2

TEXT_ALIGN_HCENTER number

Align drawn text horizontally to the center of its bounding box.

Default Value: 4

TEXT_ALIGN_JUSTIFY number

Justifies text horizontally to fit in its bounding box.

Default Value: 8

TEXT_ALIGN_TOP number

Align drawn text to the top of its bounding box.

Default Value: 32

TEXT_ALIGN_BOTTOM number

Align drawn text to the bottom of its bounding box.

Default Value: 64

TEXT_ALIGN_VCENTER number

Align drawn text vertically to the center of its bounding box.

Default Value: 32

TEXT_ALIGN_CENTER number

Align drawn text to the center of its bounding box, both horizontally and vertically.

Default Value: 132

BLEND_SOURCEOVER number

Default Value: 0

BLEND_DESTINATIONOVER number

Default Value: 1

BLEND_CLEAR number

Default Value: 2

BLEND_SOURCE number

Default Value: 3

BLEND_DESTINATION number

Default Value: 4

BLEND_SOURCEIN number

Default Value: 5

BLEND_DESTINATIONIN number

Default Value: 6

BLEND_SOURCEOUT number

Default Value: 7

BLEND_DESTINATIONOUT number

Default Value: 8

BLEND_SOURCEATOP number

Default Value: 9

BLEND_DESTINATIONATOP number

Default Value: 10

BLEND_XOR number

Default Value: 11

BLEND_PLUS number

Default Value: 12

BLEND_MULTIPLY number

Default Value: 13

BLEND_SCREEN number

Default Value: 14

BLEND_OVERLAY number

Default Value: 15

BLEND_DARKEN number

Default Value: 16

BLEND_LIGHTEN number

Default Value: 17

BLEND_COLORDODGE number

Default Value: 18

BLEND_COLORBURN number

Default Value: 19

BLEND_HARDLIGHT number

Default Value: 20

BLEND_SOFTLIGHT number

Default Value: 21

BLEND_DIFFERENCE number

Default Value: 22

BLEND_EXCLUSION number

Default Value: 23

Methods

Name Return Value Summary
blendMode object

Sets the blending mode.

clearRect object

Clears a region of a canvas with transparent black.

color object

Sets the stroke and fill color to use.

fillEllipse object
fillPath object
fillRect object
fillText object
font object

Sets the font to use for CanvasCommand.fillText.

hints object

Sets rendering hints.

imageCopy object
line object
point object
strokeArc object
strokeEllipse object
strokePath object
strokeRect object
strokeWidth object

Sets the line width for stroked shapes.

Method Details

(static) blendMode( mode ) → {object}
Returns: object

Sets the blending mode.

Parameters

Name Type Description
mode number
(static) clearRect( x, x, w, h ) → {object}
Returns: object

Clears a region of a canvas with transparent black.

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

(static) color( color ) → {object}
Returns: object

Sets the stroke and fill color to use.

Parameters

Name Type Description
color Color | Vec4

Stroke and fill color to use.

(static) fillEllipse( x, x, w, h ) → {object}
Returns: object

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

(static) fillPath( path ) → {object}
Returns: object

Parameters

Name Type Description
path Array.<CanvasPathElement>
(static) fillRect( x, x, w, h ) → {object}
Returns: object

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

(static) fillText( text, x, x, w, h, flags ) → {object}
Returns: object

Parameters

Name Type Description
text string
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

flags number
(static) font( family, size, weight, italic ) → {object}
Returns: object

Sets the font to use for CanvasCommand.fillText.

Parameters

Name Type Default Value Description
family string
size number 16

Pixel height

weight number 400

400 is normal, 700 is bold

italic boolean false
(static) hints( hints ) → {object}
Returns: object

Sets rendering hints.

Parameters

Name Type Description
hints number
(static) imageCopy( image, srcRect, destRect ) → {object}
Returns: object

Parameters

Name Type Description
image CanvasImage
srcRect Vec4 | Rect
destRect Vec4 | Rect
(static) line( x1, y1, x2, y2 ) → {object}
Returns: object

Parameters

Name Type Description
x1 number
y1 number
x2 number
y2 number
(static) point( x, y ) → {object}
Returns: object

Parameters

Name Type Description
x number
y number
(static) strokeArc( x, x, w, h, startAngle, spanAngle ) → {object}
Returns: object

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

startAngle number
spanAngle number
(static) strokeEllipse( x, x, w, h ) → {object}
Returns: object

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

(static) strokePath( path ) → {object}
Returns: object

Parameters

Name Type Description
path Array.<CanvasPathElement>
(static) strokeRect( x, x, w, h ) → {object}
Returns: object

Parameters

Name Type Description
x number

Left of the rectangle.

x number

Top of the rectangle.

w number

Width of the rectangle.

h number

Height of the rectangle.

(static) strokeWidth( width ) → {object}
Returns: object

Sets the line width for stroked shapes.

Parameters

Name Type Description
width number

Stroke width in pixels.