Canvas
Canvas is a widget that can be used to draw graphics using a canvas context.
Import this type with “”
Example:
Parameters:
- width: number
- the width of the canvas context to create
- height: number
- the height of the canvas context to create
Returns:
- the height of the canvas context to create
const {Canvas, ui, device} = require('tabris');
// Draw shapes on a canvas using HTML5 Canvas API
new Canvas({
left: 10, top: 10, right: 10, bottom: 10
}).on('resize', ({target: canvas, width, height}) => {
let scaleFactor = device.scaleFactor;
ctx.scale(scaleFactor, scaleFactor);
ctx.strokeStyle = 'rgb(78, 154, 217)';
ctx.moveTo(20, 20);
ctx.lineTo(width - 40, height - 40);
ctx.stroke();
// draw image
ctx.putImageData(createImageData(80, 40), 100, 100);
// copy region
let data = ctx.getImageData(0, 0, 100, 100);
ctx.putImageData(data, 180, 100);
function createImageData(width, height) {
let array = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let alpha = x % 20 > y % 20 ? 255 : 0;
array.push(200, 0, 0, alpha);
}
}
return new ImageData(new Uint8ClampedArray(array), width, height);
}
原文: