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

react-threeco

v1.1.1

Published

Animate (interactive) 3D and 2D graphics in React

Downloads

8

Readme

Threeco

Threeco provides a simple way to create a render loop inside a React application.
That loop can be used to animate (interactive) 3D and 2D graphics e.g. for a presentation, a web app or a browser game.

What to do with the render loop is up to you. You may create a WebGL or Three.js project or simply animate with the canvas element.

Get started

First install the npm package to your React project:

$ npm i react-threeco

Now you have access to the useThreeco hook:

import {useThreeco} from 'react-threeco';

const MyComponent = () => {
    useThreeco(() => {
        // setup your canvas, scene or whatever

        return {
            // update your values that depend on deltaTime
            onUpdate: deltaTime => {},
            // redraw your canvas
            onRender: () => {},
            // remove listeners, elements from DOM, ...
            onUnmount: () => {}
        };
    });

    return (
        <div>
            Hello World
        </div>
    );
};

export default MyComponent;

Alternatively you can use the Threeco component that works pretty similar:

import {Threeco} from 'react-threeco';

const MyComponent = () => {
    const setup = () => {
        // setup your canvas, scene or whatever

        return {
            // update your values that depend on deltaTime
            onUpdate: deltaTime => {},
            // redraw your canvas
            onRender: () => {},
            // remove listeners, elements from DOM, ...
            onUnmount: () => {}
        };
    };

    return (
        <Threeco setup={setup}>
            Hello World
        </Threeco>
    );
};

export default MyComponent;

Documentation

useThreeco

useThreeco(setup: (...context?: unknown[]) => object, ...context?: unknown[]): object

The useThreeco hook takes a setup function as first argument.
All other arguments (if any) are passed to that setup function.

const arg1 = 'Number:';
const arg2 = 42;

useThreeco(
    (label, num) => {
        return {
            onUpdate: deltaTime => {},
            onRender: () => console.log(`${label} ${num++}`)
        };
    },
    arg1,
    arg2
);

Return

The hook returns an object of the following shape:

Key | Description :--- | :--- run: () => void | This function starts the render loop (if it's not already running). pause: () => void | This function pauses the render loop (if it's not already paused). setRunning: (running: boolean) => void | This function starts (running === true) or pauses (running === false) the render loop. isRunning: boolean | This boolean value indicates if the render loop is currently running or not.

const {run, pause, isRunning} = useThreeco(() => ({
    onUpdate: deltaTime => {},
    onRender: () => {},
    autorun: false
}));

Threeco

The Threeco component functionality is similar to that of the useThreeco hook.
It can have the following properties:

Property | Description :--- | :--- setup | A mandatory setup function to setup the scene. context | An optional context that will be passed to the setup function.If the value of this property is an array it will be spreaded. isRunning | An optional flag to control the render loop.

<Threeco
    setup={(first, second) => ({onRender: () => {}})} 
    context={['first', 'second']}
    isRunning={true}
/>

The component can have children too.

<Threeco setup={() => ({onRender: () => {}})}>
    <canvas id="canvas" width="800" height="450"></canvas>
</Threeco>

Setup function

The setup function is used to prepare your scene or canvas and register the listeners you need (click, mousedown, touchstart, ...).
It may receive arguments (the context).
The function must then return an object of the following shape:

Key | Description :--- | :--- onUpdate?: (deltaTime: number, timestamp: DOMHighResTimeStamp) => void | A function to update your scene. Time-based values should be multiplied by deltaTime (the time in seconds since the last onUpdate was called). The second parameter timestamp is the timestamp in milliseconds of the last call to onUpdate.If you don't set onUpdate, there won't be a render loop and your scene will render only once (which may be desired if you want a static scene). onRender: () => void | A function that is used to render (or redraw) your scene.While onUpdate should do all the math, this function should be responsible for displaying everything.The onRender function will be called after onUpdate. If there is no onUpdate function, onRender will be called only once. onUnmount?: () => void | If your setup added elements to the DOM or registered listeners, this function is used to tidy up. It will be called when your component gets unmounted. autorun?: boolean | An optional flag to determine if the render loop should start automatically after the setup. This is true by default.