two.js

repository·dev·Indexed 27 days ago

https://github.com/jonobr1/two.js

A renderer-agnostic two-dimensional drawing API for modern browsers. It allows developers to write drawing code once and render it using WebGL, Canvas2D, or SVG. The library supports ES6 modules, headless environments via Node Canvas, and includes features for hit testing, SVG path interpretation, and memory management through .dispose() methods. Version v0.8.23.

Tokens
25.5K
Snippets
12
Records
339
Agent score
93%

What's inside two.js

  1. Use Two.Gradient as a base for gradients

    dev

    Two.Gradient is the base class for constructing gradients in Two.js. It is not typically used directly, but rather serves as the foundation for specific gradient types like Two.LinearGradient and Two.RadialGradient.

    To construct a gradient, you provide a list of Two.Stop objects which define the fill pattern.

  2. Use Two.Collection for event-driven array management

    dev

    A Two.Collection is an Array-like object that provides additional event propagation for common array operations. It extends Two.Events, allowing you to listen for changes to the collection's contents or order.

    Supported events include:

    • removed: Triggered by pop, shift, and splice.
    • inserted: Triggered by push, unshift, and splice (when splice is called with more than 2 arguments).
    • order: Triggered by sort and reverse.
  3. Use Two.Group to group objects

    dev
    The Two.Group class is used to group multiple objects (such as Two.Path, Two.Text, or Two.RoundedRectangle) together. A group contains a transformation matrix and allows you to apply styles to all its children simultaneously. Note that the group itself does not render to the screen; it only manages its children.
  4. Securely handle untrusted SVGs and assets in Two.js

    dev

    When using Two.js for client-side rendering, follow these safety practices to mitigate risks from malicious content:

    • Sanitize SVGs: Do not load or interpret SVGs from untrusted users without sanitizing them first. Malicious SVGs can contain embedded scripts or external references.
    • Manage External Assets: Prefer using same-origin assets or vetted hosts. If using embedders or iframes, disable allow-scripts and avoid using inline event handlers.
    • Implement Content Security Policy (CSP): It is recommended to use a CSP that:
      • Restricts scripts to self and trusted CDNs.
      • Disallows inline scripts and eval().
      • Sets object-src 'none'.

    Note: Two.js does not collect user data. If your application handles user content, you are responsible for implementing input validation, rate limiting, and abuse reporting.

  5. Import Two.js using ES6 Modules

    dev

    Two.js (v0.7.5+) supports ES6 imports, making it compatible with frameworks like React and Angular, and bundlers like webpack or esbuild.

    Note: The main Two import includes all modules and does not currently support full tree shaking. To reduce bundle size, you can import specific modules directly from the source paths.

    import Two from "two.js";
    
    // To import specific modules for better tree shaking:
    import { Vector } from 'two.js/src/vector.js';
    
    // In TypeScript environments, omit the ".js" extension:
    import { Vector } from 'two.js/src/vector';