Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@vbabel
Copy link
Contributor

@vbabel vbabel commented Nov 25, 2025

This PR adds support for keyboard pan/zoom actions. The keybindings here reflect Figma's behavior in the browser. The pan and zoom actions all occur when drag mode is enabled (spacebar is held down) and are supressed if the control key is pressed.

I did my best to replicate Figma's keyboard scaling behavior, which as far as I can tell changes the scale to the next (zoom in) or previous (zoom out) power of 2 including negative powers. Figma does not center keyboard scaling to the cursor, instead just keeps the position the same and scales.

Similarly, I tried to replicate Figma's keyboard panning behavior. Again, as far as I can tell, Figma will pan ~65px in regardless of scale. I based the panning on the pan speed setting in this case, but this could be hardcoded instead.

I've also added MAX_SCALE and MIN_SCALE constants to clamp scaling/zooming, again I tried to replicate what the max and min seems to be in Figma on the browser.

Copy link
Owner

@pocka pocka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pan and zoom actions all occur when drag mode is enabled (spacebar is held down)...

Why require space key? I checked Figma on macOS Chrome and they don't require space key to pan / zoom. Does it conflict with other shortcuts?

src/math.ts Outdated

export function nextPowerOfTwo(num: number): number {
const nearest = nearestPowerOfTwo(num);
return Math.min(MAX_SCALE, nearest > num ? nearest : nearest * 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min/max in nextPowerOfTwo, previousPowerOfTwo and nearestPowerOfTwo should be done at caller or these functions should take min/max parameter. math.nextPowerOfTwo internally using MAX_SCALE feels not right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, updated them to take an options argument and moved MIN_SCALE / MAX_SCALE to FrameCanvas, per above

@pocka pocka added this to the v2.1.0 milestone Nov 26, 2025
@vbabel
Copy link
Contributor Author

vbabel commented Nov 26, 2025

The pan and zoom actions all occur when drag mode is enabled (spacebar is held down)...

Why require space key? I checked Figma on macOS Chrome and they don't require space key to pan / zoom. Does it conflict with other shortcuts?

I'm not sure why I thought it was necessary! Just tested again and see that it allows it regardless of whether spacebar is held down or not, I'll update this.

@vbabel vbabel requested a review from pocka November 26, 2025 05:15
@pocka pocka merged commit 14d9b06 into pocka:master Nov 27, 2025
1 check passed
@vbabel
Copy link
Contributor Author

vbabel commented Nov 27, 2025

@pocka Just realized that this prevents the user from scrolling with the arrow keys, because we are not checking for focus or hover like the solution here: #55

I'm wondering if perhaps we should do something similar, the only issue right now is there is no way for the canvas to have focus (until something like #59 is implemented allowing kb users to focus within the canvas)

@pocka
Copy link
Owner

pocka commented Nov 28, 2025

This is difficult problem as figspec being a component meant to be embedded. We can't rely on "common sense" because there is not much prior arts thus no established commons...

I believe in our case making the canvas (or the custom element itself) focusable is the right approach.

  • This is how a UA element works.
  • Condition / detection will be simpler.
  • User pressing shortcut keys then miss (UA default runs) can be bit frustrating but avoidable. User pressing UA shortcut keys outside a custom element then the custom element steals the key is frustrating and sometimes unavoidable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants