ensure that fixed points stay fixed (#1523)

This PR fixes points on resize as well as on create. This should help
with file size for large resizes.

### Change Type
- [x] `patch` — Bug Fix

### Test Plan

- [ ] Unit Tests
This commit is contained in:
Steve Ruiz 2023-06-05 19:04:13 +01:00 committed by GitHub
parent a4fff303bf
commit 3be19ad53e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import {
linesIntersect, linesIntersect,
pointInPolygon, pointInPolygon,
setStrokePointRadii, setStrokePointRadii,
toFixed,
Vec2d, Vec2d,
VecLike, VecLike,
} from '@tldraw/primitives' } from '@tldraw/primitives'
@ -279,8 +280,8 @@ export class DrawShapeUtil extends ShapeUtil<TLDrawShape> {
...segment, ...segment,
points: segment.points.map(({ x, y, z }) => { points: segment.points.map(({ x, y, z }) => {
return { return {
x: scaleX * x, x: toFixed(scaleX * x),
y: scaleY * y, y: toFixed(scaleY * y),
z, z,
} }
}), }),

View file

@ -1,4 +1,4 @@
import { Matrix2d, snapAngle, Vec2d } from '@tldraw/primitives' import { Matrix2d, snapAngle, toFixed, Vec2d } from '@tldraw/primitives'
import { import {
createShapeId, createShapeId,
TLDrawShape, TLDrawShape,
@ -607,8 +607,8 @@ export class Drawing extends StateNode {
{ {
id: newShapeId, id: newShapeId,
type: this.shapeType, type: this.shapeType,
x: currentPagePoint.x, x: toFixed(currentPagePoint.x),
y: currentPagePoint.y, y: toFixed(currentPagePoint.y),
props: { props: {
isPen: this.isPen, isPen: this.isPen,
segments: [ segments: [

View file

@ -23,8 +23,8 @@ Object {
"z": 0.5, "z": 0.5,
}, },
Object { Object {
"x": 110.00000000000001, "x": 110,
"y": 110.00000000000001, "y": 110,
"z": 0.5, "z": 0.5,
}, },
], ],

View file

@ -631,6 +631,9 @@ export const TAU: number;
// @public // @public
export function toDomPrecision(v: number): number; export function toDomPrecision(v: number): number;
// @public (undocumented)
export function toFixed(v: number): number;
// @public // @public
export function toPrecision(n: number, precision?: number): number; export function toPrecision(n: number, precision?: number): number;

View file

@ -92,5 +92,6 @@ export {
simplify2, simplify2,
snapAngle, snapAngle,
toDomPrecision, toDomPrecision,
toFixed,
toPrecision, toPrecision,
} from './lib/utils' } from './lib/utils'

View file

@ -667,3 +667,10 @@ export function getHeight(pts: VecLike[]) {
export function toDomPrecision(v: number) { export function toDomPrecision(v: number) {
return +v.toFixed(4) return +v.toFixed(4)
} }
/**
* @public
*/
export function toFixed(v: number) {
return +v.toFixed(2)
}