1.1.10 โ€ข Published 1 month ago

@sirhc77/canvas-math-kit v1.1.10

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

๐ŸŽฏ canvas-math-kit

A lightweight, interactive canvas-based vector visualizer for math, linear algebra, and ML education. Built with React + TypeScript.

Perfect for:

  • ๐Ÿ“ Math and ML learning tools
  • ๐ŸŽ“ Educational visualizations
  • ๐Ÿงฎ Interactive vector diagrams

๐Ÿ“ฆ Installation

npm install @sirhc77/canvas-math-kit

๐Ÿ“ฆ Import

import { GraphCanvas, type CanvasVector, type CanvasParallelogram } from '@sirhc77/canvas-math-kit';

๐ŸŽ›๏ธ <GraphCanvas /> Props

PropTypeDescription
widthnumberWidth of the canvas in pixels
heightnumberHeight of the canvas in pixels
scalenumberPixels per unit
vectorsCanvasVector[] (optional)Vectors to render and optionally drag
onVectorsChange(updated: CanvasVector[]) => void (optional)Callback fired when a draggable vector is moved
snapnumber | (x: number, y: number) => [number, number] (optional)Enables snapping to a grid or custom logic
lockedboolean (optional)If true, disables all dragging
parallelogramsCanvasParallelogram[] (optional)Areas formed by two vectors, filled and outlined
customDragTargetsDragTarget[] (optional)Items other than vectors that are draggable
onCustomDragTargetsChange(updated: DragTarget[]) => void (optional)Callback fired when a custom drag target is moved
customDraw(ctx, origin, scale) => void (optional)Custom canvas drawing logic (runs after vectors and parallelograms)

๐Ÿงฉ CanvasVector Type

type VectorHeadStyle = 'arrow' | 'circle' | 'both' | 'none';

interface CanvasVector {
  x: number;
  y: number;
  color?: string;              // Default: 'blue'
  draggable?: boolean;         // Default: false
  headStyle?: VectorHeadStyle; // Default: 'arrow'
  label?: string;
  width?: number;
}

๐Ÿ”ท CanvasParallelogram Type

interface CanvasParallelogram {
  vectorA: { x: number; y: number };
  vectorB: { x: number; y: number };
  fillColor?: string;   // Default: translucent blue
  strokeColor?: string; // Default: darker blue
}

๐Ÿงช Example Usage

<GraphCanvas
  width={400}
  height={400}
  scale={40}
  vectors={[
    { x: 2, y: 1, color: 'red', draggable: true, headStyle: 'both' },
    { x: -1, y: 2, color: 'green', headStyle: 'circle' }
  ]}
  parallelograms={[
    {
      vectorA: { x: 2, y: 1 },
      vectorB: { x: -1, y: 2 },
      fillColor: 'rgba(255, 0, 0, 0.1)',
      strokeColor: 'rgba(255, 0, 0, 0.4)'
    }
  ]}
  snap={1}
  customDraw={(ctx, origin, scale) => {
    const p = (x: number, y: number) => toCanvas(x, y, origin, scale);
    drawLine(ctx, p(-3, 0), p(3, 0), 'black', 1, true);
  }}
/>

๐Ÿงช Example Projects

Try out the demo locally:

cd demo
npm install
npm run dev

Or check the live demo (coming soon).


๐Ÿ› ๏ธ Roadmap

  • Draggable vectors
  • Snapping support
  • Per-vector styling
  • Labels
  • Hover highlights
  • Animation support
  • Export to PNG

๐Ÿ“ƒ License

MIT

1.1.10

1 month ago

1.1.9

1 month ago

1.1.8

1 month ago

1.1.7

1 month ago

1.1.6

1 month ago

1.1.5

2 months ago

1.1.4

2 months ago

1.1.3

2 months ago

1.1.2

2 months ago

1.1.1

2 months ago

1.1.0

2 months ago

1.0.4

2 months ago

1.0.3

2 months ago

1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago