This directory contains comprehensive TypeScript type definitions for the DrCircuitsCanvasLibrary (DCL), providing full type safety and IntelliSense support for TypeScript developers.
npm install drcircuitscanvaslibrary
The package includes TypeScript definitions automatically. No additional @types
package is needed.
If you’re using the library from source:
types/dcl.d.ts
file is included in your projecttsconfig.json
should include DOM types:
{
"compilerOptions": {
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}
import dcl, { vector, color, Vector, Color, ScreenOptions } from 'drcircuitscanvaslibrary';
// Type-safe screen configuration
const options: ScreenOptions = {
width: 800,
height: 600,
background: '#000000',
pixelated: false
};
// Initialize with proper typing
await dcl.init(options);
import { vector, Vector } from 'drcircuitscanvaslibrary';
// Create vectors with type safety
const position: Vector = vector.create(100, 200);
const velocity: Vector = vector.create(2, -1);
// All vector methods are fully typed
const newPosition: Vector = position.add(velocity);
const distance: number = position.distance(newPosition);
const normalized: Vector = velocity.normalize();
const magnitude: number = velocity.magnitude();
// Chaining operations
const result: Vector = position
.add(velocity)
.mul(2)
.normalize()
.mul(50);
import { color, Color } from 'drcircuitscanvaslibrary';
// Create colors with full type support
const red: Color = color.create(255, 0, 0, 255);
const blue: Color = color.fromHex('#0000FF');
const randomColor: Color = color.random();
// Color operations
const mixed: Color = red.mix(blue, 0.5);
const lighter: Color = red.lighten(0.2);
const rgbString: string = red.toRgbString();
import dcl, { AnimationCallback } from 'drcircuitscanvaslibrary';
const animate: AnimationCallback = (time: number, deltaTime: number): void => {
// Clear background
dcl.background('#000');
// Type-safe drawing
dcl.fill('#ff0000');
dcl.circle(100, 100, 50);
// Text with proper typing
dcl.text(`FPS: ${Math.round(1000 / deltaTime)}`, 10, 30);
};
dcl.setupRun(animate);
dcl.playAnimation();
import dcl, { MouseInput, KeyboardInput } from 'drcircuitscanvaslibrary';
// Mouse events with proper typing
dcl.MOUSE.onMouseDown((x: number, y: number, button: number) => {
console.log(`Clicked at (${x}, ${y}) with button ${button}`);
});
// Keyboard events
dcl.KEYB.onKeyDown((key: string) => {
if (key === dcl.KEYB.SPACE) {
console.log('Spacebar pressed!');
}
});
Vector
- 2D vector with 40+ mathematical operationsColor
- RGBA color with manipulation methodsMatrix
- 2D transformation matrixComplex
- Complex number representationPalette
- Color palette managementScreenOptions
- Screen/canvas initialization optionsFrameRateOptions
- Animation frame rate settingsBuffer
- Off-screen rendering bufferTextAlign
- Text alignment optionsTextBaseline
- Text baseline optionsLineCap
- Line cap stylesLineJoin
- Line join stylesBlendMode
- Canvas blend modesAnimationCallback
- Animation loop function signatureKeyboardInput
- Keyboard state and event handlingMouseInput
- Mouse state and event handlingimport dcl from 'drcircuitscanvaslibrary';
await dcl.init({ width: 800, height: 600 });
dcl.circle(100, 100, 50);
import {
init,
circle,
vector,
color,
setupRun,
playAnimation
} from 'drcircuitscanvaslibrary';
await init({ width: 800, height: 600 });
circle(100, 100, 50);
import dcl, {
Vector,
Color,
vector,
color,
ScreenOptions
} from 'drcircuitscanvaslibrary';
const options: ScreenOptions = { width: 800, height: 600 };
await dcl.init(options);
const pos: Vector = vector.create(100, 100);
const col: Color = color.red();
When using TypeScript-enabled editors (VS Code, WebStorm, etc.), you’ll get:
Many DCL functions support multiple calling patterns:
// Circle drawing overloads
dcl.circle(x: number, y: number, radius: number): void;
dcl.circle(center: Vector, radius: number): void;
// Line drawing overloads
dcl.line(x1: number, y1: number, x2: number, y2: number): void;
dcl.line(start: Vector, end: Vector): void;
// Rectangle drawing overloads
dcl.rect(x: number, y: number, width: number, height: number): void;
dcl.rect(position: Vector, size: Vector): void;
// Initialization overloads
dcl.init(options: ScreenOptions): Promise<void>;
dcl.init(canvas: HTMLCanvasElement | string, width?: number, height?: number): Promise<void>;
dcl.init(width: number, height: number): Promise<void>;
The examples/typescript-example.ts
file demonstrates comprehensive usage of the TypeScript definitions. To test:
npm install -g typescript
tsc examples/typescript-example.ts
The type definitions are located in types/dcl.d.ts
and cover:
When adding new features to DCL:
types/dcl.d.ts
“Cannot find module ‘drcircuitscanvaslibrary’“
npm install drcircuitscanvaslibrary
tsconfig.json
has proper module resolution“Property does not exist on type”
“Type ‘X’ is not assignable to type ‘Y’“
examples/
directorytypes/dcl.d.ts