Skip to content

Commit 3dc1012

Browse files
committed
feat(extensions/scrollbars): add makeMouseWheel
1 parent 2fd8fa8 commit 3dc1012

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

extensions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { AligningGuidelines } from './aligning_guidelines';
22
export type * from './aligning_guidelines/typedefs';
33

44
export { Scrollbars } from './scrollbars';
5+
export { makeMouseWheel } from './scrollbars/util';
56
export type * from './scrollbars/typedefs';
67

78
export {

extensions/scrollbars/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Add scrollbars to the infinite canvas. The scrollbars will be hidden when all gr
55
## How to use it
66

77
```ts
8-
import { Scrollbars } from 'fabric/extensions';
8+
import { Scrollbars, makeMouseWheel } from 'fabric/extensions';
99

1010
const config = {
1111
/** Scrollbar fill color */
@@ -28,9 +28,15 @@ const config = {
2828
padding = 4;
2929
};
3030

31+
// You have the option to implement custom canvas zooming or use the plugin's built-in solution.
32+
const mousewheel = makeMouseWheel(canvas);
33+
canvas.on("mouse:wheel", mousewheel);
34+
// canvas.off("mouse:wheel", mousewheel);
35+
3136
const scrollbars = new Scrollbars(myCanvas, options);
3237

3338
// in order to disable scrollbars later:
3439

3540
scrollbars.dispose();
41+
3642
```

extensions/scrollbars/typedefs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ export type ScrollbarProps = {
2828
export type ScrollbarXProps = Pick<ScrollbarProps, 'left' | 'right'>;
2929

3030
export type ScrollbarYProps = Pick<ScrollbarProps, 'top' | 'bottom'>;
31+
32+
export type MakeMouseWheelProps = {
33+
/** Minimum zoom size */
34+
min: number;
35+
/** Maximum zoom size */
36+
max: number;
37+
/** Touchpad Zoom Speed default to 0.99 */
38+
tSpeed: number;
39+
/** Mouse Wheel Zoom Speed default to 0.998 */
40+
mSpeed: number;
41+
/** Canvas panning speed defaults to 1 */
42+
pSpeed: number;
43+
};

extensions/scrollbars/util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { TPointerEventInfo, Canvas, TMat2D } from 'fabric';
2+
import type { MakeMouseWheelProps } from './typedefs';
3+
4+
export const makeMouseWheel =
5+
(canvas: Canvas, props: Partial<MakeMouseWheelProps> = {}) =>
6+
(options: TPointerEventInfo<WheelEvent>) => {
7+
const e = options.e;
8+
if (e.target == canvas.upperCanvasEl) e.preventDefault();
9+
const { min, max, tSpeed = 0.99, mSpeed = 0.998, pSpeed = 1 } = props;
10+
11+
/** Is it touchpad zoom? */
12+
const isTouchScale = Math.floor(e.deltaY) != Math.ceil(e.deltaY);
13+
// Touchpad zooming, Mac's touchpad automatically sets e.ctrlKey to true
14+
if (e.ctrlKey || e.metaKey) {
15+
// Mouse and touchpad speed separate
16+
const speed = isTouchScale ? tSpeed : mSpeed;
17+
let zoom = canvas.getZoom();
18+
zoom *= speed ** e.deltaY;
19+
if (max != undefined && zoom > max) zoom = max;
20+
if (min != undefined && zoom < min) zoom = min;
21+
22+
canvas.zoomToPoint(options.viewportPoint, zoom);
23+
canvas.requestRenderAll();
24+
return;
25+
}
26+
27+
const vpt = canvas.viewportTransform.slice(0) as TMat2D;
28+
let { deltaX, deltaY } = e;
29+
// Hold down Shift and scroll the mouse to indicate horizontal scrolling
30+
if (e.shiftKey && deltaX == 0) {
31+
deltaX = deltaY;
32+
deltaY = 0;
33+
}
34+
vpt[4] -= deltaX * pSpeed;
35+
vpt[5] -= deltaY * pSpeed;
36+
canvas.setViewportTransform(vpt);
37+
canvas.requestRenderAll();
38+
};

0 commit comments

Comments
 (0)