Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

draft-n-draw

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draft-n-draw

### 🖍️ A utility for drawing debug visualizations in threejs.

  • 0.3.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Draft-n-Draw

🖍️ A utility for drawing debug visualizations in threejs.

Easily visualize bounding boxes, raycasts, points, lights and more in threejs. Fully typed, it can be used with vanilla JS or TypeScript and also has convenient React components for use in react-three-fiber.

npm install draft-n-draw

Getting started

React

You begin by adding the DrafterProvider component to the top level of your app.

import { Canvas } from '@react-three/fiber';
import { DrafterProvider } from '@draft-n-draw/react';

function App() {
  return (
    <Canvas>
      <DrafterProvider>
        <Game />
      </DrafterProvider>
    </Canvas>
  );
}

And then we can access the drafter either inside a component with a hook or outside a component with a getter function.

import { getDrafter, useDrafter } from '@draft-n-draw/react';

// Outside a component.
const drafter = getDrafter();

// Inside a component.
function Component() {
  const drafter2 = useDrafter();

  //...
}

👉 Note: If you just want the React package you can install @draft-n-draw/react.

npm install @draft-n-draw/react

Vanilla JS

You begin by creating a drafter for your scene.

import { Drafter } from '@draft-n-draw/vanilla';

const drafter = new Drafter(scene);

👉 Note: If you just want the vanilla JS package you can install @draft-n-draw/vanilla.

npm install @draft-n-draw/vanilla

Draw!

Then anywhere you are debugging request a draw.

drafter.drawBox3(myBox3);
drafter.drawRay({ origin, direction, distance });

By default, draws only last a single frame and are kept active by being called in a loop. This way you can safely inline them in functions you are debugging without worrying about spawning too many scene objects or cleaning them up when done — Draft-n-Draw will handle that for you.

If instead you want the draw to persist, you can specify it in the draw options.

drafter.drawSpotLight(mySpotLight, { persist: true });

👉 Note: This will cause a new draw to be persisted on screen each call, so be careful!

A persisted draw can be cleaned up using the dispose() method with the object being visualized as the key.

drafter.dispose(mySpotLight);

Finally, there are additional draw options for controlling the visuals such as drawing on top and adjusting color or opacity.

drafter.drawWireSphere({ center, radius }, { color: 'yellow', alwaysOnTop: true, opacity: 0.5 });

API

The draw coverage is a work in progress. Our drafter is learning on the job! First we have the generic draw() method which can draw any custom visualization or helper. It has all the generic options discussed above.

MethodObjectOptions
draw()THREE.Object3D{ color: THREE.ColorRepresentation, alwaysOnTop: boolean, opacity: number, persist: boolean }

And then we have draws for math objects. Unless stated otherwise all options are shared with draw().

MethodObjectSpecial options
drawBox3THREE.Box3None.
drawRay{ origin: THREE.Vector3, direction: THREE.Vector3, distance: number }None.
drawPointTHREE.Vector3radius: number
drawWireTriangleTHREE.TriangleNone.
drawTriangleTHREE.TrianglewinZFight: boolean
drawWireSphere{ center: THREE.Vector3, radius: number } or THREE.SphereNone.
drawSphere{ center: THREE.Vector3, radius: number } or THREE.SphereNone.

And finally some draws for lights. Unless stated otherwise all options are shared with draw().

MethodObjectSpecial options
drawSpotlightTHREE.SpotLightNone.
drawPointlightTHREE.PointLightNone.
drawDirectionalLightTHREE.DirectionalLightNone.
drawHemisphereLightTHREE.HemisphereLightNone.

FAQs

Package last updated on 15 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc