Skip to content

Commit

Permalink
vector utils for rotation handles
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionReed committed Dec 3, 2024
1 parent f0271aa commit 34fd98e
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/folk-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,9 @@ export class FolkShape extends HTMLElement {

// Store initial angle on rotation start
if (target.getAttribute('part') === 'rotation') {
// We need to store initial rotation/angle somewhere.
// This is a little awkward as we'll want to do *quite a lot* of this kind of thing.
// Might be an argument for making elements dumber (i.e. not have them manage their own state) and do this from the outside.
// But we also want to preserve the self-sufficient nature of elements' behaviour...
// Maybe some kind of shared utility, used by both the element and the outside environment?
const center = this.getClientRect().center();
this.#initialRotation = this.#rotation;
const centerX = this.#x + this.width / 2;
const centerY = this.#y + this.height / 2;
this.#startAngle = Math.atan2(event.clientY - centerY, event.clientX - centerX);
this.#startAngle = Vector.angleFromOrigin({ x: event.clientX, y: event.clientY }, center);
}

// ignore interactions from slotted elements.
Expand Down Expand Up @@ -448,12 +442,9 @@ export class FolkShape extends HTMLElement {
}

if (handle === 'rotation') {
const centerX = this.#x + this.width / 2;
const centerY = this.#y + this.height / 2;
const currentAngle = Math.atan2(event.clientY - centerY, event.clientX - centerX);

const deltaAngle = currentAngle - this.#startAngle;
this.rotation = this.#initialRotation + deltaAngle;
const center = this.getClientRect().center();
const currentAngle = Vector.angleFromOrigin({ x: event.clientX, y: event.clientY }, center);
this.rotation = this.#initialRotation + (currentAngle - this.#startAngle);
return;
}

Expand Down

0 comments on commit 34fd98e

Please sign in to comment.