Files
bitmappery/tests/unit/rendering/utils/drawable-canvas-utils.spec.ts
Igor Zinken 3f94cc548b Address issues when filling or stroking out-of-visual-bounds Selections (#99)
When zooming in and panning the Viewport while a Selection is active, if a bucket fill or stroke operation is used, only the visible area of the Selection is filled.

This should be updated to fill the full area, even the out of visual bounds area.

This changeset also fixes an issue where stroke width would be applied differently at different zoom levels.
2026-05-01 21:33:03 +02:00

70 lines
2.7 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from "vitest";
import type { Point } from "zcanvas";
import { createMockZoomableCanvas, mockZCanvas } from "../../mocks";
mockZCanvas();
import { applyOverrideConfig, createOverrideConfig, } from "@/rendering/utils/drawable-canvas-utils";
describe( "Drawable canvas utils", () => {
const zoomableCanvas = createMockZoomableCanvas();
let pointers: Point[];
beforeEach(() => {
zoomableCanvas.documentScale = 3;
zoomableCanvas.zoomFactor = 2;
( zoomableCanvas.getViewport as Mock ).mockReturnValue({ left: 10, top: 5 });
pointers = [
{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }, { x: 0, y: 0 },
];
});
afterEach(() => {
vi.resetAllMocks();
});
describe( "When creating a draw override configuration", () => {
it( "should store the current Document scale and zoom factor", () => {
const { scale, zoom } = createOverrideConfig( zoomableCanvas, { useViewport: true }, pointers )
expect( scale ).toEqual( 1 / zoomableCanvas.documentScale );
expect( zoom ).toEqual( zoomableCanvas.zoomFactor );
});
it( "should take the Viewport offset into account", () => {
const { vpX, vpY } = createOverrideConfig( zoomableCanvas, { useViewport: true }, pointers );
expect( vpX ).toEqual( 10 );
expect( vpY ).toEqual( 5 );
});
it( "should ignore the Viewport offset when requested", () => {
const { vpX, vpY } = createOverrideConfig( zoomableCanvas, { useViewport: false }, pointers );
expect( vpX ).toEqual( 0 );
expect( vpY ).toEqual( 0 );
});
});
describe( "When applying a draw override configuration", () => {
it( "should translate the pointer coordinates to reflect the Viewport position and Document scale in place", () => {
const overrideConfig = createOverrideConfig( zoomableCanvas, { useViewport: true }, pointers );
applyOverrideConfig( overrideConfig, pointers );
expect( pointers ).toEqual([
{ x: -30, y: -15 }, { x: -20, y: -15 }, { x: -20, y: -5 }, { x: -30, y: -5 }, { x: -30, y: -15 },
]);
});
it( "should not translate the pointer coordinates when the Viewport position should be ignored", () => {
const overrideConfig = createOverrideConfig( zoomableCanvas, { useViewport: false }, pointers );
applyOverrideConfig( overrideConfig, pointers );
expect( pointers ).toEqual([
{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }, { x: 0, y: 0 },
]);
});
});
});