@@ -26,13 +26,23 @@ type CanvasStageModuleConfig = {
26
26
* The padding in pixels to use when fitting the layers to the stage.
27
27
*/
28
28
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 ;
29
37
} ;
30
38
31
39
const DEFAULT_CONFIG : CanvasStageModuleConfig = {
32
40
MIN_SCALE : 0.1 ,
33
41
MAX_SCALE : 20 ,
34
42
SCALE_FACTOR : 0.9995 ,
35
43
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 ,
36
46
} ;
37
47
38
48
export class CanvasStageModule extends CanvasModuleBase {
@@ -234,19 +244,6 @@ export class CanvasStageModule extends CanvasModuleBase {
234
244
const rounded = Math . round ( scale * 100 ) / 100 ;
235
245
const clamped = clamp ( rounded , this . config . MIN_SCALE , this . config . MAX_SCALE ) ;
236
246
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
-
250
247
return clamped ;
251
248
} ;
252
249
@@ -255,18 +252,19 @@ export class CanvasStageModule extends CanvasModuleBase {
255
252
* @param scale The new scale to set
256
253
* @param center The center of the stage to zoom in/out on
257
254
*/
258
- setScale = ( scale : number , center : Coordinate = this . getCenter ( true ) ) : void => {
255
+ setScale = ( scale : number , center ? : Coordinate ) : void => {
259
256
this . log . trace ( 'Setting scale' ) ;
257
+ const _center = center ?? this . getCenter ( true ) ;
260
258
const newScale = this . constrainScale ( scale ) ;
261
259
262
260
const { x, y } = this . getPosition ( ) ;
263
261
const oldScale = this . getScale ( ) ;
264
262
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 ;
267
265
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 ) ;
270
268
271
269
this . konva . stage . setAttrs ( {
272
270
x : newX ,
@@ -288,12 +286,14 @@ export class CanvasStageModule extends CanvasModuleBase {
288
286
// We need the absolute cursor position - not the scaled position
289
287
const cursorPos = this . konva . stage . getPointerPosition ( ) ;
290
288
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 ;
296
291
}
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 ) ;
297
297
} ;
298
298
299
299
onStagePointerDown = ( e : KonvaEventObject < PointerEvent > ) => {
0 commit comments