From 9c0b69167478d9ba214b5ffea67e99810e46b04d Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Sun, 20 Jun 2021 11:22:42 +0100 Subject: [PATCH] Restores dot tool (keyboard shortcut only) and improves fill shape --- hooks/useKeyboardEvents.ts | 7 +++++ lib/shape-utils/draw.tsx | 55 ++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/hooks/useKeyboardEvents.ts b/hooks/useKeyboardEvents.ts index 3ff399f74..ddbdc5567 100644 --- a/hooks/useKeyboardEvents.ts +++ b/hooks/useKeyboardEvents.ts @@ -155,6 +155,13 @@ export default function useKeyboardEvents() { } break } + case 'o': { + if (metaKey(e)) { + } else { + state.send('SELECTED_DOT_TOOL', getKeyboardEventInfo(e)) + } + break + } case 'v': { if (metaKey(e)) { state.send('PASTED', getKeyboardEventInfo(e)) diff --git a/lib/shape-utils/draw.tsx b/lib/shape-utils/draw.tsx index b672ef517..56ce457e0 100644 --- a/lib/shape-utils/draw.tsx +++ b/lib/shape-utils/draw.tsx @@ -3,20 +3,19 @@ import vec from 'utils/vec' import { DashStyle, DrawShape, ShapeStyles, ShapeType } from 'types' import { registerShapeUtils } from './index' import { intersectPolylineBounds } from 'utils/intersections' -import { boundsContain, boundsContainPolygon } from 'utils/bounds' -import getStroke from 'perfect-freehand' +import { boundsContain } from 'utils/bounds' +import getStroke, { getStrokePoints } from 'perfect-freehand' import { getBoundsCenter, getBoundsFromPoints, - getRotatedCorners, getSvgPathFromStroke, - rotateBounds, translateBounds, } from 'utils/utils' -import { defaultStyle, getShapeStyle } from 'lib/shape-styles' +import { defaultStyle, getShapeStyle, strokes } from 'lib/shape-styles' const rotatedCache = new WeakMap([]) const pathCache = new WeakMap([]) +const polygonCache = new WeakMap([]) const draw = registerShapeUtils({ boundsCache: new WeakMap([]), @@ -59,17 +58,24 @@ const draw = registerShapeUtils({ ) } + const shouldFill = + points.length > 3 && + vec.dist(points[0], points[points.length - 1]) < +styles.strokeWidth * 2 + + if (shouldFill && !polygonCache.has(points)) { + renderFill(shape, style) + } + return ( - {points.length > 3 && - vec.dist(points[0], points[points.length - 1]) < 8 && ( - pt.slice(0, 2)).join(',')} - fill={styles.fill} - stroke="none" - strokeWidth={0} - /> - )} + {shouldFill && ( + + )} ) @@ -218,3 +224,24 @@ function renderPath(shape: DrawShape, style: ShapeStyles) { pathCache.set(shape.points, getSvgPathFromStroke(stroke)) } + +function renderFill(shape: DrawShape, style: ShapeStyles) { + const styles = getShapeStyle(style) + + if (shape.points.length < 2) { + polygonCache.set(shape.points, '') + return + } + + return polygonCache.set( + shape.points, + getSvgPathFromStroke( + getStrokePoints(shape.points, { + size: 1 + +styles.strokeWidth * 2, + thinning: 0.85, + end: { taper: +styles.strokeWidth * 20 }, + start: { taper: +styles.strokeWidth * 20 }, + }).map((pt) => pt.point) + ) + ) +}