ng-canvas

Own native canvas sizing, drawing schedules, and view lifecycle.

ng-canvas connects a native Canvas 2D or WebGL renderer to an AngularTS view. Install the optional runtime module and declare a renderer on the canvas:

import { createAngular } from '@angular-wave/angular.ts/runtime';
import { canvasModule } from '@angular-wave/angular.ts/runtime/canvas';

const angular = createAngular({ modules: [canvasModule] });

Add your normal template, event, and model directives to a custom runtime as needed. With the full runtime, call canvasModule(angular) and include ng.canvas in your application’s module dependencies.

<canvas
  ng-canvas="chart.renderer"
  ng-canvas-watch="chart.model.value"
  aria-label="Value chart"
></canvas>

The renderer implements the documented CanvasRenderer contract. mount(surface) receives the native canvas, context, and an invalidate() callback. Required render(frame) receives seconds-based time and delta plus the current CSS and backing-store dimensions.

Optional hooks are resize(size), suspend(), resume(), and dispose(). Set context to 2d (the default), webgl, or webgl2. animate: true opts into continuous frames; the default renders only after mounting, resizing, ng-canvas-watch changes, or explicit surface.invalidate() calls. Repeated invalidations coalesce into one animation frame. Set maxPixelRatio to a positive finite number to limit backing-store resolution.

Sizing and drawing

Give the canvas stable CSS dimensions or an explicit CSS aspect ratio. The directive measures the canvas border box and owns its backing-store width and height; application code owns drawing coordinates and context transforms. For exact drawable dimensions, put borders and padding on a wrapper.

Changing backing-store dimensions resets native drawing state. Use resize or render to restore transforms, styles, and WebGL viewport state. Zero-sized canvases skip rendering. ResizeObserver and window resize events request redraws. Continuous renderers also detect pixel-ratio changes each frame.

Keep contexts, GPU resources, and audio handles in renderer closures. Mark a renderer with $nonscope: true if it holds native resources or mutable runtime internals; keep meaningful display state in reactive models.

Lifecycle and failures

Replacing the renderer disposes the previous one. View retention with pause: 'schedulers' cancels pending frames and calls suspend; resume calls resume, requests a frame, and resets delta to zero. Scope destruction cancels frames, disconnects observers, removes listeners, and calls dispose.

Initialization and drawing failures stop the renderer and report through $exceptionHandler. Unavailable contexts fail explicitly. The directive supplies no rendering engine or context fallback. WebGL applications own GPU context-loss recovery through their renderer and normal event directives.

This module is separate from the experimental HTML-in-Canvas module. It is excluded from the default runtime and requires no experimental browser APIs.

Examples

The chart below uses redraw-on-change. The repository’s concepts/flappy-bird/ uses continuous frames, a reactive session machine, and an injectable game service. concepts/canvas/ provides a second redraw-on-change example with position and color controls.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>AngularTS canvas chart</title>
    <script
      type="module"
      src="/utils/source-bootstrap.js"
      data-angular-ts-entry="./chart.js"
    ></script>
    <style>
      body {
        font: 16px sans-serif;
        margin: 24px;
      }
      canvas {
        display: block;
        width: min(480px, 100%);
        height: 180px;
        margin-top: 16px;
      }
    </style>
  </head>
  <body ng-app="canvasChart" ng-controller="ChartController as chart">
    <label
      >Value
      <input
        aria-label="Value"
        type="range"
        min="0"
        max="100"
        ng-model="chart.model.value"
    /></label>
    <output data-testid="chart-value">{{ chart.model.value }}</output>
    <canvas
      ng-canvas="chart.renderer"
      ng-canvas-watch="chart.model.value"
      aria-label="Value chart"
    ></canvas>
  </body>
</html>
AngularTS canvas chart {{ chart.model.value }}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>AngularTS canvas chart</title>
    <script
      type="module"
      src="/utils/source-bootstrap.js"
      data-angular-ts-entry="./chart.js"
    ></script>
    <style>
      body {
        font: 16px sans-serif;
        margin: 24px;
      }
      canvas {
        display: block;
        width: min(480px, 100%);
        height: 180px;
        margin-top: 16px;
      }
    </style>
  </head>
  <body ng-app="canvasChart" ng-controller="ChartController as chart">
    <label
      >Value
      <input
        aria-label="Value"
        type="range"
        min="0"
        max="100"
        ng-model="chart.model.value"
    /></label>
    <output data-testid="chart-value">{{ chart.model.value }}</output>
    <canvas
      ng-canvas="chart.renderer"
      ng-canvas-watch="chart.model.value"
      aria-label="Value chart"
    ></canvas>
  </body>
</html>
import { canvasModule } from '/src/runtime/canvas.ts';

canvasModule(window.angular);
window.angular
  .createModule('canvasChart', ['ng.canvas'])
  .model('chartModel', () => ({ value: 60 }))
  .controller('ChartController', [
    'chartModel',
    class {
      constructor(model) {
        this.model = model;
        let context;
        this.renderer = {
          $nonscope: true,
          mount: (surface) => {
            context = surface.context;
          },
          render: ({ size }) => {
            context.setTransform(size.pixelRatio, 0, 0, size.pixelRatio, 0, 0);
            context.clearRect(0, 0, size.width, size.height);
            context.fillStyle = '#e8eef4';
            context.fillRect(0, 40, size.width, 80);
            context.fillStyle = '#2f80ed';
            context.fillRect(
              0,
              40,
              (size.width * Number(model.value)) / 100,
              80,
            );
          },
        };
      }
    },
  ]);