liveBook · Manning

Canvas element

As explained in Web Components in Action

The HTML canvas element is a scriptable bitmap drawing surface in a web document for rendering graphics.

Need somewhere to draw a 3D scene or alter a live camera feed? A canvas element provides a drawing surface in the page. JavaScript can render graphics into it, while a Web Component can keep the canvas—and much of the rendering work—out of the surrounding page markup. section 15.2 section 15.3

The surface plays two different roles in these examples:

  • For a 3D scene, a component template contains `<canvas touch-action="none"></canvas>`, selects it with `querySelector('canvas')`, and sizes it to fill the component. That selected element is passed to `BABYLON.Engine`; the engine then runs a render loop. section 15.2
  • For live video, `useCanvasForDisplay` switches a video component to its internal canvas. `canvasRefreshInterval="50"` requests a refresh every 50 milliseconds: a much longer interval looks choppy, while an overly short one can strain the browser. section 15.3

Change the video pixels

Start with a camera feed displayed through the canvas. A black-and-white filter can be assigned as the component’s `canvasFilter`. For a custom “snow” effect, request `frameDataMode="imagedata"` and loop through the frame data four values at a time: red, green, blue, and alpha. Randomizing some color values adds speckles; a range input controls how many pixels change. section 15.3

Listing 15.11. HTML/CSS for the 3D scene component
export default {
   render() {
       return `${this.css()}
               ${this.html()}`;
   },

   mapDOM(scope) {
       return {
           scene: scope.querySelector('canvas')         #1
       }
   },

   html() {
       return `<canvas touch-action="none">             #2
                  </canvas>`;
   },

   css() {
       return `<style>                                  #3
                 :host {
                   display: inline-block;
                   width: 100%;
                   height: 100%;
                 }
                 canvas {
                   width: 100%;
                   height: 100%;
                 }
               </style>`;
   }
}

See how a scene component places, selects, and sizes its canvas before any 3D engine setup. section 15.2

You need not write every shader or graphics operation yourself. Babylon.js handles the 3D scene example, and video components expose filters and frame data. The canvas remains the drawing surface; the library or component determines how you work with it. section 15.2 section 15.3

How Web Components in Action covers it

How deep it goes
Shows canvas placement, engine setup, refresh timing, and pixel filtering, but leaves low-level drawing largely to libraries and components.
Examples
Uses a Babylon.js 3D scene and live-camera black-and-white and “snow” filters.
What it assumes
Assumes working HTML and JavaScript familiarity; approaches canvas through component examples rather than teaching browser drawing APIs from zero.
What you can do after
Apply it via Babylon.js or video components to render a scene or alter displayed camera frames.
How it connects
Connects canvas to Web Components, WebGL, shaders, Babylon.js, video frames, and pixel data.

Also explained in

Compare how 4 books explain canvas element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.