npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

three-story-controls

v1.0.6

Published

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

Downloads

151

Readme


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