The Complete Production SOP: How to Deploy and Run Two.js Renderer-Agnostic 2D Drawing in Autonomous AI Agents
Master Two.js 2D drawing in Codex, Claude Code, & Cursor. Deterministic SOP for WebGL, Canvas, and SVG switching with verified prompt triggers.
Technical Overview & Architectural Context
Autonomous coding agents frequently stumble when generating multi-backend 2D vector graphics. When tasked with rendering canvas animations or vector graphics, LLMs routinely conflate native HTML5 Canvas 2D context methods (like ctx.arc or ctx.stroke) with DOM SVG attributes, or hallucinate WebGL shaders inside standard Canvas contexts. This creates tightly coupled, non-portable rendering code that breaks as soon as a user requests an SVG export or demands WebGL hardware acceleration for high-density particle passes. The 'twojs-renderer-agnostic-shapes' skill enforces strict procedural boundaries across Codex, Claude Code, Cursor, and Windsurf. By constraining the agent to the unified Two.js scene-graph API, the agent manipulates shapes, groups, anchor points, and matrices through an abstraction layer completely decoupled from the rendering surface. Whether the target is SVG for crisp vector exports, Canvas for lightweight DOM rendering, or WebGL for high-frame-rate composite animations, the agent generates deterministic code that operates identically across all three backends without hallucinating incompatible API boundaries.
Prerequisites & Environment Requirements
Verified Search Intents & Long-Tail Queries Solved
This operational tutorial is engineered to provide deterministic solutions for these developer search queries:
Step-by-Step Execution Playbook
Local Environment Setup & SKILL.md Mounting
Create the local skill manifest directory and inject verified procedural rules into the agent runtime directory.
Autonomous runtimes such as Claude Code and Cursor look for procedural boundaries inside workspace-level or user-level skill directories (e.g., `.cursor/rules`, `.claude/skills`, or `~/.agents/skills`). Mounting this SKILL.md provides explicit schema definitions, triggers, and forbidden patterns so the LLM does not write raw HTML5 Canvas or imperative DOM manipulation.
mkdir -p ~/.agents/skills/twojs-renderer-agnostic-shapes && cat << 'EOF' > ~/.agents/skills/twojs-renderer-agnostic-shapes/SKILL.md
---
name: "twojs-renderer-agnostic-shapes"
description: "Write once and render 2D vector scenes across SVG, Canvas 2D, and WebGL with Two.js."
metadata:
author: "Jono Brandel"
version: "1.0.0"
runtime_compatibility:
- "codex"
- "claude-code"
- "cursor"
- "windsurf"
triggers:
- "draw shapes with two.js"
- "switch two.js renderer webgl canvas"
- "animate two.js group"
permissions:
fs_read: true
fs_write: "workspace"
shell_exec: false
net_access: false
---
# Two.js Renderer-Agnostic 2D Drawing
Draws 2D scenes using Two.js: Two.Group hierarchies, shape primitives, gradients, anchor point manipulation, and seamless switching between SVG, Canvas, and WebGL rendering backends.
## When to Invoke
Invoke this skill whenever you need to:
- draw shapes with two.js
- switch two.js renderer webgl canvas
- animate two.js group
## Execution SOP & Rules
1. Initialize Two instance with new Two({ width, height, type: Two.Types.canvas }).
2. Add shapes with two.makeRectangle(), two.makeCircle(), or two.makePath().
3. Group related elements using two.makeGroup() and transform the group as a single unit.
4. Call two.update() to render changes, or two.play() to start continuous animation.
## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
EOF---
name: "twojs-renderer-agnostic-shapes"
description: "Write once and render 2D vector scenes across SVG, Canvas 2D, and WebGL with Two.js."
metadata:
author: "Jono Brandel"
version: "1.0.0"
runtime_compatibility:
- "codex"
- "claude-code"
- "cursor"
- "windsurf"
triggers:
- "draw shapes with two.js"
- "switch two.js renderer webgl canvas"
- "animate two.js group"
permissions:
fs_read: true
fs_write: "workspace"
shell_exec: false
net_access: false
---Inject the skill locally per project inside `.claude/skills/` or `.cursor/rules/` to ensure deterministic execution without inflating global system prompt tokens across non-frontend tasks.
Ensure `shell_exec` remains `false` in frontmatter permissions to prevent the agent from attempting to spawn unauthorized headless browser rendering workers.
Environment Secrets & Dependency Validation
Install the Two.js package and confirm TypeScript definitions are available for strict compilation.
Two.js requires no external API keys or secrets, but standard bundlers (Vite, Webpack, Rollup) require correct ES module imports. We install `two.js` alongside `@types/two.js` to guarantee TypeScript-aware agents generate type-safe geometry manipulation.
npm install two.js @types/two.jsimport Two from 'two.js';
export interface RendererConfig {
container: HTMLElement;
type: Two.Types;
width: number;
height: number;
}
export function initTwoScene(config: RendererConfig): Two {
const two = new Two({
type: config.type,
width: config.width,
height: config.height,
autostart: false
}).appendTo(config.container);
return two;
}Avoid loading whole UI framework wrappers. Plain Two.js instances mounted on a reference div offer higher execution speed and zero hydration mismatch issues.
Two.js relies on browser globals (`window`, `document`). In SSR environments like Next.js or Nuxt, dynamically import the module or guard with `typeof window !== 'undefined'`.
Intent Matching & Trigger Invocation
Trigger the skill deterministically using frontmatter trigger phrases to build modular scene graphs.
Provide the agent with targeted requirements matching the trigger keywords (`draw shapes with two.js`, `switch two.js renderer webgl canvas`, `animate two.js group`). The agent must construct scene elements as a hierarchical `Two.Group` with explicit pivot points, transforms, and an exportable renderer switch mechanism.
# Run in Claude Code CLI or Cursor Composer prompt:
# "Using twojs-renderer-agnostic-shapes: draw shapes with two.js and group them into an animated solar system model that can switch two.js renderer webgl canvas dynamically."import Two from 'two.js';
export class SolarSystemScene {
private two: Two;
private systemGroup: Two.Group;
constructor(container: HTMLElement, rendererType: Two.Types = Two.Types.canvas) {
this.two = new Two({
type: rendererType,
width: 800,
height: 600
}).appendTo(container);
this.systemGroup = this.two.makeGroup();
this.buildScene();
}
private buildScene(): void {
const sun = this.two.makeCircle(400, 300, 40);
sun.fill = '#FFA500';
sun.noStroke();
const planetOrbit = this.two.makeGroup();
planetOrbit.translation.set(400, 300);
const planet = this.two.makeCircle(120, 0, 15);
planet.fill = '#00BFFF';
planet.noStroke();
planetOrbit.add(planet);
this.systemGroup.add(sun, planetOrbit);
this.two.bind('update', () => {
planetOrbit.rotation += 0.02;
});
}
public start(): void {
this.two.play();
}
}Structure animations via `two.bind('update', callback)` rather than standard `requestAnimationFrame` loops so Two.js properly synchronizes frame renders across SVG, Canvas, and WebGL backends.
Do not mutate internal SVG DOM nodes directly or call Canvas `getContext('2d')` directly; doing so immediately invalidates Two.js renderer independence.
Verification, Linting & Output Quality Gate
Validate compilation, verify renderer swapping without memory leaks, and run syntax checks.
The verification quality gate ensures that swapping from `Two.Types.canvas` to `Two.Types.webgl` or `Two.Types.svg` unbinds events, frees WebGL contexts, and cleans the parent DOM node without leaving dangling requestAnimationFrame loops.
npx tsc --noEmit && npm run lintimport { describe, it, expect, beforeEach } from 'vitest';
import Two from 'two.js';
describe('Two.js Renderer Independence', () => {
let container: HTMLElement;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
it('creates elements across Canvas, SVG, and WebGL backends', () => {
[Two.Types.svg, Two.Types.canvas, Two.Types.webgl].forEach((backend) => {
const two = new Two({ type: backend, width: 200, height: 200 }).appendTo(container);
const rect = two.makeRectangle(50, 50, 20, 20);
two.update();
expect(two.scene.children.length).toBe(1);
expect(rect.id).toBeDefined();
two.clear();
two.renderer.domElement.remove();
});
});
});Always call `two.clear()` and remove `two.renderer.domElement` when tearing down or hot-reloading a scene to prevent WebGL context leaks.
WebGL backend in Two.js does not support all CSS vector styling properties (e.g. complex dash patterns or certain SVG stroke caps). Test SVG fallback for vector-heavy scenes.
Real-World Scenario: Interactive Data Visualizer with Dynamic SVG/Canvas/WebGL Switching
“Using twojs-renderer-agnostic-shapes: draw shapes with two.js to create a responsive multi-node network graph. Allow users to switch two.js renderer webgl canvas at runtime and animate two.js group rotations smoothly.”
Runtime Troubleshooting & Failure Modes
Root Cause: The agent attempted to initialize Two.js with `Two.Types.canvas` or `Two.Types.webgl` in a server-side rendering (SSR) lifecycle before the DOM node was mounted, or the container was not yet present in document.body.
if (typeof window !== 'undefined' && containerRef.current) { const two = new Two({ type: Two.Types.canvas }).appendTo(containerRef.current); }Root Cause: Repeated hot-reloading or continuous renderer toggling without calling `two.pause()`, `two.clear()`, and cleaning up WebGL instances causes browser WebGL context exhaustion (limit typically 8-16 contexts).
two.pause(); two.unbind('update'); two.clear(); if (two.renderer.domElement.parentNode) { two.renderer.domElement.parentNode.removeChild(two.renderer.domElement); }Frequently Asked Questions (Two.js Renderer-Agnostic 2D Drawing SOP)
Q:Can I use Two.js Renderer-Agnostic 2D Drawing across Codex, Claude Code, and Cursor simultaneously?
Yes. The skill conforms to the universal SKILL.md specification supported across all modern agent runtimes.
Q:How does Two.js Renderer-Agnostic 2D Drawing prevent context window overflow during long agent sessions?
By establishing concise procedural boundaries and deterministic validation gates rather than polluting system prompts with redundant rules.
Q:Does this skill require escalated root or container permissions?
No. It is audited with a security score of 98/100 and runs safely within workspace-scoped sandboxes.
Ready to equip your autonomous agent with Two.js Renderer-Agnostic 2D Drawing?
Copy the complete audited SKILL.md specification or download the 1-click zip bundle directly from our verified registry.