-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: line control handle(close #113)
- Loading branch information
Showing
14 changed files
with
192 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { IPoint, IRect, normalizeRadian, transformRotate } from '@suika/geo'; | ||
import { | ||
adjustSizeToKeepPolarSnap, | ||
getRectCenterPoint, | ||
getRectRotatedXY, | ||
} from '../../utils/geo'; | ||
import { IBox2WithRotation } from '../../type'; | ||
|
||
/** | ||
* Get the new position of the line when resizing | ||
* we consider the graph with 0 height as a line | ||
* | ||
* TODO: reuse, this is something same code in tool_draw_graph.ts | ||
*/ | ||
export const getResizedLine = ( | ||
/** control type, 'se' | 'ne' | 'nw' | 'sw' */ | ||
type: string, | ||
newPos: IPoint, | ||
oldBox: IBox2WithRotation, | ||
/** keep rotation in 0 45 90 ... */ | ||
keepPolarSnap: boolean, | ||
scaleFromCenter: boolean, | ||
) => { | ||
const isControlInLeft = type === 'nw' || type === 'sw'; | ||
|
||
const { x, y } = newPos; | ||
let startX: number; | ||
let startY: number; | ||
if (isControlInLeft) { | ||
const [cx, cy] = getRectCenterPoint(oldBox); | ||
const rightTop = transformRotate( | ||
oldBox.x + oldBox.width, | ||
oldBox.y + oldBox.height, | ||
oldBox.rotation || 0, | ||
cx, | ||
cy, | ||
); | ||
startX = rightTop.x; | ||
startY = rightTop.y; | ||
} else { | ||
const leftTop = getRectRotatedXY(oldBox); | ||
startX = leftTop.x; | ||
startY = leftTop.y; | ||
} | ||
|
||
const width = x - startX; | ||
const height = y - startY; | ||
|
||
let rect = { | ||
x: startX, | ||
y: startY, | ||
width, // width may be negative | ||
height, // also height | ||
}; | ||
|
||
let cx = 0; | ||
let cy = 0; | ||
if (scaleFromCenter) { | ||
const [oldCx, oldCy] = getRectCenterPoint(oldBox); | ||
const w = x - oldCx; | ||
const h = y - oldCy; | ||
rect = { | ||
x: oldCx - w, | ||
y: oldCy - h, | ||
width: w * 2, | ||
height: h * 2, | ||
}; | ||
|
||
cx = rect.x + rect.width / 2; | ||
cy = rect.y + rect.height / 2; | ||
} | ||
|
||
if (keepPolarSnap) { | ||
rect = adjustSizeToKeepPolarSnap(rect); | ||
} | ||
|
||
if (scaleFromCenter) { | ||
rect.x = cx - rect.width / 2; | ||
rect.y = cy - rect.height / 2; | ||
} | ||
|
||
if (isControlInLeft) { | ||
rect.width = -rect.width; | ||
rect.height = -rect.height; | ||
} | ||
const attrs = getLineAttrsByRect(rect); | ||
if (isControlInLeft) { | ||
attrs.x -= rect.width; | ||
attrs.y -= rect.height; | ||
} | ||
|
||
return attrs; | ||
}; | ||
|
||
const getLineAttrsByRect = ({ x, y, width, height }: IRect) => { | ||
const rotation = normalizeRadian(Math.atan2(height, width)); | ||
const cx = x + width / 2; | ||
const cy = y + height / 2; | ||
const p = transformRotate(x, y, -rotation, cx, cy); | ||
width = Math.sqrt(width * width + height * height); | ||
return { | ||
x: p.x, | ||
y: p.y, | ||
width, | ||
rotation, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/suika/src/editor/tools/tool_select/tool_select_rotation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.