Scripts2026年7月22日·1 分钟阅读

Two.js — Two-Dimensional Drawing API for the Web

Renderer-agnostic 2D drawing library for JavaScript that outputs to SVG, Canvas, and WebGL with a consistent API.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Script
安装
Stage only
信任
信任等级:Established
入口
Two.js
安全暂存命令
npx -y tokrepo@latest install ac50d9ff-8565-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Two.js is a renderer-agnostic two-dimensional drawing API for JavaScript. It provides a single API surface that can render to SVG, HTML5 Canvas, or WebGL, letting developers switch renderers without rewriting drawing code. The library is well suited for interactive graphics, data visualization, and creative coding.

What Two.js Does

  • Draws shapes, paths, text, and images through a unified API
  • Renders to SVG, Canvas, or WebGL without changing application code
  • Provides a built-in animation loop for smooth interactive graphics
  • Supports grouping, transformations, gradients, and clipping masks
  • Works in both browser and Node.js environments (with node-canvas)

Architecture Overview

Two.js maintains a scene graph of drawable objects. When you create shapes, they are added to the scene graph as nodes. On each render call, the library traverses the graph and delegates drawing to the selected renderer (SVG DOM manipulation, Canvas 2D context calls, or WebGL shader programs). The scene graph approach enables efficient updates since only changed nodes trigger re-rendering.

Self-Hosting & Configuration

  • Install via npm with npm install two.js or include from a CDN
  • Create a Two instance with width, height, and optional renderer type (svg, canvas, webgl)
  • Append the renderer element to any DOM container
  • Use two.play() for continuous animation or two.update() for single-frame rendering
  • Configure resolution scaling via the ratio option for high-DPI displays

Key Features

  • Renderer-agnostic API that swaps between SVG, Canvas, and WebGL at instantiation
  • Scene graph with hierarchical grouping, transforms, and opacity inheritance
  • Built-in animation loop with requestAnimationFrame timing
  • Support for gradients (linear and radial), textures, and clipping paths
  • Lightweight footprint with no external dependencies

Comparison with Similar Tools

  • p5.js — creative coding focus with Canvas; Two.js adds SVG and WebGL as first-class outputs
  • Fabric.js — interactive Canvas objects with editing; Two.js is renderer-agnostic
  • Paper.js — vector graphics on Canvas with path operations; Two.js supports multiple renderers
  • Konva — Canvas-based with event system and React bindings; Two.js offers SVG and WebGL options
  • D3.js — data-driven DOM manipulation; Two.js provides a drawing-first API with animation built in

FAQ

Q: When should I choose SVG vs Canvas vs WebGL rendering? A: Use SVG for small scene counts where DOM interactivity matters, Canvas for moderate complexity, and WebGL for high-performance scenes with many objects.

Q: Can I use Two.js with React or Vue? A: Yes. Create a Two instance in a ref or lifecycle hook and append it to a container element. The library is framework-agnostic.

Q: Does Two.js support user interaction like click and hover? A: Two.js focuses on rendering. For interaction, attach DOM events to the SVG renderer output or use a hit-testing approach with the Canvas renderer.

Q: Is Two.js suitable for data visualization? A: Yes. Its shape primitives and scene graph work well for custom charts and diagrams where a full charting library is not needed.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产