Skip to content

Commit 5c5a206

Browse files
author
Kevin Busch
committed
Updated pencil tool to capture data in more semantic way
1 parent a9089e4 commit 5c5a206

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

src/atoms/forms/canvas-sketch/tools/pencil-canvas-draw-tool.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ import { CanvasObjectType } from "../enums/canvas-object-type";
99
// #region Interfaces
1010
// -------------------------------------------------------------------------------------------------
1111

12+
enum PathType {
13+
Finishing = "F",
14+
Moving = "M",
15+
Starting = "S",
16+
}
17+
1218
interface PencilStrokeSettings extends CanvasDrawToolSettings {
13-
path: any[];
19+
path: [PathType, number, number][];
1420
}
1521

1622
// #endregion Interfaces
1723

1824
class PencilCanvasDrawTool extends BaseCanvasDrawTool implements CanvasDrawTool {
1925
public toolType: CanvasToolType;
2026

27+
2128
protected _path: PointerPosition[];
2229

2330
constructor(drawToolConfig: DrawToolConfig) {
@@ -43,23 +50,23 @@ class PencilCanvasDrawTool extends BaseCanvasDrawTool implements CanvasDrawTool
4350
(strokes as PencilStrokeSettings[]).forEach((stroke: PencilStrokeSettings, strokeI: number) => {
4451
let lastX: number = 0;
4552
let lastY: number = 0;
46-
stroke.path.forEach((path: any, pathI: number) => {
53+
stroke.path.forEach((path: [PathType, number, number], pathI: number) => {
4754
const type = path[0];
4855
const color = stroke.stroke;
4956
const width = stroke.strokeWidth;
50-
if (type === "M") {
57+
if (type === PathType.Starting) {
5158
// started stroke
5259
this._drawStroke(path[1], path[2], path[1], path[2], color, width);
5360
lastX = path[1];
5461
lastY = path[2];
5562
}
56-
if (type === "Q") {
63+
if (type === PathType.Moving) {
5764
// moving
58-
this._drawStroke(lastX, lastY, path[3], path[4], color, width);
65+
this._drawStroke(lastX, lastY, path[1], path[2], color, width);
5966
lastX = path[1];
6067
lastY = path[2];
6168
}
62-
if (type === "L") {
69+
if (type === PathType.Finishing) {
6370
// ended stroke
6471
this._drawStroke(lastX, lastY, path[1], path[2], color, width);
6572
lastX = path[1];
@@ -162,20 +169,20 @@ class PencilCanvasDrawTool extends BaseCanvasDrawTool implements CanvasDrawTool
162169
/**
163170
* Returns the path of the entire stroke that can then be persisted for later use
164171
*/
165-
private _getPath(): any[][] {
166-
const reformattedPath: any[] = [];
172+
private _getPath(): [PathType, number, number][] {
173+
const reformattedPath: [PathType, number, number][] = [];
167174
this._path.forEach((value: PointerPosition, index: number) => {
168175
if (index === 0) {
169176
// starting point
170-
reformattedPath.push(["M", value.x, value.y]);
177+
reformattedPath.push([PathType.Starting, value.x, value.y]);
171178
}
172179
else if (index + 1 === this._path.length) {
173180
// ending point
174-
reformattedPath.push(["L", value.x, value.y]);
181+
reformattedPath.push([PathType.Finishing, value.x, value.y]);
175182
}
176183
else {
177184
// moving point
178-
reformattedPath.push(["L", value.x, value.y]);
185+
reformattedPath.push([PathType.Moving, value.x, value.y]);
179186
}
180187
});
181188

0 commit comments

Comments
 (0)