Skip to content

Commit 34e4093

Browse files
fix(ui): revert snapping logic, doesn't work w/ certain input devices
1 parent d7f93c3 commit 34e4093

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

invokeai/frontend/web/src/features/controlLayers/konva/CanvasStageModule.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@ type CanvasStageModuleConfig = {
2626
* The padding in pixels to use when fitting the layers to the stage.
2727
*/
2828
FIT_LAYERS_TO_STAGE_PADDING_PX: number;
29+
/**
30+
* The snap points for the scale of the canvas.
31+
*/
32+
SCALE_SNAP_POINTS: number[];
33+
/**
34+
* The tolerance for snapping the scale of the canvas, as a fraction of the scale.
35+
*/
36+
SCALE_SNAP_TOLERANCE: number;
2937
};
3038

3139
const DEFAULT_CONFIG: CanvasStageModuleConfig = {
3240
MIN_SCALE: 0.1,
3341
MAX_SCALE: 20,
3442
SCALE_FACTOR: 0.9995,
3543
FIT_LAYERS_TO_STAGE_PADDING_PX: 48,
44+
SCALE_SNAP_POINTS: [0.25, 0.5, 0.75, 1.5, 2, 3, 4, 5],
45+
SCALE_SNAP_TOLERANCE: 0.05,
3646
};
3747

3848
export class CanvasStageModule extends CanvasModuleBase {
@@ -234,19 +244,6 @@ export class CanvasStageModule extends CanvasModuleBase {
234244
const rounded = Math.round(scale * 100) / 100;
235245
const clamped = clamp(rounded, this.config.MIN_SCALE, this.config.MAX_SCALE);
236246

237-
// Snap to 100% (scale = 1.0) with a more generous tolerance
238-
if (Math.abs(clamped - 1) < 0.05) {
239-
return 1;
240-
}
241-
242-
// Snap to other common zoom levels for better UX
243-
const commonScales = [0.25, 0.5, 0.75, 1.5, 2, 3, 4, 5];
244-
for (const commonScale of commonScales) {
245-
if (Math.abs(clamped - commonScale) < 0.03) {
246-
return commonScale;
247-
}
248-
}
249-
250247
return clamped;
251248
};
252249

@@ -255,18 +252,19 @@ export class CanvasStageModule extends CanvasModuleBase {
255252
* @param scale The new scale to set
256253
* @param center The center of the stage to zoom in/out on
257254
*/
258-
setScale = (scale: number, center: Coordinate = this.getCenter(true)): void => {
255+
setScale = (scale: number, center?: Coordinate): void => {
259256
this.log.trace('Setting scale');
257+
const _center = center ?? this.getCenter(true);
260258
const newScale = this.constrainScale(scale);
261259

262260
const { x, y } = this.getPosition();
263261
const oldScale = this.getScale();
264262

265-
const deltaX = (center.x - x) / oldScale;
266-
const deltaY = (center.y - y) / oldScale;
263+
const deltaX = (_center.x - x) / oldScale;
264+
const deltaY = (_center.y - y) / oldScale;
267265

268-
const newX = Math.floor(center.x - deltaX * newScale);
269-
const newY = Math.floor(center.y - deltaY * newScale);
266+
const newX = Math.floor(_center.x - deltaX * newScale);
267+
const newY = Math.floor(_center.y - deltaY * newScale);
270268

271269
this.konva.stage.setAttrs({
272270
x: newX,
@@ -288,12 +286,14 @@ export class CanvasStageModule extends CanvasModuleBase {
288286
// We need the absolute cursor position - not the scaled position
289287
const cursorPos = this.konva.stage.getPointerPosition();
290288

291-
if (cursorPos) {
292-
// When wheeling on trackpad, e.evt.ctrlKey is true - in that case, let's reverse the direction
293-
const delta = e.evt.ctrlKey ? -e.evt.deltaY : e.evt.deltaY;
294-
const scale = this.manager.stage.getScale() * this.config.SCALE_FACTOR ** delta;
295-
this.manager.stage.setScale(scale, cursorPos);
289+
if (!cursorPos) {
290+
return;
296291
}
292+
293+
// When wheeling on trackpad, e.evt.ctrlKey is true - in that case, let's reverse the direction
294+
const delta = e.evt.ctrlKey ? -e.evt.deltaY : e.evt.deltaY;
295+
const scale = this.manager.stage.getScale() * this.config.SCALE_FACTOR ** delta;
296+
this.manager.stage.setScale(scale, cursorPos);
297297
};
298298

299299
onStagePointerDown = (e: KonvaEventObject<PointerEvent>) => {

0 commit comments

Comments
 (0)