fixes rotation on drawn shapes

This commit is contained in:
Steve Ruiz 2021-06-06 21:49:15 +01:00
parent 4c876d3d35
commit 45fd645885
4 changed files with 21 additions and 19 deletions

View file

@ -1,6 +1,6 @@
import styled from 'styles'
import { useSelector } from 'state'
import { deepCompareArrays, getPage } from 'utils/utils'
import { deepCompareArrays, getBoundsCenter, getPage } from 'utils/utils'
import { getShapeUtils } from 'lib/shape-utils'
import useShapeEvents from 'hooks/useShapeEvents'
import { memo, useRef } from 'react'
@ -38,12 +38,10 @@ export const ShapeOutline = memo(function ShapeOutline({ id }: { id: string }) {
// to handle parent rotation.
const center = getShapeUtils(shape).getCenter(shape)
const bounds = getShapeUtils(shape).getBounds(shape)
const transform = `
rotate(${shape.rotation * (180 / Math.PI)},
${center})
translate(${bounds.minX},${bounds.minY})
rotate(${shape.rotation * (180 / Math.PI)}, ${center})
translate(${shape.point})
`
return (

View file

@ -2,7 +2,7 @@ import React, { useRef, memo } from 'react'
import { useSelector } from 'state'
import styled from 'styles'
import { getShapeUtils } from 'lib/shape-utils'
import { getPage } from 'utils/utils'
import { getBoundsCenter, getPage } from 'utils/utils'
import { ShapeStyles, ShapeType } from 'types'
import useShapeEvents from 'hooks/useShapeEvents'
import * as vec from 'utils/vec'
@ -30,10 +30,12 @@ function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
const isGroup = shape.type === ShapeType.Group
const center = getShapeUtils(shape).getCenter(shape)
const rotation = shape.rotation * (180 / Math.PI)
const transform = `
rotate(${shape.rotation * (180 / Math.PI)}, ${vec.sub(center, parentPoint)})
translate(${vec.sub(shape.point, parentPoint)})
translate(${vec.neg(parentPoint)})
rotate(${rotation}, ${center})
translate(${shape.point})
`
const style = getShapeStyle(shape.style)

View file

@ -72,21 +72,14 @@ const draw = registerShapeUtils<DrawShape>({
},
getRotatedBounds(shape) {
const rBounds = translateBounds(
return translateBounds(
getBoundsFromPoints(shape.points, shape.rotation),
shape.point
)
const bounds = this.getBounds(shape)
const delta = vec.sub(getBoundsCenter(bounds), getBoundsCenter(rBounds))
return translateBounds(rBounds, delta)
},
getCenter(shape) {
const bounds = this.getRotatedBounds(shape)
return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
return getBoundsCenter(this.getBounds(shape))
},
hitTest(shape, point) {
@ -131,11 +124,11 @@ const draw = registerShapeUtils<DrawShape>({
shape.points = initialShape.points.map(([x, y]) => {
return [
bounds.width *
(scaleX < 0
(scaleX < 0 // * sin?
? 1 - x / initialShapeBounds.width
: x / initialShapeBounds.width),
bounds.height *
(scaleY < 0
(scaleY < 0 // * cos?
? 1 - y / initialShapeBounds.height
: y / initialShapeBounds.height),
]

View file

@ -1 +1,10 @@
# Shape Utils
## Text
- Click to create a shape, type to add its content
- Click to select
- Double click (long press?) to begin editing
- Deselect to confirm changes
- Escape to revert changes
- If confirmed changes and text is empty, delete shape