|
| 1 | +import { PlaitBoard } from '@plait/core'; |
| 2 | +import { |
| 3 | + drainPoints, |
| 4 | + drawLaserPen, |
| 5 | + IOriginalPointData, |
| 6 | + setColor, |
| 7 | + setDelay, |
| 8 | + setMaxWidth, |
| 9 | + setMinWidth, |
| 10 | + setOpacity, |
| 11 | + setRoundCap, |
| 12 | +} from 'laser-pen'; |
| 13 | + |
| 14 | +export const LASER_POINTER_CLASS_NAME = 'laser-pointer'; |
| 15 | + |
| 16 | +const calculateRatio = (context: any): number => { |
| 17 | + const backingStore = |
| 18 | + context.backingStorePixelRatio || |
| 19 | + context.webkitBackingStorePixelRatio || |
| 20 | + context.mozBackingStorePixelRatio || |
| 21 | + context.msBackingStorePixelRatio || |
| 22 | + context.oBackingStorePixelRatio || |
| 23 | + context.backingStorePixelRatio || |
| 24 | + 1; |
| 25 | + return (window.devicePixelRatio || 1) / backingStore; |
| 26 | +}; |
| 27 | + |
| 28 | +export class LaserPointer { |
| 29 | + private mouseTrack: IOriginalPointData[] = []; |
| 30 | + private mouseMoveHandler: ((event: MouseEvent) => void) | null = null; |
| 31 | + private resizeHandler: (() => void) | null = null; |
| 32 | + private cvsDom: HTMLCanvasElement | null = null; |
| 33 | + private ctx: CanvasRenderingContext2D | null = null; |
| 34 | + private canvasPos: DOMRect | null = null; |
| 35 | + private drawing = false; |
| 36 | + private container: HTMLElement | null = null; |
| 37 | + |
| 38 | + public init(board: PlaitBoard): void { |
| 39 | + this.container = PlaitBoard.getBoardContainer(board).closest( |
| 40 | + '.drawnix' |
| 41 | + ) as HTMLElement; |
| 42 | + this.cvsDom = this.container.querySelector( |
| 43 | + `.${LASER_POINTER_CLASS_NAME}` |
| 44 | + ) as HTMLCanvasElement; |
| 45 | + this.ctx = this.cvsDom.getContext('2d') as CanvasRenderingContext2D; |
| 46 | + this.canvasPos = this.cvsDom.getBoundingClientRect(); |
| 47 | + |
| 48 | + this.mouseMoveHandler = (event: MouseEvent) => { |
| 49 | + if (!this.canvasPos) return; |
| 50 | + const relativeX = event.clientX - this.canvasPos.x; |
| 51 | + const relativeY = event.clientY - this.canvasPos.y; |
| 52 | + this.mouseTrack.push({ |
| 53 | + x: relativeX, |
| 54 | + y: relativeY, |
| 55 | + time: Date.now(), |
| 56 | + }); |
| 57 | + this.ctx && this.startDraw(); |
| 58 | + }; |
| 59 | + |
| 60 | + this.resizeHandler = () => this.setCanvasSize(); |
| 61 | + this.container.addEventListener('pointermove', this.mouseMoveHandler); |
| 62 | + window.addEventListener('resize', this.resizeHandler); |
| 63 | + |
| 64 | + this.setCanvasSize(); |
| 65 | + } |
| 66 | + |
| 67 | + public destroy(): void { |
| 68 | + if (this.mouseMoveHandler && this.container) { |
| 69 | + this.container.removeEventListener('pointermove', this.mouseMoveHandler); |
| 70 | + this.mouseMoveHandler = null; |
| 71 | + } |
| 72 | + |
| 73 | + if (this.resizeHandler) { |
| 74 | + window.removeEventListener('resize', this.resizeHandler); |
| 75 | + this.resizeHandler = null; |
| 76 | + } |
| 77 | + |
| 78 | + if (this.ctx && this.cvsDom) { |
| 79 | + this.ctx.clearRect(0, 0, this.cvsDom.width, this.cvsDom.height); |
| 80 | + } |
| 81 | + |
| 82 | + this.cvsDom = null; |
| 83 | + this.ctx = null; |
| 84 | + this.canvasPos = null; |
| 85 | + this.drawing = false; |
| 86 | + } |
| 87 | + |
| 88 | + private startDraw(): void { |
| 89 | + if (!this.drawing) { |
| 90 | + this.drawing = true; |
| 91 | + this.draw(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private draw(): void { |
| 96 | + if (!this.ctx || !this.cvsDom) return; |
| 97 | + |
| 98 | + this.ctx.clearRect(0, 0, this.cvsDom.width, this.cvsDom.height); |
| 99 | + let needDrawInNextFrame = false; |
| 100 | + |
| 101 | + this.mouseTrack = drainPoints(this.mouseTrack); |
| 102 | + if (this.mouseTrack.length >= 3) { |
| 103 | + setColor(211, 211, 211); |
| 104 | + setDelay(180); |
| 105 | + setRoundCap(true); |
| 106 | + setMaxWidth(10); |
| 107 | + setMinWidth(0); |
| 108 | + setOpacity(0.6); |
| 109 | + drawLaserPen(this.ctx, this.mouseTrack); |
| 110 | + needDrawInNextFrame = true; |
| 111 | + } else { |
| 112 | + const centerPoint = this.mouseTrack[this.mouseTrack.length - 1]; |
| 113 | + if (!centerPoint) return; |
| 114 | + |
| 115 | + this.ctx.save(); |
| 116 | + this.ctx.beginPath(); |
| 117 | + this.ctx.fillStyle = `rgba(211, 211, 211)`; |
| 118 | + this.ctx.arc(centerPoint.x, centerPoint.y, 5, 0, Math.PI * 2, false); |
| 119 | + this.ctx.closePath(); |
| 120 | + this.ctx.fill(); |
| 121 | + this.ctx.restore(); |
| 122 | + } |
| 123 | + |
| 124 | + if (needDrawInNextFrame) { |
| 125 | + requestAnimationFrame(() => this.draw()); |
| 126 | + } else { |
| 127 | + this.drawing = false; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private setCanvasSize(): void { |
| 132 | + if (!this.cvsDom || !this.ctx) return; |
| 133 | + |
| 134 | + const rect = this.cvsDom.getBoundingClientRect(); |
| 135 | + const ratio = calculateRatio(this.ctx); |
| 136 | + |
| 137 | + this.cvsDom.setAttribute('width', `${rect.width * ratio}px`); |
| 138 | + this.cvsDom.setAttribute('height', `${rect.height * ratio}px`); |
| 139 | + this.ctx.scale(ratio, ratio); |
| 140 | + |
| 141 | + this.canvasPos = this.cvsDom.getBoundingClientRect(); |
| 142 | + } |
| 143 | +} |
0 commit comments