4.0.1 • Published 4 months ago

ng-whiteboard v4.0.1

Weekly downloads
69
License
MIT
Repository
github
Last release
4 months ago

Lightweight angular whiteboard.

Build Status ng-whiteboard npm version License: MIT Downloads codecov

ng-whiteboard is a feature-rich, modular, and flexible whiteboard library for Angular. Designed for ease of use, performance, and a seamless drawing experience. It provides essential drawing tools and features with a scalable architecture.

šŸ”— Live Demo

✨ Features

  • šŸŽØ SVG-based rendering for high-quality visuals
  • ⚔ Optimized performance for smooth interactions
  • šŸ“¦ Modular, tree-shakable, and lightweight architecture
  • šŸ› ļø Comprehensive Public API via WhiteboardService and component bindings
  • šŸ“ Undo/Redo, Zoom, and Save functionalities
  • šŸ–Šļø Multiple drawing tools (pen, shapes, text, etc.)
  • šŸ— Future-proof and scalable design

šŸ“¦ Installation

Install via yarn:

yarn add ng-whiteboard

or npm:

npm install ng-whiteboard

Import the module in your app.module.ts:

import { NgWhiteboardModule } from 'ng-whiteboard';

@NgModule({
  imports: [BrowserModule, NgWhiteboardModule],
  bootstrap: [AppComponent],
})
export class AppModule {}

šŸš€ Usage

Basic Example

<ng-whiteboard></ng-whiteboard>

Advanced Example With Persist Data

import { Component, OnInit } from '@angular/core';
import { WhiteboardService } from 'ng-whiteboard';

@Component({
  selector: 'app-whiteboard-container',
  templateUrl: './whiteboard-container.component.html',
  styleUrls: ['./whiteboard-container.component.css'],
})
export class WhiteboardContainerComponent implements OnInit {
  whiteboardOptions: WhiteboardOptions = {
    backgroundColor: '#fff',
    strokeColor: '#2c80b1',
    strokeWidth: 5,
  };

  constructor(private whiteboardService: WhiteboardService) {}

  ngOnInit() {
    const savedData = localStorage.getItem('whiteboardData');
    if (savedData) {
      this.data = JSON.parse(savedData);
    }
  }

  onDataChange(data: WhiteboardElement[]) {
    localStorage.setItem('whiteboardData', JSON.stringify(data));
  }

  clearBoard() {
    this.whiteboardService.clear();
  }
}

Component Integration

<ng-whiteboard (dataChange)="onDataChange($event)" [options]="whiteboardOptions"></ng-whiteboard>

āš™ļø Configuration

InputTypeDefaultDescription
[data]WhiteboardElement[][]The whiteboard data
[options]WhiteboardOptionsnullComponent configuration object, properties described below
[drawingEnabled]booleantrueEnable mouse/touch interactions
[selectedTool]ToolTypeToolType.PenThe current selected tool
[canvasWidth]number800The width of whiteboard canvas
[canvasHeight]number600The height of whiteboard canvas
[fullScreen]booleantrueIf true, change (canvasWidth, canvasHeight) to fit the parent container
[strokeColor]string#333333The default stroke color
[backgroundColor]string#F8F9FAThe default background color
[fill]stringtransparentThe default fill color
[strokeWidth]number2The default stroke width
[zoom]number1Zoom level
[fontFamily]stringsans-serifThe default font family
[fontSize]number24The default font size
[center]booleantrueCenter the canvas in parent component, works with fullScreen: false
[x]number0If center is false, set the X axis
[y]number0If center is false, set the Y axis
[enableGrid]booleanfalseEnable the grid pattern
[gridSize]number10Set the grid inner boxes size
[snapToGrid]booleanfalseEnable snapping to grid
[lineJoin]LineJoinLineJoin.MiterThe default Line join
[lineCap]LineCapLineCap.ButtThe default Line cap
[dasharray]string''The default dash-array
[dashoffset]number0The default dash-offset

šŸ“– API Reference

WhiteboardService Methods

šŸ“Œ Element Management

  • addElement(element: WhiteboardElement)
    Adds a new element (e.g., shape, text) to the whiteboard.
  • addImage(image: string, x?: number, y?: number)
    Adds an image to the whiteboard at a specified position.
  • removeElement(id: string)
    Removes an element from the whiteboard by its ID.
  • updateElement(element: WhiteboardElement)
    Updates an existing element with new properties.
  • updateSelectedElement(partialElement: Partial<WhiteboardElement>)
    Modifies only specific properties of the currently selected element.
  • selectElement(element: WhiteboardElement | null)
    Selects or deselects an element on the whiteboard.

šŸ”„ State Management

  • clear()
    Clears all elements from the whiteboard.
  • undo()
    Reverts the last action.
  • redo()
    Restores the last undone action.
  • save(format = FormatType.Base64, name = 'New board')
    Saves the current whiteboard state in the specified format (e.g., Base64, JSON, SVG).

šŸ–Œ Drawing Tools & Interaction

  • setActiveTool(tool: ToolType)
    Sets the current drawing tool (e.g., pen, eraser, shape).

šŸŽØ Canvas Control

  • setCanvasDimensions(width: number, height: number)
    Sets the width and height of the whiteboard canvas.
  • setCanvasPosition(x: number, y: number)
    Moves the canvas to a specific position.
  • centerCanvas()
    Centers the whiteboard canvas within the viewport.
  • fullScreen()
    Toggles full-screen mode for the whiteboard.
  • toggleGrid()
    Enables or disables the background grid for alignment.

šŸ“¢ Whiteboard Events (Outputs)

The NgWhiteboardComponent emits the following events to notify about changes and interactions.

🟢 Lifecycle Events

  • ready
    Emitted when the whiteboard is fully initialized and ready for use.
  • destroyed
    Emitted when the whiteboard is destroyed, allowing for cleanup.

āœļø Drawing Events

  • drawStart
    Triggered when a user starts drawing on the whiteboard.
  • drawing
    Emitted continuously while the user is drawing.
  • drawEnd
    Triggered when the user stops drawing.

šŸ”„ State & Data Events

  • undo
    Emitted when an undo action is performed.
  • redo
    Emitted when a redo action is performed.
  • clear
    Triggered when the whiteboard is cleared.
  • dataChange
    Emitted when the whiteboard's internal data state changes.
  • save
    Triggered when the whiteboard state is saved.

šŸ“Œ Element Events

  • elementAdded
    Emitted when a new element is added to the whiteboard.
  • elementUpdated
    Triggered when an existing element is modified.
  • elementSelected
    Emitted when an element is selected.
  • elementDeleted
    Triggered when an element is removed.

šŸ–¼ Image Events

  • imageAdded
    Emitted when an image is added to the whiteboard.

šŸ›  Configuration & Tool Events

  • selectedToolChange
    Triggered when the active drawing tool is changed.
  • configChanged
    Emitted when the whiteboard configuration settings are updated.

šŸ¤ Contributing

We welcome contributions! Feel free to submit issues, feature requests, or pull requests.

šŸ“œ License

This project is licensed under the MIT License.

3.2.2

4 months ago

3.2.1

5 months ago

3.2.0

5 months ago

3.2.6

4 months ago

3.2.5

4 months ago

3.2.4

4 months ago

3.2.3

4 months ago

4.0.1

4 months ago

4.0.0

4 months ago

3.1.3

8 months ago

3.1.2

8 months ago

3.1.6

6 months ago

3.1.5

6 months ago

3.1.4

7 months ago

3.1.1

8 months ago

3.0.8

10 months ago

3.0.7

10 months ago

3.1.0

9 months ago

3.0.6

10 months ago

3.0.5

1 year ago

3.0.4

1 year ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.22

2 years ago

2.0.20

2 years ago

2.0.19

2 years ago

2.0.17

2 years ago

2.0.18

2 years ago

2.0.15

2 years ago

2.0.16

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.11

2 years ago

2.0.12

2 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.3

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.5.7

3 years ago

1.5.0

4 years ago

1.4.6

4 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago