mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Fix incorrect canvas bounds calculations (#3457)
* Fix incorrect canvas bounds calculations * Fix contributors * Set variables to be constant
This commit is contained in:
parent
343a304257
commit
fdc69505e9
3 changed files with 14 additions and 9 deletions
|
@ -191,7 +191,7 @@ Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com>
|
|||
Themis Demetriades <themis100@outlook.com>
|
||||
Luke Bartholomew <lukesbart@icloud.com>
|
||||
Gregory Abrasaldo <degeemon@gmail.com>
|
||||
Taylor Obyen <taylorobyen@gmail.com>
|
||||
Taylor Obyen <162023405+taylorobyen@users.noreply.github.com>
|
||||
Kris Cherven <krischerven@gmail.com>
|
||||
twwn <github.com/twwn>
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import { optimumCssSizeForCanvas } from "./canvas-scale";
|
|||
import { notesDataStore, tagsWritable } from "./store";
|
||||
import Toast from "./Toast.svelte";
|
||||
import { addShapesToCanvasFromCloze } from "./tools/add-from-cloze";
|
||||
import { enableSelectable, makeShapeRemainInCanvas, moveShapeToCanvasBoundaries } from "./tools/lib";
|
||||
import { enableSelectable, makeShapesRemainInCanvas, moveShapeToCanvasBoundaries } from "./tools/lib";
|
||||
import { modifiedPolygon } from "./tools/tool-polygon";
|
||||
import { undoStack } from "./tools/tool-undo-redo";
|
||||
import { enablePinchZoom, onResize, setCanvasSize } from "./tools/tool-zoom";
|
||||
|
@ -132,7 +132,7 @@ const setupBoundingBox = (canvas: fabric.Canvas, size: Size): fabric.Rect => {
|
|||
|
||||
canvas.add(boundingBox);
|
||||
onResize(canvas);
|
||||
makeShapeRemainInCanvas(canvas, boundingBox);
|
||||
makeShapesRemainInCanvas(canvas, boundingBox);
|
||||
moveShapeToCanvasBoundaries(canvas, boundingBox);
|
||||
// enable pinch zoom for mobile devices
|
||||
enablePinchZoom(canvas);
|
||||
|
|
|
@ -268,15 +268,20 @@ export const clear = (canvas: fabric.Canvas): void => {
|
|||
canvas.clear();
|
||||
};
|
||||
|
||||
export const makeShapeRemainInCanvas = (canvas: fabric.Canvas, boundingBox: fabric.Rect) => {
|
||||
/**
|
||||
* Creates a canvas event listener on shape movement to restrict movement to within the `boundingBox`
|
||||
*/
|
||||
export const makeShapesRemainInCanvas = (canvas: fabric.Canvas, boundingBox: fabric.Rect) => {
|
||||
canvas.on("object:moving", function(e) {
|
||||
const obj = e.target!;
|
||||
if (obj.getScaledHeight() > boundingBox.height! || obj.getScaledWidth() > boundingBox.width!) {
|
||||
|
||||
const objWidth = obj.getScaledWidth();
|
||||
const objHeight = obj.getScaledHeight();
|
||||
|
||||
if (objWidth > boundingBox.width! || objHeight > boundingBox.height!) {
|
||||
return;
|
||||
}
|
||||
|
||||
obj.setCoords();
|
||||
|
||||
const top = obj.top!;
|
||||
const left = obj.left!;
|
||||
|
||||
|
@ -285,8 +290,8 @@ export const makeShapeRemainInCanvas = (canvas: fabric.Canvas, boundingBox: fabr
|
|||
const leftBound = boundingBox.left!;
|
||||
const rightBound = leftBound + boundingBox.width!;
|
||||
|
||||
obj.left = Math.min(Math.max(left, leftBound), rightBound - obj.width!);
|
||||
obj.top = Math.min(Math.max(top, topBound), bottomBound - obj.height!);
|
||||
obj.left = Math.min(Math.max(left, leftBound), rightBound - objWidth);
|
||||
obj.top = Math.min(Math.max(top, topBound), bottomBound - objHeight);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue