Canvas
The Canvas APIs provide interfaces for creating canvas objects, drawing graphics, and rendering content on screen. Use createCanvas to obtain a canvas, then call getContext to retrieve a 2D or WebGL rendering context for drawing.
TTMinis.game.createCanvas()
Creates a canvas object. The first call creates the canvas displayed on the screen. Subsequent calls create off-screen canvases.
Returns
Canvas object:
Field | Type | Description |
|
| The width of the canvas |
|
| The height of the canvas |
|
| Gets the rendering context of the canvas object |
|
| Returns the drawing content on the canvas as a |
Example
const canvas = TTMinis.game.createCanvas();
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);TTMinis.game.createImage()
Creates an image object.
Returns
Image object:
Field | Type | Description |
|
| The image URL, base64 content, or ArrayBuffer data |
|
| The actual width of the image |
|
| The actual height of the image |
|
| Adds an event listener |
|
| Removes an event listener |
Example
const img = TTMinis.game.createImage();
img.addEventListener('load', () => {
console.log('Image loaded', img.width, img.height);
});
img.src = 'https://example.com/image.png';Canvas.getContext(contextType)
Gets the rendering context of the canvas object.
Parameters
Field | Type | Description | Required |
|
| The context type. Valid values are | Yes |
Returns
The context object of the canvas. The type depends on the contextType parameter (CanvasRenderingContext2D for 2d, WebGLRenderingContext for webgl).
Example
const canvas = TTMinis.game.createCanvas();
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 50, 50);Canvas.toDataURL()
Returns the drawing content on the canvas as a data URI string.
Returns
string - A data URI string of the canvas content.
Example