two.js is a free, open source design & prototyping project written in JavaScript and released under MIT. It has 8,662 GitHub stars, 463 forks and 42 open issues, and was last pushed 7 hours ago. On this registry it ranks #29 of 59 tracked projects in Design & Prototyping, with 5 head-to-head comparisons available.

What is two.js?

Two.js is a renderer-agnostic two-dimensional drawing API for the web, published as the npm package two.js under the MIT licence, for JavaScript developers who want one API to draw and animate the same scene in WebGL, Canvas2D, or SVG.

What it is

Two.js is a JavaScript drawing library for modern browsers whose defining property is renderer agnosticism: the same API can render in multiple contexts — webgl, canvas2d, and svg. Scenes are built as a scenegraph of shapes created with methods such as two.makeRectangle, and animation is driven by an update event bound with two.bind('update', ...). It ships on npm as two.js and as a downloadable minified build, and since v0.7.5 it supports ES6 imports for use with React, Angular, and bundlers such as webpack, esbuild, and gulp.

The problem it addresses is fragmentation across rendering contexts: WebGL, Canvas2D, and SVG each expose a different drawing model, so code written for one does not run against another. Two.js replaces that renderer-specific drawing code with a single API surface, letting an application select or change its backend without rewriting the drawing layer; since v0.7.x it can also run headlessly on a server through Node Canvas.

Key capabilities

  • A single API that renders through webgl, canvas2d, and svg, with the renderer swapped while the scene code stays the same.
  • A scenegraph of shapes built with calls such as two.makeRectangle(x, y, width, height), whose properties like rotation can change on every frame.
  • An animation loop driven by two.bind('update', fn), configured with options such as { fullscreen: true, autostart: true } and attached with appendTo(document.body).
  • ES6 support since v0.7.5+, including import Two from "two.js" and direct module imports such as import { Vector } from 'two.js/src/vector.js'.
  • Headless rendering since v0.7.x using Node Canvas, which is deliberately not a dependency of Two.js.
  • Custom builds that drop unused renderers: edit /utils/build.js, run node ./utils/build, and /build/two.js and /build/two.min.js are regenerated.

Who uses it and how

  • Front-end developers building animated 2D graphics in modern browsers, creating a Two instance and updating shape properties on the update event.
  • React and Angular applications using the documented ES6 setup, including cleanup that unbinds the update handler, calls two.pause(), and removes two.renderer.domElement.
  • Projects bundling with webpack, esbuild, or gulp that import either the Two namespace or individual modules from two.js/src/.
  • Server-side and cloud workflows rendering without a browser by running Two.js headlessly with Node Canvas, plus teams trimming payload size through custom builds such as a SVGRenderer-only bundle.

Getting started

Install it with npm install --save two.js, or download the latest minified library and include it in an HTML page; the boilerplate creates new Two({ fullscreen: true, autostart: true }).appendTo(document.body). Building from source requires Node.js, an npm install in the repository, and node ./utils/build.

How it compares

The facts list no paid products that Two.js replaces, and no directly comparable drawing library is named either; the adjacent tools mentioned — React, Angular, webpack, esbuild, gulp, and Node Canvas — are frameworks, bundlers, or a rendering dependency rather than substitutes. It stands alone in this registry as a renderer-agnostic 2D drawing API.

When to use it — and when not to

Two.js is a library rather than a hosted service, so there is no database, storage, or SMTP to operate; the only operational extra is the optional server-side path, which requires installing Node Canvas separately because Two.js does not depend on it. The README states that proper tree shaking is not yet implemented and that the main Two namespace import pulls in all modules, so teams with strict bundle budgets should use custom builds or expect to carry unused renderers. Anyone who needs only one rendering backend and no portability across WebGL, Canvas2D, and SVG will not benefit much from the library's central feature.

project readme (upstream, from github) — read inline

Two.js

OpenSSF Scorecard NPM Package Build Size NPM Downloads

A two-dimensional drawing api meant for modern browsers. It is renderer agnostic enabling the same api to render in multiple contexts: webgl, canvas2d, and svg.

Home • Releases • Examples • Documentation • Changelog • Help

Usage

Download the latest minified library and include it in your html.

<script src="js/two.min.js"></script>

It can also be installed via npm, Node Package Manager:

npm install --save two.js

Alternatively see how to build the library yourself.

Here is boilerplate html in order to draw a spinning rectangle in two.js:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="js/two.min.js"></script>
  </head>
  <body>
    <script>
      var two = new Two({
        fullscreen: true,
        autostart: true
      }).appendTo(document.body);
      var rect = two.makeRectangle(two.width / 2, two.height / 2, 50 ,50);
      two.bind('update', function() {
        rect.rotation += 0.001;
      });
    </script>
  </body>
</html>

Custom Build

Two.js uses nodejs in order to build source files. You'll first want to install that. Once installed open up a terminal and head to the repository folder:

cd ~/path-to-repo/two.js
npm install

This will give you a number of libraries that the development of Two.js relies on. If for instance you only use the SVGRenderer then you can really cut down on the file size by excluding the other renderers. To do this, modify /utils/build.js to only add the files you'd like. Then run:

node ./utils/build

And the resulting /build/two.js and /build/two.min.js will be updated to your specification.


Using ES6 Imports

As of version v0.7.5+ Two.js is compatible with EcmaScript 6 imports. This is typically employed in contemporary frameworks like React and Angular as well as bundling libraries like webpack, esbuild, and gulp. This adaptation of the boilerplate can be found on CodeSandbox:

import React, { useEffect, useRef } from "react";
import Two from "two.js";

export default function App() {
  var domElement = useRef();

  useEffect(setup, []);

  function setup() {
    var two = new Two({
      fullscreen: true,
      autostart: true
    }).appendTo(domElement.current);

    var rect = two.makeRectangle(two.width / 2, two.height / 2, 50, 50);
    two.bind("update", update);

    return unmount;

    function unmount() {
      two.unbind("update");
      two.pause();
      domElement.current.removeChild(two.renderer.domElement);
    }

    function update() {
      rect.rotation += 0.001;
    }
  }

  return <div ref={domElement} />;
}

In addition to importing, the published packages of Two.js include the specific modules. So, if necessary you can import specific modules from the source code and bundle / minify for yourself like so:

import { Vector } from 'two.js/src/vector.js';

// In TypeScript environments leave out the ".js"
// when importing modules directly. e.g:
import { Vector } from 'two.js/src/vector';

While useful, the main import of the Two namespace imports all modules. So, there isn't yet proper tree shaking implemented for the library, though it's on the roadmap.

Running in Headless Environments

As of version v0.7.x Two.js can also run in a headless environment, namely running on the server with the help of a library called Node Canvas. We don't add Node Canvas to dependencies of Two.js because it's not necessary to run it in the browser. However, it has all the hooks set up to run in a cloud environment. To get started follow the installation instructions on Automattic's readme. After you've done that run:

npm install canvas
npm install two.js

Now in a JavaScript file set up your Two.js scenegraph and save out frames whenever you need to:

var { createCanvas, Image } = require('canvas');
var Two = require('two.js')

var fs = require('fs');
var path = require('path');

var width = 800;
var height = 600;

var canvas = createCanvas(width, height);
Two.Utils.polyfill(canvas, Image);

var time = Date.now();

var two = new Two({
  width: width,
  height: height,
  domElement: canvas
});

var rect = two.makeRectangle(width / 2, height / 2, 50, 50);
rect.fill = 'rgb(255, 100, 100)';
rect.noStroke();

two.render();

var settings = { compressionLevel: 3, filters: canvas.PNG_FILTER_NONE };
fs.writeFileSync(path.resolve(__dirname, './images/rectangle.png'), canvas.toBuffer('image/png', settings));
console.log('Finished rendering. Time took: ', Date.now() - time);

process.exit();

Build Documentation

The Two.js website is bundled with this repository. Relying on Vuepress the repository generates a website based on numerous README.md files housed in the wiki directory. Use the following the node commands as follows:

npm run docs:generate   // Generate README.md files for documentation from source code comments
npm run docs:dev        // Creates a local server to generate all documentation
npm run docs:build      // Builds out static site and associated files to wiki/.vuepress/dist

The documentation toolchain uses Node 24 LTS. Run nvm use before working on the docs site, or install the version listed in .nvmrc if you do not use Node Version Manager.

Change Log

Two.js has been in operation since 2012. For a full list of changes from its first alpha version built with Three.js to the most up-to-date tweaks. Check out the wiki here.


And a big thank you to our sponsors who include:

Epilogue Press

Frequently asked questions

Is two.js free to use?

two.js is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does two.js do?

A renderer agnostic two-dimensional drawing api for the web

What is two.js written in?

two.js is primarily written in JavaScript. Its source is publicly available at https://github.com/jonobr1/two.js, and it has 8,662 GitHub stars.