开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.
feat(eraser): drawing erasing trail animation effect (#295)
pubuzhixing8535af0ebad66
diff --git a/package-lock.json b/package-lock.jsonindex bc1b5aa..b10c937 100644--- a/package-lock.json+++ b/package-lock.json@@ -21,6 +21,7 @@"ahooks": "^3.8.0","browser-fs-access": "^0.35.0","classnames": "^2.5.1",+ "laser-pen": "^1.0.1","localforage": "^1.10.0","mobile-detect": "^1.4.5","open-color": "^1.9.1",@@ -13849,6 +13850,12 @@"language-subtag-registry": "~0.3.2"}},+ "node_modules/laser-pen": {+ "version": "1.0.1",+ "resolved": "https://registry.npmjs.org/laser-pen/-/laser-pen-1.0.1.tgz",+ "integrity": "sha512-+K3CQK5ryLDDjX0pEdSIRXh89F6KSpm225DEymWMheg2/umhUO+na/4l8MGs2gee7VO2Mx3kyG288WGrofasoA==",+ "license": "MIT"+ },"node_modules/layout-base": {"version": "1.0.2","resolved": "https://registry.npmjs.org/layout-base/-/layout-base-1.0.2.tgz",diff --git a/package.json b/package.jsonindex a41ddca..44ba127 100644--- a/package.json+++ b/package.json@@ -5,6 +5,7 @@"scripts": {"start": "nx serve web --host=0.0.0.0","build": "nx run-many -t=build",+ "lint": "nx run-many --target=lint --all --fix","build:web": "nx build web","test": "nx run-many -t=test","release": "node scripts/release-version.js",@@ -24,6 +25,7 @@"ahooks": "^3.8.0","browser-fs-access": "^0.35.0","classnames": "^2.5.1",+ "laser-pen": "^1.0.1","localforage": "^1.10.0","mobile-detect": "^1.4.5","open-color": "^1.9.1",diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsxindex 5b8c2aa..31ef7f5 100644--- a/packages/drawnix/src/drawnix.tsx+++ b/packages/drawnix/src/drawnix.tsx@@ -39,6 +39,7 @@ import { buildTextLinkPlugin } from './plugins/with-text-link';import { LinkPopup } from './components/popup/link-popup/link-popup';import { I18nProvider } from './i18n';import { Tutorial } from './components/tutorial';+import { LASER_POINTER_CLASS_NAME } from './utils/laser-pointer';export type DrawnixProps = {value: PlaitElement[];@@ -156,6 +157,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({<TTDDialog container={containerRef.current}></TTDDialog><CleanConfirm container={containerRef.current}></CleanConfirm></Wrapper>+ <canvas className={`${LASER_POINTER_CLASS_NAME} mouse-course-hidden`}></canvas></div></DrawnixContext.Provider></I18nProvider>diff --git a/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts b/packages/drawnix/src/plugins/freehand/with-freehand-erase.tsindex 6fe4bbd..b7302a9 100644--- a/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts+++ b/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts@@ -10,10 +10,13 @@ import { isDrawingMode } from '@plait/common';import { isHitFreehand } from './utils';import { Freehand, FreehandShape } from './type';import { CoreTransforms } from '@plait/core';+import { LaserPointer } from '../../utils/laser-pointer';export const withFreehandErase = (board: PlaitBoard) => {const { pointerDown, pointerMove, pointerUp, globalPointerUp } = board;+ const laserPointer = new LaserPointer();+let isErasing = false;const elementsToDelete = new Set<string>();@@ -49,6 +52,7 @@ export const withFreehandErase = (board: PlaitBoard) => {deleteMarkedElements();isErasing = false;elementsToDelete.clear();+ laserPointer.destroy();}};@@ -60,6 +64,7 @@ export const withFreehandErase = (board: PlaitBoard) => {elementsToDelete.clear();const currentPoint: Point = [event.x, event.y];checkAndMarkFreehandElementsForDeletion(currentPoint);+ laserPointer.init(board);return;}diff --git a/packages/drawnix/src/styles/index.scss b/packages/drawnix/src/styles/index.scssindex 4e6925f..1a8fe47 100644--- a/packages/drawnix/src/styles/index.scss+++ b/packages/drawnix/src/styles/index.scss@@ -134,6 +134,19 @@cursor: zoom-in;}}++ .laser-pointer {+ background: transparent;+ position: fixed;+ left: 0;+ top: 0;+ z-index: 2022;+ width: 100vw;+ height: 100vh;+ &.mouse-course-hidden {+ pointer-events: none;+ }+ }}.plait-board-container {diff --git a/packages/drawnix/src/utils/laser-pointer.ts b/packages/drawnix/src/utils/laser-pointer.tsnew file mode 100644index 0000000..6126093--- /dev/null+++ b/packages/drawnix/src/utils/laser-pointer.ts@@ -0,0 +1,143 @@+import { PlaitBoard } from '@plait/core';+import {+ drainPoints,+ drawLaserPen,+ IOriginalPointData,+ setColor,+ setDelay,+ setMaxWidth,+ setMinWidth,+ setOpacity,+ setRoundCap,+} from 'laser-pen';++export const LASER_POINTER_CLASS_NAME = 'laser-pointer';++const calculateRatio = (context: any): number => {+ const backingStore =+ context.backingStorePixelRatio ||+ context.webkitBackingStorePixelRatio ||+ context.mozBackingStorePixelRatio ||+ context.msBackingStorePixelRatio ||+ context.oBackingStorePixelRatio ||+ context.backingStorePixelRatio ||+ 1;+ return (window.devicePixelRatio || 1) / backingStore;+};++export class LaserPointer {+ private mouseTrack: IOriginalPointData[] = [];+ private mouseMoveHandler: ((event: MouseEvent) => void) | null = null;+ private resizeHandler: (() => void) | null = null;+ private cvsDom: HTMLCanvasElement | null = null;+ private ctx: CanvasRenderingContext2D | null = null;+ private canvasPos: DOMRect | null = null;+ private drawing = false;+ private container: HTMLElement | null = null;++ public init(board: PlaitBoard): void {+ this.container = PlaitBoard.getBoardContainer(board).closest(+ '.drawnix'+ ) as HTMLElement;+ this.cvsDom = this.container.querySelector(+ `.${LASER_POINTER_CLASS_NAME}`+ ) as HTMLCanvasElement;+ this.ctx = this.cvsDom.getContext('2d') as CanvasRenderingContext2D;+ this.canvasPos = this.cvsDom.getBoundingClientRect();++ this.mouseMoveHandler = (event: MouseEvent) => {+ if (!this.canvasPos) return;+ const relativeX = event.clientX - this.canvasPos.x;+ const relativeY = event.clientY - this.canvasPos.y;+ this.mouseTrack.push({+ x: relativeX,+ y: relativeY,+ time: Date.now(),+ });+ this.ctx && this.startDraw();+ };++ this.resizeHandler = () => this.setCanvasSize();+ this.container.addEventListener('pointermove', this.mouseMoveHandler);+ window.addEventListener('resize', this.resizeHandler);++ this.setCanvasSize();+ }++ public destroy(): void {+ if (this.mouseMoveHandler && this.container) {+ this.container.removeEventListener('pointermove', this.mouseMoveHandler);+ this.mouseMoveHandler = null;+ }++ if (this.resizeHandler) {+ window.removeEventListener('resize', this.resizeHandler);+ this.resizeHandler = null;+ }++ if (this.ctx && this.cvsDom) {+ this.ctx.clearRect(0, 0, this.cvsDom.width, this.cvsDom.height);+ }++ this.cvsDom = null;+ this.ctx = null;+ this.canvasPos = null;+ this.drawing = false;+ }++ private startDraw(): void {+ if (!this.drawing) {+ this.drawing = true;+ this.draw();+ }+ }++ private draw(): void {+ if (!this.ctx || !this.cvsDom) return;++ this.ctx.clearRect(0, 0, this.cvsDom.width, this.cvsDom.height);+ let needDrawInNextFrame = false;++ this.mouseTrack = drainPoints(this.mouseTrack);+ if (this.mouseTrack.length >= 3) {+ setColor(211, 211, 211);+ setDelay(180);+ setRoundCap(true);+ setMaxWidth(10);+ setMinWidth(0);+ setOpacity(0.6);+ drawLaserPen(this.ctx, this.mouseTrack);+ needDrawInNextFrame = true;+ } else {+ const centerPoint = this.mouseTrack[this.mouseTrack.length - 1];+ if (!centerPoint) return;++ this.ctx.save();+ this.ctx.beginPath();+ this.ctx.fillStyle = `rgba(211, 211, 211)`;+ this.ctx.arc(centerPoint.x, centerPoint.y, 5, 0, Math.PI * 2, false);+ this.ctx.closePath();+ this.ctx.fill();+ this.ctx.restore();+ }++ if (needDrawInNextFrame) {+ requestAnimationFrame(() => this.draw());+ } else {+ this.drawing = false;+ }+ }++ private setCanvasSize(): void {+ if (!this.cvsDom || !this.ctx) return;++ const rect = this.cvsDom.getBoundingClientRect();+ const ratio = calculateRatio(this.ctx);++ this.cvsDom.setAttribute('width', `${rect.width * ratio}px`);+ this.cvsDom.setAttribute('height', `${rect.height * ratio}px`);+ this.ctx.scale(ratio, ratio);++ this.canvasPos = this.cvsDom.getBoundingClientRect();+ }+}