• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal Three.js Bootstrapper

threestrap

Use Three.js with zero hassle.

Example


Threestrap is a minimal, pluggable bootstrapper for Three.js that gets out of your way.

While this is definitely a miniature framework, it's not really meant to wrap your code, but rather the code you don't care about.

Examples:

Usage

Install via npm:

npm install threestrap

Add build/threestrap.js to your Three.js:

<!-- 
  Or use CDNs like
  https://cdn.jsdelivr.net/npm/[email protected]/build/three.js
 -->
<script src="three.js"></script>
<script src="threestrap.js"></script>

Get a threestrap context:

const three = new Threestrap.Bootstrap();

This will create a full-page Three.js WebGL canvas, initialize the scene and camera, and set up a rendering loop.

You can access the globals three.scene and three.camera, and bind events on the three context:

// Insert a cube
const mesh = new THREE.Mesh(
  new THREE.CubeGeometry(0.5, 0.5, 0.5),
  new THREE.MeshNormalMaterial()
);
three.scene.add(mesh);

// Orbit the camera
three.on("update", function () {
  var t = three.Time.now;
  three.camera.position.set(Math.cos(t), 0.5, Math.sin(t));
  three.camera.lookAt(new THREE.Vector3());
});

Configuration

Threestrap is made out of plugins that each do one thing. The basic set up of empty or core gets you a fully-functional canvas in the body or a specific DOM element.

Empty:

  • fallback - Displays a standard message with a link if WebGL is unavailable.
  • bind - Enables event/method binding.
  • renderer - Creates the THREE.WebGLRenderer (or a given class).
  • size - Autosizes canvas to fit or size to given dimensions.
  • fill - Removes margin/padding and sets positioning on the element.
  • loop - Runs the rendering loop.
  • time - Measures time and fps in seconds. Provides clocks.

Core:

  • scene - Creates the THREE.Scene
  • camera - Creates the THREE.Camera
  • render - Renders the global scene and camera directly.
  • warmup - Hide canvas for first few frames to avoid stuttering.

Additional plug-ins can be added, or the default set can be overridden on a case by case basis. empty is a do-it-yourself set up.

Shorthands:

// Core only
var three = new Threestrap.Bootstrap();

// Pass in list of plugins
var three = new Threestrap.Bootstrap("core", "stats");
var three = new Threestrap.Bootstrap(["core", "stats"]);

// Insert into specific element
var three = new Threestrap.Bootstrap(element);
var three = new Threestrap.Bootstrap(element, "core", "stats");
var three = new Threestrap.Bootstrap(element, ["core", "stats"]);

// Replace plugins ad-hoc
var three = new Threestrap.Bootstrap(["core", "stats", "render:myRender"]);

The following global options are available with these defaults:

var three = new Threestrap.Bootstrap({
  init: true, // Initialize on creation

  element: document.body, // Containing element

  plugins: [
    // Active plugins
    "core", // Use all core plugins
    // 'render:myRender'           // Ad-hoc overrides
  ],

  aliases: {
    // Ad-hoc overrides
    // 'render': 'myRender',
    // 'alias': ['myFoo', 'myBar']
  },
});

When init is set to false, initialization only happens when manually calling three.init(). To destroy the widget, call three.destroy().

Plugins can make objects and methods available on the threestrap context, like three.Time.now or three.Loop.start().

Plugins

To enable a plug-in, include its name in the plugins field. Plugins are installed in the given order.

Plug-in specific options are grouped under the plug-in's name:

const three = new Threestrap.Bootstrap({
  plugins: ["core", "stats"],
  size: {
    width: 1280,
    height: 720,
  },
  camera: {
    fov: 40,
  },
});

The following aliases are available:

  • empty = fallback, bind, renderer, size, fill, loop, time
  • core = empty + scene, camera, render, warmup

Events

Threestrap plugins broadcast events to each other, like resize or render.

You can listen for events with .on() and unset them with .off().

three.on("event", function (event, three) {});
var handler = function () {};
three.on("event", handler);
three.off("event", handler);

You can also bind events directly to object methods using .bind:

const object = {
  render: function (event, three) {},
  yup: function (event, three) {},
  redraw: function (event, three) {},
  resize: function (event, three) {},
  change: function (event, three) {},
};

// Bind three.render event to object.render(event, three)
three.bind("render", object);

// Bind three.ready event to object.yup(event, three);
three.bind("ready:yup", object);

// Bind object.change event to object.redraw(event, three);
three.bind("this.change:redraw", object);

// Bind window.resize event to object.resize(event, three);
three.bind("window.resize", object);

// Bind DOM element's onchange event to object.change(event, three);
three.bind([element, "change"], object);

Docs


Steven Wittens - http://acko.net/

More Repositories

1

TermKit

Experimental Terminal platform built on WebKit + node.js. Currently only for Mac and Windows, though the prototype works 90% in any WebKit browser.
JavaScript
4,433
star
2

MathBox.js

MathBox is a (work in progress) library for making presentation-quality math diagrams in WebGL.
JavaScript
1,942
star
3

mathbox

Presentation-quality WebGL math graphing
JavaScript
1,328
star
4

shadergraph

Functional GLSL Linker
JavaScript
721
star
5

ThreeAudio.js

ThreeAudio helps you create music visualizations in Three.js or tQuery.
JavaScript
531
star
6

CSS3D.js

CSS 3D renderer for Three.js.
JavaScript
366
star
7

fullfrontal

MathBox-based conference talks
JavaScript
339
star
8

fuse10

Front-end for Acko.net 2013
JavaScript
246
star
9

console-extras.js

Enhancements to the JavaScript console object.
JavaScript
127
star
10

NFSpace

Procedural planet generator
C
123
star
11

ShaderGraph.js

(deprecated) Library to build GLSL shaders out of reusable building blocks.
JavaScript
123
star
12

ThreeRTT.js

ThreeRTT helps you create advanced render-to-texture effects in Three.js.
JavaScript
73
star
13

NeverSeenTheSky

Source code for the WebGL demo Never Seen The Sky
JavaScript
53
star
14

iremotepipe

Command-line tool that streams button presses from Apple IR remotes on OS X.
Objective-C
48
star
15

imgui-wgpu-rs

Dear imgui renderer for wgpu-rs.
Rust
29
star
16

DarkSunrise

Music visualizer demo for Christmas Experiments 2013
JavaScript
26
star
17

ThreeBox.js

(dead) ThreeBox provides an improved boilerplate set up for tQuery / Three.js.
JavaScript
23
star
18

jsFsck

Makes Douglas Crockford cry and gives JavaScript debuggers nightmares.
JavaScript
21
star
19

headspace

TypeScript
12
star
20

pixelfactory

MathBox2-based talk about Pixels, MathBox and GPUs.
JavaScript
12
star
21

WikiTLDR

Chrome/Safari Extension. Reformats WikiLeaks cables for readability.
JavaScript
11
star
22

uncolorblind

Uncolor blindness goggles (Web / JS / GLSL)
JavaScript
11
star
23

vscode-headspace

Headspace audio VSCode plugin
JavaScript
4
star