• Stars
    star
    247
  • Rank 163,130 (Top 4 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

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

Repository Details

A three.js camera toolkit for creating interactive 3d stories

Three Story Controls

A three.js camera toolkit for creating interactive 3d stories.

โ€ข Flexible camera rig API
โ€ข Visual tool for designing camera animations
โ€ข Collection of camera control schemes
โ€ข Helper components to wire smoothed inputs to camera actions for custom control schemes.

License Build status

Demos โ€” Usage โ€” Installation โ€” API Docs โ€” Contributing

Components:
Camera Rig โ€” Camera Helper โ€” Control Schemes โ€” Input Adaptors
Building your own control scheme



Demos

  • FreeMovement controls: First-person controls to move freely around the scene.
  • Scroll + 3DOF controls: Scroll through the page to scrub through a camera animation. Slightly rotate the camera with mouse movements.
  • StoryPoint + 3DOF controls: Transition between specific points in the scene. Slightly rotate the camera with mouse movements.
  • PathPoint controls: Transition between specific frames of a camera animation.
  • Camera Helper: Helper tool to create camera animations and/or points of interest that can be exported and used by the control schemes.



Usage

Here is an example of the FreeMovementControls scheme, where camera translation is controlled by arrow keys or the mouse wheel, and rotation by clicking and dragging the mouse.

import { Scene, PerspectiveCamera, WebGLRenderer, GridHelper } from 'three'
import { CameraRig, FreeMovementControls } from 'three-story-controls'

const scene = new Scene()
const camera = new PerspectiveCamera()
const renderer = new WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)

const rig = new CameraRig(camera, scene)
const controls = new FreeMovementControls(rig)
controls.enable()

function render(t) {
  window.requestAnimationFrame(render)
  controls.update(t)
  renderer.render(scene, camera)
}

render()



Installation

The library depends on three.js r129 or later and gsap 3.6.1, which need to be installed separately.

1. ES Module

Download dist/three-story-controls.esm.min.js (or use the CDN link) and use an importmap-shim to import the dependencies. See here for a full example. The demos also use this method of installation:

index.html

<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
  "imports": {
    "three": "https://cdn.skypack.dev/[email protected]",
    "gsap": "https://cdn.skypack.dev/[email protected]",
    "three-story-controls" : "./three-story-controls.esm.min.js"
  }
}
</script>
<script src='index.js' type='module-shim'></script>

index.js

import { Scene, PerspectiveCamera } from 'three'
import { ScrollControls } from 'three-story-controls'

2. NPM

If you use a build system such as Webpack / Parcel / Rollup etc, you can also install the library along with three.js and gsap from npm:

npm install -s three gsap three-story-controls

See here for a webpack example.

3. Script tag

Download dist/three-story-controls.min.js (or use the CDN link) and include it in your HTML file with a script tag, along with three.js and gsap. This will expose a global variable ThreeStoryControls. See here for more:

<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/gsap.min.js"></script>
<script src='three-story-controls.min.js'></script>




Components

Camera Rig

The core component of the library is the CameraRig - a wrapper around three.js camera that makes it easier to specify camera actions such as pan / tilt / dolly etc. without worrying about the existing camera transform.

const rig = new CameraRig(camera, scene)
rig.do(CameraAction.Pan, Math.PI / 6)
rig.do(CameraAction.Tilt, Math.PI / 12)

With the default up axis set to Y, the actions map like so:

Action Transform
Pan Rotate around Y
Tilt Rotate around X
Roll Rotate around Z
Pedestal Translate on Y
Truck Translate on X
Dolly Translate on Z

The CameraRig can also be provided with a three.js AnimationClip to animate/control it on a predefined rail. See here for more.


Camera Helper

The CameraHelper tool can be enabled on any scene to allow one to record camera positions and create a camera animation path. The data can be exported as a JSON file, which can then be used in various control schemes. See here for more.

Camera Helper

Control schemes

The library comes with 5 pre-built control schemes:

Name Description
FreeMovementControls Click-and-drag to rotate the camera up/down/left/right; and WASD, Arrow keys, mouse wheel/trackpad to move forwards/backwards and side-to-side
ScrollControls Scrub the camera along a path specified by an AnimationClip by scrolling through a DOM element
StoryPointControls Transition the camera between specified points
PathPointControls Transition the camera to specific frames of a path specified by an AnimationClip
ThreeDOFControls Rotate the camera slightly while staying in place - intended to be used alongside the other control schemes.

Input Adaptors

Adaptors are responsible for smoothing and transforming input data into something more digestable, and emit events with this transformed data.

Name Description
PointerAdaptor Handles pointer movements, click and drag, and multi-touch events
KeyboardAdaptor Handles keyboard event for specified keys
ScrollAdaptor Handles calculation for scroll distance for a specified DOM element
SwipeAdaptor Detects and handles swipe events
WheelAdaptor Handles mouse wheel events and detects thresholded wheel movement

Building your own control scheme

You could build your own control schemes using a combination of Adaptors and the CameraRig. Here is a rough implementation in TypeScript, see the existing control schemes for examples.

class MyCustomControls implements BaseControls {
  constructor(cameraRig) {
    this.rig = rig
    // Initialize required adaptors
    this.keyboardAdaptor = new KeyboardAdaptor( /* props */ )
    this.pointerAdaptor = new PointerAdaptor( /* props */ )
    // Bind this class instance to the event handler functions (implemented below)
    this.onKey = this.onKey.bind(this)
    this.onPointer = this.onPointer.bind(this)
  }


  // Handle events
  // Adaptors emit smoothed (and normalized) values that can be processed as needed
  // See adaptor docs for details on the event signatures  
  private onKey(event) {
    // Tell the Camera Rig to do a specific action, by a given amount
    this.cameraRig.do(CameraAction.Dolly, event.value.backward - event.value.forward)
  }

  private onPointer(event) {
    this.cameraRig.do(CameraAction.Pan, event.deltas.x)
  }

  // Implement BaseControl method
  enable() {
    // Connect the adaptors
    this.keyboardAdaptor.connect()
    this.pointerAdaptor.connect()
    this.keyboardAdaptor.addEventListener('update', this.onKey)
    this.pointerAdaptor.addEventListener('update', this.onPointer)
    this.enabled = true
  }

  // Implement BaseControl method
  disable() {
    // Disconnect, remove event listeners, set enabled to false
  }

  // Implement BaseControl method
  update(time: number): void {
    if (this.enabled) {
      this.keyboardAdaptor.update()
      this.pointerAdaptor.update(time)
    }
  }
}

API and demos

API documentation lives here, and demos can be viewed here. Code for the demos lives in examples/demos


Contributing

Contributions are welcome! To develop locally, run npm install and then npm run dev. The demos directory will be watched and served at http://localhost:8080/examples/demos, where you can add a new page to test out changes (please ensure test pages are ignored by git).

If you add a new component, be sure to create an example and document it following the TSDoc standard. The library uses API Extractor, which has some additional comment tags available. To extract the documentation, run npm run docs.



This repository is maintained by the Research & Development team at The New York Times and is provided as-is for your own use. For more information about R&D at the Times visit rd.nytimes.com

More Repositories

1

covid-19-data

A repository of data on coronavirus cases and deaths in the U.S.
6,992
star
2

objective-c-style-guide

The Objective-C Style Guide used by The New York Times
5,848
star
3

gizmo

A Microservice Toolkit from The New York Times
Go
3,753
star
4

Store

Android Library for Async Data Loading and Caching
Java
3,531
star
5

NYTPhotoViewer

A modern photo viewing experience for iOS.
Objective-C
2,847
star
6

pourover

A library for simple, fast filtering and sorting of large collections in the browser. There is a community-maintained fork that addresses a handful of post-NYT issues available via @hhsnopek's https://github.com/hhsnopek/pourover
JavaScript
2,393
star
7

kyt

Starting a new JS app? Build, test and run advanced apps with kyt ๐Ÿ”ฅ
JavaScript
1,922
star
8

react-tracking

๐ŸŽฏ Declarative tracking for React apps.
JavaScript
1,876
star
9

ice

track changes with javascript
JavaScript
1,708
star
10

backbone.stickit

Backbone data binding, model binding plugin. The real logic-less templates.
JavaScript
1,641
star
11

library

A collaborative documentation site, powered by Google Docs.
JavaScript
1,137
star
12

openapi2proto

A tool for generating Protobuf v3 schemas and gRPC service definitions from OpenAPI specifications
Go
940
star
13

gziphandler

Go middleware to gzip HTTP responses
Go
857
star
14

svg-crowbar

Extracts an SVG node and accompanying styles from an HTML document and allows you to download it all as an SVG file.
JavaScript
840
star
15

ingredient-phrase-tagger

Extract structured data from ingredient phrases using conditional random fields
Python
784
star
16

Emphasis

Dynamic Deep-Linking and Highlighting
JavaScript
576
star
17

tamper

Ruby
499
star
18

three-loader-3dtiles

This is a Three.js loader module for handling OGC 3D Tiles, created by Cesium. It currently supports the two main formats, Batched 3D Model (b3dm) - based on glTF Point cloud.
TypeScript
444
star
19

react-prosemirror

A library for safely integrating ProseMirror and React.
TypeScript
418
star
20

rd-blender-docker

A collection of Docker containers for running Blender headless or distributed โœจ
Python
415
star
21

Register

Android Library and App for testing Play Store billing
Kotlin
381
star
22

text-balancer

Eliminate typographic widows and other type crimes with this javascript module
JavaScript
373
star
23

document-viewer

The NYTimes Document Viewer
JavaScript
310
star
24

ios-360-videos

NYT360Video plays 360-degree video streamed from an AVPlayer on iOS.
Objective-C
273
star
25

backbone.trackit

Manage unsaved changes in a Backbone Model.
JavaScript
202
star
26

aframe-loader-3dtiles-component

A-Frame component using 3D-Tiles
JavaScript
187
star
27

marvin

A go-kit HTTP server for the App Engine Standard Environment
Go
177
star
28

drone-gke

Drone plugin for deploying containers to Google Kubernetes Engine (GKE)
Go
165
star
29

Chronicler

A better way to write your release notes.
JavaScript
162
star
30

nginx-vod-module-docker

Docker image for nginx with Kaltura's VoD module used by The New York Times
Dockerfile
161
star
31

collectd-rabbitmq

A collected plugin, written in python, to collect statistics from RabbitMQ.
Python
143
star
32

public_api_specs

The API Specs (in OpenAPI/Swagger) for the APIs available from developer.nytimes.com
136
star
33

gunsales

Statistical analysis of monthly background checks of gun purchases
R
130
star
34

gcp-vault

A client for securely retrieving secrets from Vault in Google Cloud infrastructure
Go
119
star
35

rd-bundler-3d-plugins

Bundler plugins for optimizing glTF 3D models
JavaScript
119
star
36

Fech

Deprecated. Please see https://github.com/dwillis/Fech for a maintained fork.
Ruby
115
star
37

data-training

Files from the NYT data training program, available for public use.
114
star
38

drone-gae

Drone plugin for managing deployments and services on Google App Engine (GAE)
Go
97
star
39

mock-ec2-metadata

Go
95
star
40

encoding-wrapper

Collection of Go wrappers for Video encoding cloud providers (moved to @video-dev)
Go
85
star
41

redux-taxi

๐Ÿš• Component-driven asynchronous SSR in isomorphic Redux apps
JavaScript
70
star
42

video-captions-api

Agnostic API to generate captions for media assets across different transcription services.
Go
61
star
43

lifeline

A cron-based alternative to running daemons
Ruby
58
star
44

gcs-helper

Tool for proxying and mapping HTTP requests to Google Cloud Storage (GCS).
Go
54
star
45

logrotate

Go
54
star
46

httptest

A simple concurrent HTTP testing tool
Go
48
star
47

kyt-starter-universal

Deprecated, see: https://github.com/NYTimes/kyt/tree/master/packages/kyt-starter-universal
JavaScript
33
star
48

nytcampfin

A thin Python client for The New York Times Campaign Finance API
Python
27
star
49

safejson

safeJSON provides replacements for the 'load' and 'loads' methods in the standard Python 'json' module.
Python
27
star
50

thumbor-docker-image

Docker image for Thumbor smart imaging service
26
star
51

times_wire

A thin Ruby client for The New York Times Newswire API
Ruby
26
star
52

haiti-debt

Historical data on Haitiโ€™s debt payments to France collected by The New York Times.
21
star
53

jsonlogic

Clojure
20
star
54

elemental-live-client

JS library to communicate with Elemental live API.
JavaScript
19
star
55

Open-Source-Science-Fair

The New York Times Open Source Science Fair
JavaScript
19
star
56

tweetftp

Ruby Implementation of the Tweet File Transfer Protocol (APRIL FOOLS JOKE)
Ruby
19
star
57

hhs-child-migrant-data

Data from the U.S. Department of Human Health and Services on children who have migrated to the United States without an adult.
19
star
58

prosemirror-change-tracking-prototype

JavaScript
18
star
59

plumbook

Data from the Plum Book, published by the GPO every 4 years
17
star
60

libvmod-queryfilter

Simple querystring filter/sort module for Varnish Cache v3-v6
M4
16
star
61

sneeze

Python
16
star
62

querqy-clj

Search Query Rewriting for Elasticsearch and more! Built on Querqy.
Clojure
14
star
63

sqliface

handy interfaces and test implementations for Go's database/sql package
Go
14
star
64

grocery

The grocery package provides easy mechanisms for storing, loading, and updating Go structs in Redis.
Go
13
star
65

oak-byo-react-prosemirror-redux

JavaScript
13
star
66

vase.elasticsearch

Vase Bindings for Elasticsearch
Clojure
11
star
67

library-customization-example

An example repo that customizes Library behavior
SCSS
11
star
68

counter

count things, either as a one-off or aggregated over time
Ruby
11
star
69

kyt-starter

The default starter-kyt for kyt apps.
JavaScript
10
star
70

open-blog-projects

A repository for code examples that are paired with our Open Blog posts
Swift
9
star
71

rd-mobile-pg-demos

HTML
9
star
72

tulsa-1921-data

Data files associated with our story on the 1921 race massacre in Tulsa, Oklahoma.
9
star
73

pocket_change

Python
9
star
74

mentorship

7
star
75

sort_by_str

SQL-like sorts on your Enumerables
Ruby
7
star
76

drone-gdm

Drone.io plugin to facilitate the use of Google Deployment Manager in drone deploy phase.
Go
6
star
77

kyt-starter-static

Deprecated, see: https://github.com/NYTimes/kyt/tree/master/packages/kyt-starter-static
JavaScript
6
star
78

s3yum

Python
5
star
79

pocket

Python
5
star
80

drone-openapi

A Drone plugin for publishing Open API service specifications
Go
5
star
81

threeplay

Go client for the 3Play API.
Go
4
star
82

amara

Amara client for Go
Go
4
star
83

go-compare-expressions

Go
3
star
84

kaichu

Python
3
star
85

license

NYT Apache 2.0 license
3
star
86

prosemirror-tooltip

JavaScript
2
star
87

photon-dev_demo

A "Sustainable Systems, Powered By Python" Demo Repository (1 of 3)
Shell
2
star
88

std-cat

Content Aggregation Technology โ€” a standard for content aggregation on the Web
HTML
1
star