canvas.drawText
canvas.drawText(text, attrs)
Draw a string within the canvas directed by the user-specified properties.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | The text string to display. |
attrs | object | An object of properties containing information about how to draw the text. See Draw Text Options below. |
Draw Text Options
| Key | Type | Description |
|---|---|---|
font | string | The font to use to render the text (required, no default). |
x | number | The x position of the upper left corner of the image (default: 0). |
y | number | The y position of the upper left corner of the image (default: 0). |
size | number | The point size to render the text at (default: 18). |
color | number | The color to render the text in (default: black: 0x000000). |
rotation | number | The angle in degrees to render the text at (default: 0). |
Example
// Draw a hello world string centered on the canvas
function DrawCenteredText(name) {
var msg = "Hello World";
var canvas = sb.getCanvas(name);
var attrs = {font : "fonts/RobotoBold.ttf", size : 24, x : 0, y : 0};
var strSize = sb.getStringSize(attrs.font, attrs.size, msg, 0);
attrs.x = (canvas.width - strSize.width) / 2;
attrs.y = (canvas.height - strSize.height) / 2;
canvas.drawText("Hello World", attrs);
}