Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

feat(freehand): apply gaussianSmooth to freehand curve (#47)

pubuzhixing80bd561250756
diff --git a/packages/drawnix/src/plugins/freehand/freehand.generator.ts b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
index 81468c7..a4dd283 100644
--- a/packages/drawnix/src/plugins/freehand/freehand.generator.ts
+++ b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
@@ -2,11 +2,20 @@ import { Generator } from '@plait/common';
import { PlaitBoard, setStrokeLinecap } from '@plait/core';
import { Options } from 'roughjs/bin/core';
import { DefaultFreehand, Freehand } from './type';
+import { gaussianSmooth } from './utils';
-export class FreehandGenerator extends Generator<Freehand> {
- protected draw(element: Freehand): SVGGElement | undefined {
- const option: Options = { ...DefaultFreehand };
- const g = PlaitBoard.getRoughSVG(this.board).curve(element.points, option);
+export interface FreehandData {}
+
+export class FreehandGenerator extends Generator<Freehand, FreehandData> {
+ protected draw(
+ element: Freehand,
+ data?: FreehandData | undefined
+ ): SVGGElement | undefined {
+ let option: Options = { ...DefaultFreehand };
+ const g = PlaitBoard.getRoughSVG(this.board).curve(
+ gaussianSmooth(element.points, 1, 9),
+ option
+ );
setStrokeLinecap(g, 'round');
return g;
}
diff --git a/packages/drawnix/src/plugins/freehand/utils.ts b/packages/drawnix/src/plugins/freehand/utils.ts
index 1aa4829..8ec07fd 100644
--- a/packages/drawnix/src/plugins/freehand/utils.ts
+++ b/packages/drawnix/src/plugins/freehand/utils.ts
@@ -69,3 +69,37 @@ export const isRectangleHitFreehand = (
export const getSelectedFreehandElements = (board: PlaitBoard) => {
return getSelectedElements(board).filter((ele) => Freehand.isFreehand(ele));
};
+
+export function gaussianWeight(x: number, sigma: number) {
+ return Math.exp(-(x * x) / (2 * sigma * sigma));
+}
+
+export function gaussianSmooth(points: Point[], sigma: number, windowSize: number) {
+ if (points.length < 2) return points;
+
+ const halfWindow = Math.floor(windowSize / 2);
+ const smoothedPoints: Point[] = [];
+
+ for (let i = 0; i < points.length; i++) {
+ let sumX = 0;
+ let sumY = 0;
+ let weightSum = 0;
+
+ for (let j = -halfWindow; j <= halfWindow; j++) {
+ const idx = i + j;
+ if (idx >= 0 && idx < points.length) {
+ const weight = gaussianWeight(j, sigma);
+ sumX += points[idx][0] * weight;
+ sumY += points[idx][1] * weight;
+ weightSum += weight;
+ }
+ }
+
+ smoothedPoints.push([
+ sumX / weightSum,
+ sumY / weightSum
+ ]);
+ }
+
+ return smoothedPoints;
+}
\ No newline at end of file
diff --git a/packages/drawnix/src/plugins/freehand/with-freehand-create.ts b/packages/drawnix/src/plugins/freehand/with-freehand-create.ts
index 4c6a6ec..313fcc6 100644
--- a/packages/drawnix/src/plugins/freehand/with-freehand-create.ts
+++ b/packages/drawnix/src/plugins/freehand/with-freehand-create.ts
@@ -2,6 +2,7 @@ import {
PlaitBoard,
Point,
Transforms,
+ distanceBetweenPointAndPoint,
throttleRAF,
toHostPoint,
toViewBoxPoint,
@@ -22,6 +23,8 @@ export const withFreehandCreate = (board: PlaitBoard) => {
let temporaryElement: Freehand | null = null;
+ let previousScreenPoint: Point | null = null;
+
const complete = (cancel?: boolean) => {
if (isDrawing) {
const pointer = PlaitBoard.getPointer(board) as FreehandShape;
@@ -34,6 +37,7 @@ export const withFreehandCreate = (board: PlaitBoard) => {
temporaryElement = null;
isDrawing = false;
points = [];
+ previousScreenPoint = null;
};
board.pointerDown = (event: PointerEvent) => {
@@ -43,12 +47,18 @@ export const withFreehandCreate = (board: PlaitBoard) => {
isDrawing = true;
const point = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
points.push(point);
+ previousScreenPoint = [event.x, event.y];
}
pointerDown(event);
};
board.pointerMove = (event: PointerEvent) => {
- if (isDrawing) {
+ if (isDrawing && previousScreenPoint) {
+ const distance = distanceBetweenPointAndPoint(previousScreenPoint[0], previousScreenPoint[1], event.x, event.y);
+ if (distance < 2) {
+ return;
+ }
+ previousScreenPoint = [event.x, event.y];
throttleRAF(board, 'with-freehand-creation', () => {
generator?.destroy();
if (isDrawing) {