百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ng2-canvas-whiteboard

> 编程语言
开源

Canvas 白板

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Canvas 白板

ng2-canvas-whiteboard

Canvas version changes

v7.0.1 fix dependency issues

v7.0.0 merges PR to add Angular17 support

https://github.com/webfactorymk/ng2-canvas-whiteboard/pull/112

  • The CanvasWhiteboardService and CanvasWhiteboardShapeService will now be using providedIn: 'root' instead of being provided in the CanvasWhiteboardModule directly.
  • Keep this in mind if you need to scope the services differently.

v6.0.0 Updates to Angular16

v5.0.0 merges PR to add Angular15 support

https://github.com/webfactorymk/ng2-canvas-whiteboard/pull/110

v4.0.2 merges PR to add variables for fillColorPickerText and strokeColorPickerText

https://github.com/webfactorymk/ng2-canvas-whiteboard/pull/68

v3.1.3, v4.0.1

Exports all existing canvas shapes, so that they can be easily unregistered from the canvas. (see README for unregistering the shapes).

Also, this version introduces two new Inputs, strokeColorPickerEnabled: boolean and fillColorPickerEnabled: boolean, also deprecates the colorPickerEnabled Input.

For the sake of reverse-compat, the colorPickerEnabled input is still there and it will be used in combination with the two new variables. (ex: colorPickerEnabled || fillColorPickerEnabled).

v3.1.1 Audits the npm packages and upgrades the lodash version from 4.17.11 to 4.17.13

v3.1.0 Merges the pull request from https://github.com/webfactorymk/ng2-canvas-whiteboard/pull/55 to allow the component to be used in Angular 8 and 9 applications. Also fixes the imports for rxjs items from 'rxjs/index' to 'rxjs'

v3.0.4 Fixes a bug with production build and recognition of shape names by adding an abstract method in the base Shape class.

v3.0.0 Removes the rxjs-compat library and adds rxjs^6. This means that older versions will not be supported if they upgrade to ng2-canvas-whiteboard^3.0.0.

This version also changes the way of how this library is built and made ready for publish.

For applications before Angular 6 please use versions below v3.0.0.

Features:

  • Premade Shapes
  • The ability to create custom premade shapes
  • Supports touch.
  • Supports UNDO/REDO.
  • Implements a color picker for stroke and fill colors.
  • Sends outputs on every action.
  • Contains inputs for multiple modifications.
  • Save drawn images

Install

  1. Install npm module:

    bash
    npm install ng2-canvas-whiteboard --save
  2. If you are using system.js you may want to add this into map and package config:

    json
    {
        "map": {
            "ng2-canvas-whiteboard": "node_modules/ng2-canvas-whiteboard"
        },
        "packages": {
            "ng2-canvas-whiteboard": { "main": "index.js", "defaultExtension": "js" }
        }
    }

Add the module to your project

typescript
@NgModule({
    imports: [
        CanvasWhiteboardModule
    ]
    ...
)}

In your component, you should add the CanvasWhiteboardComponent as a view provider

typescript
@Component({
    selector: '...',
    viewProviders: [CanvasWhiteboardComponent],
    templateUrl: '...'
})

In the html file, you can insert the Canvas Whiteboard

xml

If there is too much overhead with inputs, you can just specify the [options] input, and specify the options from the typescript code

Example:

xml

Code:

…

Drawing on the canvas

The canvas drawing is triggered when the user touches the canvas, draws (moves the mouse or finger) and then stops drawing. When the drawing is started, after 100 ms all the signals in between are added to a list and are sent as a batch signal which is emitted by the onBatchUpdate emitter. If received, the user can then manipulate with the sent signals.

Inputs

batchUpdateTimeoutDuration: number (default: 100)

The time in milliseconds that a batch update should be sent after drawing.

imageUrl: string (optional)

The path to the image. If not specified, the drawings will be placed on the background color of the canvas. This path can either be a base64 string or an actual path to a resource

aspectRatio: number (optional)

If specified, the canvas will be resized according to this ratio

drawButtonClass: string

clearButtonClass: string undoButtonClass: string redoButtonClass: string saveDataButtonClass: string The classes of the draw, clear, undo and redo buttons. These classes are used in "" tags.

Example:

xml
[drawButtonClass]="'fa fa-pencil fa-2x'"
[clearButtonClass]="'fa fa-eraser fa-2x canvas_whiteboard_button-clear'"
drawButtonEnabled: boolean (default: true)

clearButtonEnabled: boolean (default: true) undoButtonEnabled: boolean (default: false) redoButtonEnabled: boolean (default: false) saveDataButtonEnabled: boolean (default: false) Specifies whether or not the button for drawing or clearing the canvas should be shown.

drawButtonText, clearButtonText, undoButtonText, redoButtonText, saveDataButtonText

Specify the text to add to the buttons, default is no text

xml
[drawButtonText]="'Draw'"
[clearButtonText]="'Clear'"

##Use the options: CanvasWhiteboardOptions to send the inputs Changes to this object will be detected by the canvas in the OnChange listener and will be changed accordingly

typescript
  //Component
  canvasOptions: CanvasWhiteboardOptions = {
      drawButtonEnabled: true,
      drawButtonClass: 'drawButtonClass',
      drawButtonText: 'Draw',
      clearButtonEnabled: true,
      clearButtonClass: 'clearButtonClass',
      clearButtonText: 'Clear',
      undoButtonText: 'Undo',
      undoButtonEnabled: true,
      redoButtonText: 'Redo',
      redoButtonEnabled: true,
      colorPickerEnabled: true,
      saveDataButtonEnabled: true,
      saveDataButtonText: 'Save',
      lineWidth: 4,
      scaleFactor: 1
  };

   //View
  
  

NOTE: In order for the changeDetection to pick up the options changes, you must change the options reference whenever you want to change a value. Example:

typescript
  public changeOptions(): void {
    this.canvasOptions = {
      ...this.canvasOptions,
      fillColorPickerEnabled: true,
      colorPickerEnabled: false
    };
  }

To add text to the buttons via css

Each button has its on class (example: Draw button -> .canvas_whiteboard_button-draw)

This button can be customized by overriding its css

css
.canvas_whiteboard_button-draw:before {
  content: "Draw";
}

will add the "Draw" text to the button.

If using component-only styles, for this to work the viewEncapsulation must be set to None.

typescript
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})

You can also use the ::ng-deep selector if you do not want to change the ViewEncapsulation property on the component.

Deprecated: colorPickerEnabled: boolean (default: false)

This allows the adding of a colorPickers that the user can choose to draw with (stroke and fill color), and the original colors will be used when redrawing

fillColorPickerEnabled: boolean (default: false)

This shows/hides the fill color picker. Note: if this field has been to false, but the colorPickerEnabled field has been to true, the color picker will be shown, as per reverse-compat needs.

fillColorPickerText: string (default: 'Fill')

Specify the text to add to the fill color picker button. Default is 'Fill' for reverse-compat needs.

strokeColorPickerEnabled: boolean (default: false)

This shows/hides the stroke color picker. Note: if this field has been to false, but the colorPickerEnabled field has been set to true, the color picker will be shown, as per reverse-compat needs.

strokeColorPickerText: string (default: 'Stroke')

Specify the text to add to the stroke color picker button. Default is 'Stroke' for reverse-compat needs.

lineWidth: number (default: 2)

This input controls the drawing pencil size

strokeColor: string (default: "rgba(0, 0, 0, 1)")

This input control the color of the brush

fillColor: string (default: "rgba(0, 0, 0, 0) -> transparent")

This input control the background color of the shapes

shouldDownloadDrawing: boolean (default: true)

This input control if the image created when clicking the save button should be downloaded right away.

startingColor: string (default: "#fff")

This input control is used to fill the canvas with the specified color at initialization and on resize events.

scaleFactor: number (default: 0)

This input controls the generation of the X and Y coordinates with a given scaleOffset. If not provided, the current with and height of the bounding rect and the canvas object will be used so that it works when transforming the canvas with css.

drawingEnabled: boolean (default: false)

This input controls if the drawing should be enabled from the start, instead of waiting for the user to click draw

showStrokeColorPicker: boolean (default: false)

This input controls if the CanvasWhiteboardColorPickerComponent for the strokeColor should be shown programmatically

showFillColorPicker: boolean (default: false)

This input controls if the CanvasWhiteboardColorPickerComponent for the fillColor should be shown programmatically

downloadedFileName: string (no default value)

This input controls the name of the file that will be downloaded when an image is saved. If the downloadCanvasImage method is called with a fileName as a third parameter, then it will have priority over everything If the fileName is not provided, then this Input will have priority. If this input is not provided as well, the image will be saved as canvas_drawing_" + new Date().valueOf(); At the end the file extension will be added so that it can be opened by a particular app.

lineJoin: string (default: "round")

The lineJoin property sets or returns the type of corner created, when two lines meet.

lineCap: string (default: "round")

The lineCap property sets or returns the style of the end caps for a line.

shapeSelectorEnabled: boolean (default: true)

This input controls if the CanvasWhiteboardShapeSelectorComponent will be enabled so that the user can pick other shapes via the View

showShapeSelector: boolean (default: false)

This input controls if the CanvasWhiteboardShapeSelectorComponent should be shown programmatically

Event emitters

typescript
 @Output() onClear = new EventEmitter();
 @Output() onBatchUpdate = new EventEmitter();
 @Output() onImageLoaded = new EventEmitter();
 @Output() onUndo = new EventEmitter();
 @Output() onRedo = new EventEmitter();
 @Output() onSave = new EventEmitter();

onClear is emitted when the canvas has been cleared.

onImageLoaded is emitted if the user specified an image and it has successfully been drawn on the canvas. onUndo is emitted when the canvas has done an UNDO function, emits an UUID (string) for the continuous last drawn shape undone.

onClear is emitted when the canvas has done a REDO function, emits an UUID (string) for the continuous shape redrawn.

onSave is emitted when the canvas has done a SAVE function, emits a Data URL or a Blob (IE).

Canvas Whiteboard Shapes

Every shape in the application extends the abstract class CanvasWhiteboardShape. This class adds predefined methods so that the creator of the shape can follow them and decide how his shape should be drawn.

Each shape is made of a starting position point of type CanvasWhiteboardPoint, and an options object which may be different for each shape, and it's of type CanvasWhiteboardShapeOptions.

Each predefined shape must know how to:

  • Return the name of the shape (should be the same as the name of the class)
  • Draw itself given a canvas context
  • Draw it's preview given a canvas

Issues· 48 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言