Skip to content

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

feat(freehand): freehand support theme

pubuzhixing8b7c79656c4ac
diff --git a/packages/drawnix/src/plugins/freehand/freehand.generator.ts b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
index 7442333..fb7a0af 100644
--- a/packages/drawnix/src/plugins/freehand/freehand.generator.ts
+++ b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
@@ -1,12 +1,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';
+import { Freehand } from './type';
+import {
+ gaussianSmooth,
+ getFillByElement,
+ getStrokeColorByElement,
+} from './utils';
+import { getStrokeWidthByElement } from '@plait/draw';
export class FreehandGenerator extends Generator<Freehand> {
protected draw(element: Freehand): SVGGElement | undefined {
- const option: Options = { ...DefaultFreehand };
+ const strokeWidth = getStrokeWidthByElement(element);
+ const strokeColor = getStrokeColorByElement(this.board, element);
+ const fill = getFillByElement(this.board, element);
+ const option: Options = { strokeWidth, stroke: strokeColor, fill };
const g = PlaitBoard.getRoughSVG(this.board).curve(
gaussianSmooth(element.points, 1, 3),
option
diff --git a/packages/drawnix/src/plugins/freehand/type.ts b/packages/drawnix/src/plugins/freehand/type.ts
index 60a43f7..67d25de 100644
--- a/packages/drawnix/src/plugins/freehand/type.ts
+++ b/packages/drawnix/src/plugins/freehand/type.ts
@@ -1,9 +1,31 @@
-import { DEFAULT_COLOR, Point } from '@plait/core';
+import { DEFAULT_COLOR, Point, ThemeColorMode } from '@plait/core';
import { PlaitCustomGeometry } from '@plait/draw';
-export const DefaultFreehand = {
- strokeColor: DEFAULT_COLOR,
- strokeWidth: 2,
+export const FreehandThemeColors = {
+ [ThemeColorMode.default]: {
+ strokeColor: DEFAULT_COLOR,
+ fill: '#FFFFFF'
+ },
+ [ThemeColorMode.colorful]: {
+ strokeColor: '#06ADBF',
+ fill: '#CDEFF2'
+ },
+ [ThemeColorMode.soft]: {
+ strokeColor: '#6D89C1',
+ fill: '#DADFEB'
+ },
+ [ThemeColorMode.retro]: {
+ strokeColor: '#E9C358',
+ fill: '#F6EDCF'
+ },
+ [ThemeColorMode.dark]: {
+ strokeColor: '#FFFFFF',
+ fill: '#434343'
+ },
+ [ThemeColorMode.starry]: {
+ strokeColor: '#42ABE5',
+ fill: '#163F5A'
+ }
};
export enum FreehandShape {
diff --git a/packages/drawnix/src/plugins/freehand/utils.ts b/packages/drawnix/src/plugins/freehand/utils.ts
index 9c32806..84d4e69 100644
--- a/packages/drawnix/src/plugins/freehand/utils.ts
+++ b/packages/drawnix/src/plugins/freehand/utils.ts
@@ -3,14 +3,18 @@ import {
idCreator,
isPointInPolygon,
PlaitBoard,
+ PlaitElement,
Point,
RectangleClient,
rotateAntiPointsByElement,
Selection,
+ ThemeColorMode,
} from '@plait/core';
-import { DefaultFreehand, Freehand, FreehandShape } from './type';
+import { Freehand, FreehandShape, FreehandThemeColors } from './type';
import {
+ DefaultDrawStyle,
isClosedPoints,
+ isCustomGeometryClosed,
isHitPolyLine,
isRectangleHitRotatedPoints,
} from '@plait/draw';
@@ -28,8 +32,6 @@ export const createFreehandElement = (
type: 'freehand',
shape,
points,
- strokeWidth: DefaultFreehand.strokeWidth,
- strokeColor: DefaultFreehand.strokeColor,
};
return element;
};
@@ -70,6 +72,34 @@ export const getSelectedFreehandElements = (board: PlaitBoard) => {
return getSelectedElements(board).filter((ele) => Freehand.isFreehand(ele));
};
+export const getFreehandDefaultStrokeColor = (theme: ThemeColorMode) => {
+ return FreehandThemeColors[theme].strokeColor;
+};
+
+export const getFreehandDefaultFill = (theme: ThemeColorMode) => {
+ return FreehandThemeColors[theme].fill;
+};
+
+export const getStrokeColorByElement = (
+ board: PlaitBoard,
+ element: PlaitElement
+) => {
+ const defaultColor = getFreehandDefaultStrokeColor(
+ board.theme.themeColorMode
+ );
+ const strokeColor = element.strokeColor || defaultColor;
+ return strokeColor;
+};
+
+export const getFillByElement = (board: PlaitBoard, element: PlaitElement) => {
+ const defaultFill =
+ Freehand.isFreehand(element) && isCustomGeometryClosed(board, element)
+ ? getFreehandDefaultFill(board.theme.themeColorMode)
+ : DefaultDrawStyle.fill;
+ const fill = element.fill || defaultFill;
+ return fill;
+};
+
export function gaussianWeight(x: number, sigma: number) {
return Math.exp(-(x * x) / (2 * sigma * sigma));
}