@nativescript-community/ui-drawingcanvas
v0.2.4
Published
Interactive drawing canvas plugin for NativeScript – draw shapes, images and freehand paths with multiple modes, layers, undo/redo and JSON serialization.
Downloads
327
Readme
Table of Contents
- Features
- Installation
- Usage
- Drawing Modes
- Undo / Redo
- Layer Management
- JSON Serialization
- Path Simplification
- Custom Modes
- Custom Shape Types
- Events
- Demos and Development
- Contributing
- Questions
An interactive drawing canvas plugin for NativeScript, built on top of @nativescript-community/ui-canvas.
Features
- Multiple drawing modes: pen/freehand, rectangle, ellipse, arrow, image placement, select/transform, and move (pan)
- Layer management: access shapes as an ordered list of layers – reorder, remove, duplicate
- Undo / Redo: full history with configurable depth
- Shape selection & transform: resize, rotate and move any shape with on-canvas handles
- Colour & style: stroke colour, fill colour, stroke width and opacity per shape
- JSON serialization: export the entire canvas to a compact JSON string and re-import it later
- Path simplification: automatic Douglas-Peucker + Catmull-Rom smoothing for pen strokes (configurable, extensible)
- Extensible: register custom drawing modes and custom shape types
Installation
Run the following command from the root of your project:
ns plugin add @nativescript-community/ui-drawingcanvas
Usage
NativeScript Core / TypeScript
import { DrawingCanvas } from '@nativescript-community/ui-drawingcanvas';
const dc = new DrawingCanvas();
dc.strokeColor = new Color('red');
dc.strokeWidth = 3;
dc.setMode('pen'); // 'pen' | 'select' | 'move' | 'rectangle' | 'ellipse' | 'arrow' | 'image'Vue (NativeScript-Vue)
// app.ts / main.ts
import DrawingCanvasPlugin from '@nativescript-community/ui-drawingcanvas/vue';
Vue.use(DrawingCanvasPlugin);<DrawingCanvas ref="dc" width="100%" height="100%" />Drawing Modes
| Mode | Description |
|------|-------------|
| pen | Freehand stroke |
| rectangle | Tap-drag to draw a rectangle |
| ellipse | Tap-drag to draw an ellipse / circle |
| arrow | Tap-drag to draw an arrow |
| image | Tap to place an image (set imageSource on the ImageMode first) |
| select | Tap to select shapes; drag handles to resize / rotate; drag body to move |
| move | Pan gesture (emits pan event with dx/dy) |
Undo / Redo
dc.undo();
dc.redo();
console.log(dc.canUndo, dc.canRedo);
// Configure undo history depth (default: 50)
dc.maxUndoDepth = 30;Layer Management
const layers = dc.layers; // DrawableShape[]
dc.moveLayerUp(shape);
dc.moveLayerDown(shape);
dc.duplicateShape(shape);
dc.removeLayer(shape);JSON Serialization
const json = dc.exportJSON();
// later …
dc.importJSON(json);Path Simplification
dc.simplificationOptions = {
enabled: true,
epsilon: 2, // Douglas-Peucker tolerance in dp
smoothing: true, // apply Catmull-Rom spline
splineSegments: 8,
splineAlpha: 0.5 // 0=uniform, 0.5=centripetal (recommended)
};Custom Modes
import { DrawingMode } from '@nativescript-community/ui-drawingcanvas';
class MyMode extends DrawingMode {
readonly name = 'mymode';
onTouchEnd(point) {
// … create and commit a shape
this.canvas.commitShape(myShape);
}
}
dc.registerMode(new MyMode(dc));
dc.setMode('mymode');Custom Shape Types
import { CustomShape } from '@nativescript-community/ui-drawingcanvas';
// Register a factory so importJSON can recreate the shape
dc.registerShapeFactory('star', () => new StarShape());Events
| Event | Payload | Description |
|-------|---------|-------------|
| shapeAdded | { shape } | Fired when a shape is committed |
| selectionChange | { shape } | Fired when the selected shape changes in select mode |
| modeChange | { mode } | Fired when the active mode changes |
| historyChange | { canUndo, canRedo } | Fired after undo / redo stack changes |
| pan | { dx, dy } | Fired by move mode on each gesture move |
Examples:
- Basic
- A basic Drawing example
Demos and Development
Repo Setup
The repo uses submodules. If you did not clone with --recursive then you need to call
git submodule update --initThe package manager used to install and link dependencies must be pnpm or yarn. npm wont work.
To develop and test:
if you use yarn then run yarn
if you use pnpm then run pnpm i
Interactive Menu:
To start the interactive menu, run npm start (or yarn start or pnpm start). This will list all of the commonly used scripts.
Build
npm run build.allWARNING: it seems yarn build.all wont always work (not finding binaries in node_modules/.bin) which is why the doc explicitly uses npm run
Demos
npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # ExampleDemo setup is a bit special in the sense that if you want to modify/add demos you dont work directly in demo-[ng|react|svelte|vue]
Instead you work in demo-snippets/[ng|react|svelte|vue]
You can start from the install.ts of each flavor to see how to register new demos
Contributing
Update repo
You can update the repo files quite easily
First update the submodules
npm run updateThen commit the changes Then update common files
npm run syncThen you can run yarn|pnpm, commit changed files if any
Update readme
npm run readmeUpdate doc
npm run docPublish
The publishing is completely handled by lerna (you can add -- --bump major to force a major release)
Simply run
npm run publishmodifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules.
One easy solution is t modify ~/.gitconfig and add
[url "ssh://[email protected]/"]
pushInsteadOf = https://github.com/Questions
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
