From db13880ceb1e92637c4d9c6bc3b0886da34f4808 Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Tue, 31 Aug 2021 16:20:01 +0100 Subject: [PATCH] Adds time data to draw points. Not sure if I'll keep this, but together with the improved trimming it actually reduces the size of these arrays. --- packages/tldraw/src/shape/shapes/draw/draw.tsx | 8 ++++++-- packages/tldraw/src/shape/shapes/text/text.tsx | 1 - .../src/state/session/sessions/draw/draw.session.ts | 10 ++++++++-- packages/tldraw/src/state/tlstate.ts | 4 ++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/tldraw/src/shape/shapes/draw/draw.tsx b/packages/tldraw/src/shape/shapes/draw/draw.tsx index f58e0147f..7d70f0dd4 100644 --- a/packages/tldraw/src/shape/shapes/draw/draw.tsx +++ b/packages/tldraw/src/shape/shapes/draw/draw.tsx @@ -267,10 +267,14 @@ export class Draw extends TLDrawShapeUtil { onSessionComplete(shape: DrawShape): Partial { const bounds = this.getBounds(shape) - const [x1, y1] = Vec.sub([bounds.minX, bounds.minY], shape.point) + const [x1, y1] = Vec.round(Vec.sub([bounds.minX, bounds.minY], shape.point)) + + const points = shape.points.map(([x0, y0, p, t]) => Vec.round([x0 - x1, y0 - y1]).concat(p, t)) + + console.log(points) return { - points: shape.points.map(([x0, y0, p]) => [x0 - x1, y0 - y1, p]), + points, point: Vec.add(shape.point, [x1, y1]), } } diff --git a/packages/tldraw/src/shape/shapes/text/text.tsx b/packages/tldraw/src/shape/shapes/text/text.tsx index 352cccb0c..8e4ffd9e7 100644 --- a/packages/tldraw/src/shape/shapes/text/text.tsx +++ b/packages/tldraw/src/shape/shapes/text/text.tsx @@ -245,7 +245,6 @@ export class Text extends TLDrawShapeUtil { getBounds(shape: TextShape): TLBounds { const bounds = Utils.getFromCache(this.boundsCache, shape, () => { if (!melm) { - console.log('no melm!') // We're in SSR return { minX: 0, minY: 0, maxX: 0, maxY: 0, width: 0, height: 0 } } diff --git a/packages/tldraw/src/state/session/sessions/draw/draw.session.ts b/packages/tldraw/src/state/session/sessions/draw/draw.session.ts index 804188bf1..ffd9fdc7a 100644 --- a/packages/tldraw/src/state/session/sessions/draw/draw.session.ts +++ b/packages/tldraw/src/state/session/sessions/draw/draw.session.ts @@ -12,6 +12,7 @@ export class DrawSession implements Session { snapshot: DrawSnapshot isLocked?: boolean lockedDirection?: 'horizontal' | 'vertical' + startTime: number constructor(data: Data, id: string, point: number[]) { this.origin = point @@ -24,6 +25,7 @@ export class DrawSession implements Session { // when the draw session ends; if the user hasn't added additional // points, this single point will be interpreted as a "dot" shape. this.points = [] + this.startTime = 0 } start = () => void null @@ -33,7 +35,8 @@ export class DrawSession implements Session { // Roundabout way of preventing the "dot" from showing while drawing if (this.points.length === 0) { - this.points.push([0, 0, pressure]) + this.startTime = Date.now() + this.points.push([0, 0, pressure, 0]) } // Drawing while holding shift will "lock" the pen to either the @@ -81,7 +84,10 @@ export class DrawSession implements Session { // Don't add duplicate points. It's important to test against the // adjusted (low-passed) point rather than the input point. - const newPoint = Vec.round([...Vec.sub(this.previous, this.origin), pressure]) + const newPoint = Vec.round(Vec.sub(this.previous, this.origin)).concat( + pressure, + Date.now() - this.startTime + ) if (Vec.isEqual(this.last, newPoint)) return diff --git a/packages/tldraw/src/state/tlstate.ts b/packages/tldraw/src/state/tlstate.ts index d76427a6c..77b4f9e90 100644 --- a/packages/tldraw/src/state/tlstate.ts +++ b/packages/tldraw/src/state/tlstate.ts @@ -982,8 +982,12 @@ export class TLDrawState extends StateManager { } selectAll = () => { + if (this.session) return this.setSelectedIds(Object.keys(this.page.shapes)) this.addToSelectHistory(this.selectedIds) + if (this.appState.activeTool !== 'select') { + this.selectTool('select') + } return this }