1
0
mirror of https://github.com/MultiMote/niimblue synced 2026-01-19 19:37:11 +03:00

Zoom using the mouse wheel (#59)

This commit is contained in:
Xiang
2024-11-29 18:30:55 +08:00
committed by GitHub
parent 20c1e6c012
commit 359b8f648e
3 changed files with 59 additions and 0 deletions

View File

@@ -11,6 +11,60 @@ export class CustomCanvas extends fabric.Canvas {
private readonly MIRROR_GHOST_COLOR = "rgba(0, 0, 0, 0.3)";
private customBackground: boolean = true;
private highlightMirror: boolean = true;
private virtualZoomRatio: number = 1;
constructor(el?: string | HTMLCanvasElement, options?: fabric.TOptions<fabric.CanvasOptions>) {
super(el, options);
this.setupZoom();
}
private setupZoom() {
this.on("mouse:wheel", (opt) => {
const event = opt.e as WheelEvent;
event.preventDefault();
const delta = event.deltaY;
if (delta > 0) {
this.virtualZoomOut();
} else {
this.virtualZoomIn();
}
});
this.on("mouse:down:before", (opt) => {
const event = opt.e as MouseEvent;
if (event.button == 1) {
event.preventDefault();
this.resetVirtualZoom();
}
});
}
public virtualZoom(newZoom: number) {
this.virtualZoomRatio = Math.min(Math.max(0.25, newZoom), 4);
this.setDimensions(
{
width: this.virtualZoomRatio * this.getWidth() + "px",
height: this.virtualZoomRatio * this.getHeight() + "px",
},
{ cssOnly: true }
);
}
public virtualZoomIn() {
this.virtualZoom(this.virtualZoomRatio * 1.05);
}
public virtualZoomOut() {
this.virtualZoom(this.virtualZoomRatio * 0.95);
}
public getVirtualZoom(): number {
return this.virtualZoomRatio;
}
public resetVirtualZoom() {
this.virtualZoom(1);
}
setLabelProps(value: LabelProps) {
this.labelProps = value;

View File

@@ -141,6 +141,7 @@
const onUpdateLabelProps = (newProps: LabelProps) => {
labelProps = newProps;
fabricCanvas.setDimensions(labelProps.size);
fabricCanvas.virtualZoom(fabricCanvas.getVirtualZoom());
try {
LocalStoragePersistence.saveLastLabelProps(labelProps);
undo.push(fabricCanvas, labelProps);

View File

@@ -11,6 +11,7 @@ import { OBJECT_DEFAULTS, THUMBNAIL_HEIGHT, THUMBNAIL_QUALITY } from "../default
import { z } from "zod";
import { FileSharer } from "@byteowls/capacitor-filesharer";
import { Toasts } from "./toasts";
import { CustomCanvas } from "../fabric-object/custom_canvas";
export class FileUtils {
static timestamp(): number {
@@ -140,6 +141,9 @@ export class FileUtils {
await canvas.loadFromJSON(state, (_, obj) => {
if ("set" in obj) obj.set({ snapAngle: OBJECT_DEFAULTS.snapAngle });
});
if (canvas instanceof CustomCanvas) {
canvas.virtualZoom(canvas.getVirtualZoom())
}
canvas.requestRenderAll();
}
}