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:
  1. const {Canvas, ui, device} = require('tabris');
  2. // Draw shapes on a canvas using HTML5 Canvas API
  3. new Canvas({
  4. left: 10, top: 10, right: 10, bottom: 10
  5. }).on('resize', ({target: canvas, width, height}) => {
  6. let scaleFactor = device.scaleFactor;
  7. ctx.scale(scaleFactor, scaleFactor);
  8. ctx.strokeStyle = 'rgb(78, 154, 217)';
  9. ctx.moveTo(20, 20);
  10. ctx.lineTo(width - 40, height - 40);
  11. ctx.stroke();
  12. // draw image
  13. ctx.putImageData(createImageData(80, 40), 100, 100);
  14. // copy region
  15. let data = ctx.getImageData(0, 0, 100, 100);
  16. ctx.putImageData(data, 180, 100);
  17. function createImageData(width, height) {
  18. let array = [];
  19. for (let y = 0; y < height; y++) {
  20. for (let x = 0; x < width; x++) {
  21. let alpha = x % 20 > y % 20 ? 255 : 0;
  22. array.push(200, 0, 0, alpha);
  23. }
  24. }
  25. return new ImageData(new Uint8ClampedArray(array), width, height);
  26. }

原文: