two.js

A renderer agnostic two-dimensional drawing api for the web.

README

Two.js

[![NPM Package][npm]][npm-url] [![Build Size][build-size]][build-size-url] [![NPM Downloads][npm-downloads]][npmtrends-url] [![Language Grade][lgtm]][lgtm-url]

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.


Usage

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

  1. ``` html
  2. <script src="js/two.min.js"></script>
  3. ```

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

  1. ``` js
  2. npm install --save two.js
  3. ```


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

  1. ``` html
  2. <!doctype html>
  3. <html>
  4.   <head>
  5.     <meta charset="utf-8">
  6.     <script src="js/two.min.js"></script>
  7.   </head>
  8.   <body>
  9.     <script>
  10.       var two = new Two({
  11.         fullscreen: true,
  12.         autostart: true
  13.       }).appendTo(document.body);
  14.       var rect = two.makeRectangle(two.width / 2, two.height / 2, 50 ,50);
  15.       two.bind('update', function() {
  16.         rect.rotation += 0.001;
  17.       });
  18.     </script>
  19.   </body>
  20. </html>
  21. ```

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:

  1. ```
  2. cd ~/path-to-repo/two.js
  3. npm install
  4. ```

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:

  1. ```
  2. node ./utils/build
  3. ```

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:

  1. ``` js
  2. import React, { useEffect, useRef } from "react";
  3. import Two from "two.js";

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

  6.   useEffect(setup, []);

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

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

  14.     return unmount;

  15.     function unmount() {
  16.       two.unbind("update");
  17.       two.pause();
  18.       domElement.current.removeChild(two.renderer.domElement);
  19.     }

  20.     function update() {
  21.       rect.rotation += 0.001;
  22.     }
  23.   }

  24.   return <div ref={domElement} />;
  25. }
  26. ```

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:

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

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

_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:

  1. ```
  2. npm install canvas
  3. npm install two.js
  4. ```

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

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

  4. var fs = require('fs');
  5. var path = require('path');

  6. var width = 800;
  7. var height = 600;

  8. var canvas = createCanvas(width, height);
  9. Two.Utils.shim(canvas, Image);

  10. var time = Date.now();

  11. var two = new Two({
  12.   width: width,
  13.   height: height,
  14.   domElement: canvas
  15. });

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

  19. two.render();

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

  23. process.exit();

  24. ```

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:


[npm]: https://img.shields.io/npm/v/two.js
[npm-url]: https://www.npmjs.com/package/two.js
[build-size]: https://badgen.net/bundlephobia/minzip/two.js
[build-size-url]: https://bundlephobia.com/result?p=two.js
[npm-downloads]: https://img.shields.io/npm/dm/two.js
[npmtrends-url]: https://www.npmtrends.com/two.js
[lgtm]: https://img.shields.io/lgtm/alerts/github/jonobr1/two.js
[lgtm-url]: https://lgtm.com/projects/g/jonobr1/two.js/