Why Declarativas?

Comparisons

Summary

Here I will show a comparison between raw canvas, declarativas without jsx, and with jsx, using a function that will render a rotating 20x20 square centered on 100,100 in the canvas.

Declarativas Goals

One thing to note is declarativas isn't meant to make canvas programs shorter. While custom components can be made to manage multiple canvas calls, the main goal is to make canvas more approachable to the React generation of javascript developers. Thinking about canvas in terms of components makes things easier to manage, and using a virtual canvas operations (similar to virtual dom), it is far easier to test your canvas application and have predictable output, as well as build logical hierarchies.

Raw Canvas

const ctx = document.querySelector('canvas').getContext('2d');

const render = (lastTimestamp, angle) => (timestamp) => {
  const delta = lastTimestamp
    ? (timestamp - lastTimestamp) / 1000
    : 0;

  ctx.save();
  
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  
  ctx.translate(100, 100);
  ctx.rotate(angle * Math.PI / 180);
  
  ctx.fillStyle = 'blue';
  ctx.fillRect(-10, -10, 20, 20);
  
  ctx.restore();
  
  requestAnimationFrame(render(timestamp, angle + (10 * delta)));
}

requestAnimationFrame(render(null, 0));

Pros

  • No extra learning on top of the canvas API

  • Shortest program

Cons

  • No meaningful relationship between calls

    • save does not imply a later restore

    • A lack of hierarchy makes it non-obvious what mutations to the canvas state require the save and restore

  • No community consensus on how to build drawing components

  • Arguments to some functions are non-obvious

Declarativas without JSX

import { render, c, components } from 'declarativas';
const { RevertableState, Rect } = components;

const ctx = document.querySelector('canvas').getContext('2d');

const render = (lastTimestamp, angle) => (timestamp) => {
  const delta = lastTimestamp
    ? (timestamp - lastTimestamp) / 1000
    : 0;
    
  render(
    ctx,
    [
      c('clearRect', { x: 0, y: 0, width: ctx.canvas.width, ctx.canvas.height }),
      c(RevertableState, {}, [
        c('translate', { x: 100, y: 100 }),
        c('rotate', { value: angle * Math.PI / 180 }),
        
        c(Rect, { x: -10, y: -10, width: 20, height: 20, fill: 'blue' }),
      ]),
    ]
  );
  
  requestAnimationFrame(render(timestamp, angle + (10 * delta)));
}

requestAnimationFrame(render(null, 0));

Pros

  • Hierarchy of drawing operations

  • Clear distinction of drawing code from logic

  • Named parameters

  • Declarative components can make the code easier to debug and test

Cons

  • The c function can look awkward

  • More code

  • Adds dependency

  • Could feel foreign

Declarativas with JSX

/** @jsx c */
import { render, c, components } from 'declarativas';
const { RevertableState, Rect } = components;

const ctx = document.querySelector('canvas').getContext('2d');

const render = (lastTimestamp, angle) => (timestamp) => {
  const delta = lastTimestamp
    ? (timestamp - lastTimestamp) / 1000
    : 0;
    
  render(
    ctx,
    <g>
      <clearRect x={0} y={0} width={ctx.canvas.width} height={ctx.canvas.height} />
      <RevertableState>
        <translate x={100} y={100} />
        <rotate value={angle * Math.PI / 180} />
        
        <Rect x={-10} y={-10} width={20} height={20} fill="blue />
      </RevertableState>
    </g>
  );
  
  requestAnimationFrame(render(timestamp, angle + (10 * delta)));
}

requestAnimationFrame(render(null, 0));

Pros

  • Hierarchy of drawing operations

  • Clear distinction of drawing code from logic

  • Named parameters

  • Declarative components can make the code easier to debug and test

  • Higher familiarity to the code (from the perspective of React) because of the JSX transform used for operations

Cons

  • More code

  • Adds dependency

  • Could feel foreign

TL;DR

If you want a very lean bare-metal canvas experience, there is nothing distinctly wrong with the javascript API for CanvasContext2d. If you want a more manageable code-base that uses a lot of canvas, I do think declarativas has a well defined use. I personally enjoy the way I can draw with declarativas, and hope you would try it and see the difference.

Last updated