All files / lib DraggableBoundingBox.svelte.js

100% Statements 160/160
100% Branches 67/67
100% Functions 9/9
100% Lines 160/160

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274                                              1x 1x 1x 1x 1x                 1x 7x 7x 7x 7x 7x   7x 7x   7x 2x 2x 7x 5x 5x 5x   7x 3x 3x 3x     7x 7x 7x 7x   7x   1x         34x 34x 34x 34x 34x       34x     34x 33x 33x     34x 34x 34x 34x 34x   34x 34x 34x         34x         34x 9x 2x 2x 2x 2x 2x 2x 2x   9x 2x 2x   9x 4x 4x   1x 34x             34x   5x 5x 5x 5x   5x   34x 5x 5x 2x 34x   34x 5x 5x 2x 34x   34x 5x 5x 2x 34x   34x 5x 5x 2x 34x                   34x 41x   2x   41x 25x   25x   14x 14x           34x 61x   60x   61x 40x 40x 40x   40x   60x     61x 48x 48x 48x   61x 3x 3x 3x 3x 3x   61x 4x 4x 4x 4x   61x 5x 5x 5x 5x 61x   34x 5x 5x 5x 5x 5x   5x   34x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x             34x 34x 34x 34x 34x 34x 34x 34x 34x 34x     34x 34x 34x  
import { type } from 'arktype';
import { clamp, sign } from './utils';
 
/**
 * Represents the zoom state of the image.
 * x & y coordinates are in pixels of the resized, post-object-fit but pre-zoom image
 * @typedef {object} ZoomState
 * @property {object} origin
 * @property {number} origin.x
 * @property {number} origin.y
 * @property {number} scale
 * @property {boolean} panning
 * @property {object} panStart
 * @property {number} panStart.x
 * @property {number} panStart.y
 * @property {object} panStart.zoomOrigin
 * @property {number} panStart.zoomOrigin.x
 * @property {number} panStart.zoomOrigin.y
 */
 
/**
 * @type {ZoomState}
 */
export const INITIAL_ZOOM_STATE = {
	origin: { x: 0, y: 0 },
	scale: 1,
	panning: false,
	panStart: { x: 0, y: 0, zoomOrigin: { x: 0, y: 0 } }
};
 
/**
 * Calculate bounding rect for an image element that has object-fit: contain. The boundingClientRect is not the same as the actual, displayed image. We use both natural{Width,Height} and client{Width,Height} to calculate the displayed image size.
 * You can also provide the zoom state to take it into account.
 * @param {Pick<HTMLImageElement, `${'natural'|'client'}${'Width'|'Height'}` | `client${'Top'|'Left'}`>} imageElement
 * @param {ZoomState} [zoomState] take into account the zoom state of the image
 */
export function fittedImageRect(
	{ naturalWidth, naturalHeight, clientWidth, clientHeight, clientTop, clientLeft },
	zoomState
) {
	const naturalRatio = naturalWidth / naturalHeight;
	const clientRatio = clientWidth / clientHeight;
 
	let width = 0;
	let height = 0;
 
	if (naturalRatio < clientRatio) {
		width = clientHeight * naturalRatio;
		height = clientHeight;
	} else {
		height = clientWidth / naturalRatio;
		width = clientWidth;
	}
 
	if (zoomState) {
		width *= zoomState.scale;
		height *= zoomState.scale;
	}
 
	return {
		width,
		height,
		x: (zoomState?.origin.x ?? 0) + clientLeft + (clientWidth - width) / 2,
		y: (zoomState?.origin.y ?? 0) + clientTop + (clientHeight - height) / 2
	};
}
 
export class NewBoundingBox {
	/**
	 * @type {import('./BoundingBoxes.svelte').Rect}
	 * Limits for the resulting bounding box. Coordinates will be clamped to these values.
	 */
	limits = {
		x: 0,
		y: 0,
		width: 0,
		height: 0
	};
 
	/** @type {'clickanddrag' | '2point' | '4point'|'off'} */
	createMode = $state('clickanddrag');
 
	/** @param {'clickanddrag' | '2point' | '4point'|'off'} mode */
	setCreateMode(mode) {
		this.createMode = mode;
	}
 
	/** @type {import('./BoundingBoxes.svelte').Rect & { dragDirection: {x:-1|0|1, y:-1|0|1} }} */
	clickanddrag = $state({
		x: 0,
		y: 0,
		width: 0,
		height: 0,
		// -1 or 1
		dragDirection: {
			x: 0,
			y: 0
		}
	});
 
	/** @type {Array<{x: number; y: number}>}  */
	points = $state([]);
 
	/**
	 * The bounding box has enough data to be created
	 */
	ready = $derived.by(() => {
		if (this.createMode === 'clickanddrag') {
			return type({
				x: 'number > 0',
				y: 'number > 0',
				width: 'number > 0',
				height: 'number > 0'
			}).allows(this.clickanddrag);
		}
 
		if (this.createMode == '2point') {
			return this.points.length >= 2;
		}
 
		if (this.createMode === '4point') {
			return this.points.length >= 4;
		}
 
		return false;
	});
 
	/**
	 *
	 * @param {import('./BoundingBoxes.svelte').Rect} rect
	 * @returns {import('./BoundingBoxes.svelte').Rect}
	 */
	clamp(rect) {
		return {
			x: clamp(rect.x, this.limits.x, this.limits.width),
			y: clamp(rect.y, this.limits.y, this.limits.height),
			width: clamp(rect.width, 0, this.limits.width - rect.x),
			height: clamp(rect.height, 0, this.limits.height - rect.y)
		};
	}
 
	x = $derived.by(() => {
		if (this.createMode === 'clickanddrag') return this.clickanddrag.x;
		if (this.createMode === 'off') return 0;
		return Math.min(...this.points.map((point) => point.x));
	});
 
	y = $derived.by(() => {
		if (this.createMode === 'clickanddrag') return this.clickanddrag.y;
		if (this.createMode === 'off') return 0;
		return Math.min(...this.points.map((point) => point.y));
	});
 
	width = $derived.by(() => {
		if (this.createMode === 'clickanddrag') return this.clickanddrag.width;
		if (this.createMode === 'off') return 0;
		return Math.max(...this.points.map((point) => point.x)) - this.x;
	});
 
	height = $derived.by(() => {
		if (this.createMode === 'clickanddrag') return this.clickanddrag.height;
		if (this.createMode === 'off') return 0;
		return Math.max(...this.points.map((point) => point.y)) - this.y;
	});
 
	/**
	 * A new point was just clicked: register it.
	 * clickanddrag: sets x, y
	 * 2point: sets x1, y1 then x2, y2
	 * 4point: sets topleft, topright, bottomright, bottomleft
	 * @param {number} x
	 * @param {number} y
	 */
	registerPoint(x, y) {
		if (this.createMode === 'off') {
			return;
		}
 
		if (this.createMode === 'clickanddrag') {
			this.clickanddrag = { ...this.clickanddrag, x, y };
			return;
		}
 
		this.points.push({ x, y });
	}
 
	/**
	 * @param {number} dx (MouseEvent).movementX
	 * @param {number} dy (MouseEvent).movementY
	 */
	registerMovement(dx, dy) {
		if (this.createMode !== 'clickanddrag') return;
 
		const { width, height } = this.clickanddrag;
 
		if (width <= 0 || height <= 0) {
			this.clickanddrag.dragDirection = {
				x: sign(dx),
				y: sign(dy)
			};
		}
 
		const { x: xdir, y: ydir } = this.clickanddrag.dragDirection;
 
		// Drag direction: topleft -> bottomright
		if (xdir > 0 && ydir > 0) {
			this.clickanddrag.width += dx;
			this.clickanddrag.height += dy;
		}
		// Drag direction: bottomright -> topleft
		if (xdir < 0 && ydir < 0) {
			this.clickanddrag.x += dx;
			this.clickanddrag.y += dy;
			this.clickanddrag.width -= dx;
			this.clickanddrag.height -= dy;
		}
		// Drag direction: topright -> bottomleft
		if (xdir < 0 && ydir > 0) {
			this.clickanddrag.x += dx;
			this.clickanddrag.height += dy;
			this.clickanddrag.width -= dx;
		}
		// Drag direction: bottomleft -> topright
		if (xdir > 0 && ydir < 0) {
			this.clickanddrag.y += dy;
			this.clickanddrag.width += dx;
			this.clickanddrag.height -= dy;
		}
	}
 
	rect() {
		return this.clamp({
			x: this.x,
			y: this.y,
			width: this.width,
			height: this.height
		});
	}
 
	reset() {
		this.clickanddrag = {
			x: 0,
			y: 0,
			width: 0,
			height: 0,
			dragDirection: {
				x: 0,
				y: 0
			}
		};
		this.points = [];
	}
 
	/**
	 *
	 * @param {object} options
	 * @param {typeof this.limits} options.limits
	 */
	constructor({ limits }) {
		this.limits = limits;
		this.clickanddrag = {
			x: 0,
			y: 0,
			width: 0,
			height: 0,
			dragDirection: {
				x: 0,
				y: 0
			}
		};
		this.points = [];
	}
}