File size: 47,531 Bytes
36064ef | 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 | import {
Container,
Graphics,
Point,
Rectangle,
FederatedPointerEvent,
type FederatedOptions
} from "pixi.js";
import { type ImageEditorContext, type Tool } from "../core/editor";
import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte";
import { type Command } from "../core/commands";
/**
* Command that handles resizing the image and supports undo/redo
*/
export class ResizeCommand implements Command {
private original_width: number;
private original_height: number;
private original_x: number;
private original_y: number;
private new_width: number;
private new_height: number;
private new_x: number;
private new_y: number;
private reset_ui: () => void;
name: string;
constructor(
private context: ImageEditorContext,
original_state: { width: number; height: number; x: number; y: number },
new_state: { width: number; height: number; x: number; y: number },
reset_ui: () => void
) {
this.name = "Resize";
this.original_width = original_state.width;
this.original_height = original_state.height;
this.original_x = original_state.x;
this.original_y = original_state.y;
this.new_width = new_state.width;
this.new_height = new_state.height;
this.new_x = new_state.x;
this.new_y = new_state.y;
this.reset_ui = reset_ui;
}
async execute(context?: ImageEditorContext): Promise<void> {
if (context) {
this.context = context;
}
if (!this.context.background_image) return;
this.context.background_image.width = this.new_width;
this.context.background_image.height = this.new_height;
this.context.background_image.position.set(this.new_x, this.new_y);
this.reset_ui();
}
async undo(): Promise<void> {
if (!this.context.background_image) return;
this.context.background_image.width = this.original_width;
this.context.background_image.height = this.original_height;
this.context.background_image.position.set(
this.original_x,
this.original_y
);
this.reset_ui();
}
}
/**
* @class ResizeTool
* @implements {Tool}
* @description Implements the resize tool functionality for the image editor
*/
export class ResizeTool implements Tool {
name = "resize" as const;
private image_editor_context!: ImageEditorContext;
current_tool: ToolbarTool = "image";
current_subtool: Subtool = "size";
private current_cursor: FederatedOptions["cursor"] = "unset";
private readonly CORNER_SIZE = 25;
private readonly LINE_THICKNESS = 5;
private readonly HANDLE_COLOR = 0x000000;
private readonly HIT_AREA_SIZE = 40;
private borderRegion = 0;
private is_dragging = false;
private selected_handle: Container | null = null;
private active_corner_index = -1;
private active_edge_index = -1;
private last_pointer_position: Point | null = null;
private resize_ui_container: Container | null = null;
private dimensions: { width: number; height: number } = {
width: 0,
height: 0
};
private position: { x: number; y: number } = { x: 0, y: 0 };
private scale = 1;
private is_moving = false;
private dom_mousedown_handler: ((e: MouseEvent) => void) | null = null;
private dom_mousemove_handler: ((e: MouseEvent) => void) | null = null;
private dom_mouseup_handler: ((e: MouseEvent) => void) | null = null;
private event_callbacks: Map<string, (() => void)[]> = new Map();
private last_scale = 1;
private original_state: {
width: number;
height: number;
x: number;
y: number;
} | null = null;
/**
* @method setup
* @async
* @description Sets up the resize tool with the given context and tool/subtool
* @param {ImageEditorContext} context - The image editor context
* @param {ToolbarTool} tool - The current tool
* @param {Subtool} subtool - The current subtool
* @returns {Promise<void>}
*/
async setup(
context: ImageEditorContext,
tool: ToolbarTool,
subtool: Subtool
): Promise<void> {
this.image_editor_context = context;
this.current_tool = tool;
this.current_subtool = subtool;
if (context.background_image) {
if ((context.background_image as any).borderRegion !== undefined) {
this.borderRegion = (context.background_image as any).borderRegion;
} else {
const bg = context.background_image;
const container = context.image_container;
const isHorizontallyCentered =
Math.abs(bg.position.x - (container.width - bg.width) / 2) < 2;
const isVerticallyCentered =
Math.abs(bg.position.y - (container.height - bg.height) / 2) < 2;
if (!isHorizontallyCentered || !isVerticallyCentered) {
this.borderRegion = Math.max(bg.position.x, bg.position.y);
(context.background_image as any).borderRegion = this.borderRegion;
}
}
}
context.dimensions.subscribe((dimensions) => {
this.dimensions = dimensions;
if (this.resize_ui_container) {
this.update_resize_ui();
}
});
context.position.subscribe((position) => {
this.position = position;
if (
this.resize_ui_container &&
this.current_tool === "image" &&
this.current_subtool === "size"
) {
this.update_resize_ui();
}
});
context.scale.subscribe((scale) => {
this.scale = scale;
if (
this.resize_ui_container &&
this.current_tool === "image" &&
this.current_subtool === "size"
) {
this.update_resize_ui();
}
});
await this.init_resize_ui();
this.setup_event_listeners();
if (
this.current_subtool === "size" &&
this.image_editor_context.background_image
) {
const bg = this.image_editor_context.background_image;
const container = this.image_editor_context.image_container;
const bg_width = bg.width;
const bg_height = bg.height;
const container_bounds = container.getLocalBounds();
const container_width = container_bounds.width;
const container_height = container_bounds.height;
const min_x = -(bg_width - container_width);
const min_y = -(bg_height - container_height);
const max_x = 0;
const max_y = 0;
const x_out_of_bounds =
bg_width > container_width &&
(bg.position.x < min_x || bg.position.x > max_x);
const y_out_of_bounds =
bg_height > container_height &&
(bg.position.y < min_y || bg.position.y > max_y);
if (x_out_of_bounds || y_out_of_bounds) {
this.apply_boundary_constraints();
}
}
}
/**
* @method cleanup
* @description Cleans up the resize tool resources and removes event listeners
* @returns {void}
*/
cleanup(): void {
if (this.resize_ui_container) {
this.image_editor_context.app.stage.removeChild(this.resize_ui_container);
}
this.cleanup_event_listeners();
}
/**
* @method set_tool
* @description Sets the current tool and subtool, updating UI visibility
* @param {ToolbarTool} tool - The tool to set
* @param {Subtool} subtool - The subtool to set
* @returns {void}
*/
set_tool(tool: ToolbarTool, subtool: Subtool): void {
this.current_tool = tool;
this.current_subtool = subtool;
if (tool === "image" && subtool === "size") {
if (this.image_editor_context.background_image) {
const storedBorderRegion = (
this.image_editor_context.background_image as any
).borderRegion;
if (typeof storedBorderRegion === "number" && storedBorderRegion > 0) {
this.borderRegion = storedBorderRegion;
}
}
this.show_resize_ui();
if (this.image_editor_context.background_image) {
const bg = this.image_editor_context.background_image;
const container = this.image_editor_context.image_container;
const bg_width = bg.width;
const bg_height = bg.height;
const container_bounds = container.getLocalBounds();
const container_width = container_bounds.width;
const container_height = container_bounds.height;
const effectiveWidth = container_width - this.borderRegion * 2;
const effectiveHeight = container_height - this.borderRegion * 2;
const min_x =
bg_width > effectiveWidth
? -(bg_width - effectiveWidth) + this.borderRegion
: this.borderRegion;
const max_x =
bg_width > effectiveWidth
? this.borderRegion
: container_width - bg_width - this.borderRegion;
const min_y =
bg_height > effectiveHeight
? -(bg_height - effectiveHeight) + this.borderRegion
: this.borderRegion;
const max_y =
bg_height > effectiveHeight
? this.borderRegion
: container_height - bg_height - this.borderRegion;
const x_out_of_bounds = bg.position.x < min_x || bg.position.x > max_x;
const y_out_of_bounds = bg.position.y < min_y || bg.position.y > max_y;
if (x_out_of_bounds || y_out_of_bounds) {
this.apply_boundary_constraints();
}
}
} else {
this.hide_resize_ui();
}
}
/**
* @method init_resize_ui
* @private
* @async
* @description Initializes the resize UI elements and sets up the update loop
* @returns {Promise<void>}
*/
private async init_resize_ui(): Promise<void> {
this.resize_ui_container = this.make_resize_ui(
this.dimensions.width * this.scale,
this.dimensions.height * this.scale
);
this.resize_ui_container.position.set(this.position.x, this.position.y);
this.resize_ui_container.visible = false;
this.image_editor_context.app.stage.addChild(this.resize_ui_container);
}
/**
* @method make_resize_ui
* @private
* @description Creates the resize UI with the specified dimensions
* @param {number} width - Width of the resize area
* @param {number} height - Height of the resize area
* @returns {Container} The created resize UI container
*/
private make_resize_ui(width: number, height: number): Container {
const resize_container = new Container();
resize_container.eventMode = "static";
resize_container.interactiveChildren = true;
const outline = new Graphics().rect(0, 0, width, height).stroke({
width: 1,
color: 0x000000,
alignment: 0,
alpha: 0.3
});
const dotted_outline = new Graphics();
dotted_outline.rect(0, 0, width, height).stroke({
width: 1,
color: 0x000000,
alpha: 0.7,
pixelLine: true
});
const dash_length = 5;
const gap_length = 5;
const total_length = dash_length + gap_length;
for (let x = 0; x < width; x += total_length * 2) {
dotted_outline
.rect(x, 0, Math.min(dash_length, width - x), 1)
.fill(0x000000);
dotted_outline
.rect(x, height - 1, Math.min(dash_length, width - x), 1)
.fill(0x000000);
}
for (let y = 0; y < height; y += total_length * 2) {
dotted_outline
.rect(0, y, 1, Math.min(dash_length, height - y))
.fill(0x000000);
dotted_outline
.rect(width - 1, y, 1, Math.min(dash_length, height - y))
.fill(0x000000);
}
resize_container.addChild(outline);
resize_container.addChild(dotted_outline);
const move_area = new Graphics()
.rect(0, 0, width, height)
.fill(0xffffff, 0);
move_area.eventMode = "static";
move_area.cursor = "move";
resize_container.addChild(move_area);
this.create_corner_handles(resize_container, width, height);
this.create_edge_handles(resize_container, width, height);
return resize_container;
}
/**
* @method create_handle
* @private
* @description Creates a handle for the resize UI
* @param {boolean} [is_edge=false] - Whether the handle is an edge handle
* @returns {Container} The created handle container
*/
private create_handle(is_edge = false): Container {
const handle = new Container();
handle.eventMode = "static";
handle.cursor = "pointer";
const handle_graphics = new Graphics();
const handle_size = is_edge ? 8 : 10;
handle_graphics
.rect(-handle_size / 2, -handle_size / 2, handle_size, handle_size)
.fill(0xffffff)
.stroke({ width: 1, color: this.HANDLE_COLOR });
handle.addChild(handle_graphics);
const hit_size = is_edge ? this.HIT_AREA_SIZE * 1.5 : this.HIT_AREA_SIZE;
handle.hitArea = new Rectangle(
-hit_size / 2,
-hit_size / 2,
hit_size,
hit_size
);
return handle;
}
/**
* @method create_edge_handles
* @private
* @description Creates edge handles for the resize UI
* @param {Container} container - The container to add handles to
* @param {number} width - Width of the resize area
* @param {number} height - Height of the resize area
* @returns {void}
*/
private create_edge_handles(
container: Container,
width: number,
height: number
): void {
[-1, 1].forEach((i, index) => {
const handle = this.create_handle(true);
handle.rotation = 0;
handle.on("pointerdown", (event: FederatedPointerEvent) => {
this.handle_pointer_down(event, handle, -1, index);
});
handle.x = width / 2;
handle.y = i < 0 ? 0 : height;
handle.cursor = "ns-resize";
container.addChild(handle);
});
[-1, 1].forEach((i, index) => {
const handle = this.create_handle(true);
handle.rotation = 0;
handle.on("pointerdown", (event: FederatedPointerEvent) => {
this.handle_pointer_down(event, handle, -1, index + 2);
});
handle.x = i < 0 ? 0 : width;
handle.y = height / 2;
handle.cursor = "ew-resize";
container.addChild(handle);
});
}
/**
* @method create_corner_handles
* @private
* @description Creates corner handles for the resize UI
* @param {Container} container - The container to add handles to
* @param {number} width - Width of the resize area
* @param {number} height - Height of the resize area
* @returns {void}
*/
private create_corner_handles(
container: Container,
width: number,
height: number
): void {
const corners = [
{ x: 0, y: 0, cursor: "nwse-resize" },
{ x: width, y: 0, cursor: "nesw-resize" },
{ x: 0, y: height, cursor: "nesw-resize" },
{ x: width, y: height, cursor: "nwse-resize" }
];
corners.forEach(({ x, y, cursor }, i) => {
const corner = this.create_handle(false);
corner.x = x;
corner.y = y;
corner.on("pointerdown", (event: FederatedPointerEvent) => {
this.handle_pointer_down(event, corner, i, -1);
});
corner.cursor = cursor;
container.addChild(corner);
});
}
/**
* @method handle_pointer_down
* @private
* @description Handles pointer down events on handles
* @param {FederatedPointerEvent} event - The pointer event
* @param {Container} handle - The handle that was clicked
* @param {number} corner_index - Index of the corner (-1 if not a corner)
* @param {number} edge_index - Index of the edge (-1 if not an edge)
* @returns {void}
*/
private handle_pointer_down(
event: FederatedPointerEvent,
handle: Container,
corner_index: number,
edge_index: number
): void {
if (this.current_subtool !== "size") return;
event.stopPropagation();
this.is_dragging = true;
this.selected_handle = handle;
this.active_corner_index = corner_index;
this.active_edge_index = edge_index;
const local_pos = this.image_editor_context.image_container.toLocal(
event.global
);
this.last_pointer_position = new Point(local_pos.x, local_pos.y);
if (this.image_editor_context.background_image) {
const bg = this.image_editor_context.background_image;
this.original_state = {
width: bg.width,
height: bg.height,
x: bg.position.x,
y: bg.position.y
};
}
}
/**
* Maintains aspect ratio during resizing
* @param new_width - The new width
* @param new_height - The new height
* @param original_aspect_ratio - The original aspect ratio
* @param delta - The delta to apply
* @returns The adjusted dimensions
*/
private maintain_aspect_ratio(
new_width: number,
new_height: number,
original_aspect_ratio: number,
delta: Point
): { width: number; height: number } {
const abs_delta_x = Math.abs(delta.x);
const abs_delta_y = Math.abs(delta.y);
if (abs_delta_x > abs_delta_y * 1.2) {
new_height = new_width / original_aspect_ratio;
} else if (abs_delta_y > abs_delta_x * 1.2) {
new_width = new_height * original_aspect_ratio;
} else {
if (new_width / original_aspect_ratio > new_height) {
new_height = new_width / original_aspect_ratio;
} else {
new_width = new_height * original_aspect_ratio;
}
}
return { width: new_width, height: new_height };
}
/**
* Limits dimensions based on constraints
* @param width - The width to limit
* @param height - The height to limit
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
* @returns The limited dimensions
*/
private limit_dimensions(
width: number,
height: number,
maintain_aspect_ratio: boolean
): { width: number; height: number } {
const max_width = this.dimensions.width;
const max_height = this.dimensions.height;
let new_width = width;
let new_height = height;
if (new_width > max_width) {
if (maintain_aspect_ratio && this.active_corner_index !== -1) {
new_height = (max_width / new_width) * new_height;
}
new_width = max_width;
}
if (new_height > max_height) {
if (maintain_aspect_ratio && this.active_corner_index !== -1) {
new_width = (max_height / new_height) * new_width;
}
new_height = max_height;
}
new_width = Math.max(20, new_width);
new_height = Math.max(20, new_height);
return { width: new_width, height: new_height };
}
/**
* Calculates position deltas based on dimension changes
* @param old_width - The original width
* @param old_height - The original height
* @param new_width - The new width
* @param new_height - The new height
* @returns Object containing x and y position deltas
*/
private calculate_position_deltas(
old_width: number,
old_height: number,
new_width: number,
new_height: number
): { x: number; y: number } {
let delta_x = 0;
let delta_y = 0;
if (this.active_corner_index === 0 || this.active_edge_index === 2) {
delta_x = old_width - new_width;
} else if (this.active_corner_index === 2) {
delta_x = old_width - new_width;
delta_y = 0;
}
if (
this.active_corner_index === 0 ||
this.active_corner_index === 1 ||
this.active_edge_index === 0
) {
delta_y = old_height - new_height;
}
return { x: delta_x, y: delta_y };
}
/**
* Applies boundary constraints to keep the image within the container
* @private
*/
private apply_boundary_constraints(): void {
if (!this.image_editor_context.background_image) return;
const bg = this.image_editor_context.background_image;
const image_container = this.image_editor_context.image_container;
let new_x = bg.position.x;
let new_y = bg.position.y;
const container_width = image_container.width;
const container_height = image_container.height;
const bg_width = bg.width;
const bg_height = bg.height;
const effectiveWidth = container_width - this.borderRegion * 2;
const effectiveHeight = container_height - this.borderRegion * 2;
if (bg_width > effectiveWidth) {
const min_x = -(bg_width - effectiveWidth) + this.borderRegion;
const max_x = this.borderRegion;
new_x = Math.max(min_x, Math.min(max_x, new_x));
} else {
const min_x = this.borderRegion;
const max_x = container_width - bg_width - this.borderRegion;
if (new_x < min_x || new_x > max_x) {
new_x = Math.max(min_x, Math.min(max_x, new_x));
}
}
if (bg_height > effectiveHeight) {
const min_y = -(bg_height - effectiveHeight) + this.borderRegion;
const max_y = this.borderRegion;
new_y = Math.max(min_y, Math.min(max_y, new_y));
} else {
const min_y = this.borderRegion;
const max_y = container_height - bg_height - this.borderRegion;
if (new_y < min_y || new_y > max_y) {
new_y = Math.max(min_y, Math.min(max_y, new_y));
}
}
bg.position.set(new_x, new_y);
if (this.resize_ui_container) {
const total_x = image_container.position.x + new_x * this.scale;
const total_y = image_container.position.y + new_y * this.scale;
this.resize_ui_container.position.set(total_x, total_y);
}
}
/**
* Updates the resize bounds based on the delta
* @param delta - The delta to apply
* @param maintain_aspect_ratio - Whether to maintain the aspect ratio
*/
private update_resize_bounds(
delta: Point,
maintain_aspect_ratio = false
): void {
const background_image = this.image_editor_context.background_image;
if (!background_image) return;
const original_width = background_image.width;
const original_height = background_image.height;
const original_x = background_image.position.x;
const original_y = background_image.position.y;
const original_aspect_ratio = original_width / original_height;
let current_position: Point;
if (this.active_corner_index !== -1) {
switch (this.active_corner_index) {
case 0:
current_position = new Point(
original_x - delta.x,
original_y - delta.y
);
break;
case 1:
current_position = new Point(
original_x + original_width + delta.x,
original_y - delta.y
);
break;
case 2:
current_position = new Point(
original_x - delta.x,
original_y + original_height + delta.y
);
break;
case 3:
current_position = new Point(
original_x + original_width + delta.x,
original_y + original_height + delta.y
);
break;
default:
return;
}
const dimensions = this.handle_direct_corner_resize(
current_position,
original_width,
original_height,
original_x,
original_y,
original_aspect_ratio,
maintain_aspect_ratio
);
background_image.width = dimensions.width;
background_image.height = dimensions.height;
background_image.position.set(dimensions.x, dimensions.y);
} else if (this.active_edge_index !== -1) {
switch (this.active_edge_index) {
case 0:
current_position = new Point(
original_x + original_width / 2,
original_y - delta.y
);
break;
case 1:
current_position = new Point(
original_x + original_width / 2,
original_y + original_height + delta.y
);
break;
case 2:
current_position = new Point(
original_x - delta.x,
original_y + original_height / 2
);
break;
case 3:
current_position = new Point(
original_x + original_width + delta.x,
original_y + original_height / 2
);
break;
default:
return;
}
const dimensions = this.handle_direct_edge_resize(
current_position,
original_width,
original_height,
original_x,
original_y
);
background_image.width = dimensions.width;
background_image.height = dimensions.height;
background_image.position.set(dimensions.x, dimensions.y);
}
this.update_resize_ui();
}
/**
* updates the resize ui position and dimensions
*/
private update_resize_ui(): void {
if (
!this.resize_ui_container ||
!this.image_editor_context.background_image ||
!this.image_editor_context.background_image.position
)
return;
const background_image = this.image_editor_context.background_image;
const image_container = this.image_editor_context.image_container;
const container_global_pos = image_container.getGlobalPosition();
const bg_pos_x = background_image.position.x * this.scale;
const bg_pos_y = background_image.position.y * this.scale;
this.resize_ui_container.position.set(
container_global_pos.x + bg_pos_x,
container_global_pos.y + bg_pos_y
);
const scaled_width = background_image.width * this.scale;
const scaled_height = background_image.height * this.scale;
const outline = this.resize_ui_container.getChildAt(0) as Graphics;
outline
.clear()
.rect(0, 0, scaled_width, scaled_height)
.stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.3 });
const dotted_outline = this.resize_ui_container.getChildAt(1) as Graphics;
dotted_outline.clear();
dotted_outline.rect(0, 0, scaled_width, scaled_height).stroke({
width: 1,
color: 0x000000,
alpha: 0.7,
pixelLine: true
});
if (
!this.is_dragging &&
!this.is_moving &&
Math.abs(this.scale - this.last_scale) < 0.001
) {
const dash_length = 5;
const gap_length = 5;
const total_length = dash_length + gap_length;
for (let x = 0; x < scaled_width; x += total_length * 2) {
dotted_outline
.rect(x, 0, Math.min(dash_length, scaled_width - x), 1)
.fill(0x000000);
dotted_outline
.rect(
x,
scaled_height - 1,
Math.min(dash_length, scaled_width - x),
1
)
.fill(0x000000);
}
for (let y = 0; y < scaled_height; y += total_length * 2) {
dotted_outline
.rect(0, y, 1, Math.min(dash_length, scaled_height - y))
.fill(0x000000);
dotted_outline
.rect(
scaled_width - 1,
y,
1,
Math.min(dash_length, scaled_height - y)
)
.fill(0x000000);
}
}
this.last_scale = this.scale;
const move_area = this.resize_ui_container.getChildAt(2) as Graphics;
move_area.clear().rect(0, 0, scaled_width, scaled_height).fill(0xffffff, 0);
this.update_handle_positions(scaled_width, scaled_height);
}
/**
* updates the positions of all handles
* @param width - width of the resize area
* @param height - height of the resize area
*/
private update_handle_positions(width: number, height: number): void {
if (!this.resize_ui_container) return;
const handles = this.resize_ui_container.children.slice(3);
const move_area = this.resize_ui_container.getChildAt(2) as Graphics;
move_area.clear().rect(0, 0, width, height).fill(0xffffff, 0);
const corners = handles.slice(0, 4);
const cornerPositions = [
{ x: 0, y: 0 },
{ x: width, y: 0 },
{ x: 0, y: height },
{ x: width, y: height }
];
corners.forEach((handle, i) => {
handle.position.set(cornerPositions[i].x, cornerPositions[i].y);
});
const edges = handles.slice(4);
edges.forEach((handle, i) => {
if (i < 2) {
handle.position.set(width / 2, i === 0 ? 0 : height);
} else {
handle.position.set(i === 2 ? 0 : width, height / 2);
}
});
}
/**
* sets up event listeners for the resize tool
*/
private setup_event_listeners(): void {
const stage = this.image_editor_context.app.stage;
stage.eventMode = "static";
stage.on("pointermove", this.handle_pointer_move.bind(this));
stage.on("pointerup", this.handle_pointer_up.bind(this));
stage.on("pointerupoutside", this.handle_pointer_up.bind(this));
this.setup_dom_event_listeners();
}
/**
* Sets up direct DOM event listeners for dragging the background image
*/
private setup_dom_event_listeners(): void {
this.cleanup_dom_event_listeners();
const canvas = this.image_editor_context.app.canvas;
this.dom_mousedown_handler = (e: MouseEvent) => {
if (this.current_subtool !== "size") return;
if (this.is_dragging || this.selected_handle) return;
if (!this.image_editor_context.background_image) return;
const bg = this.image_editor_context.background_image;
const container = this.image_editor_context.image_container;
const containerBounds = container.getBounds();
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const localX = (x - containerBounds.x) / this.scale;
const localY = (y - containerBounds.y) / this.scale;
if (
localX >= bg.position.x &&
localX <= bg.position.x + bg.width &&
localY >= bg.position.y &&
localY <= bg.position.y + bg.height
) {
this.is_moving = true;
this.last_pointer_position = new Point(localX, localY);
this.original_state = {
width: bg.width,
height: bg.height,
x: bg.position.x,
y: bg.position.y
};
e.preventDefault();
e.stopPropagation();
}
};
this.dom_mousemove_handler = (e: MouseEvent) => {
if (this.current_subtool !== "size" || !this.is_moving) return;
if (
!this.last_pointer_position ||
!this.image_editor_context.background_image
)
return;
const container = this.image_editor_context.image_container;
const containerBounds = container.getBounds();
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const localX = (x - containerBounds.x) / this.scale;
const localY = (y - containerBounds.y) / this.scale;
const current_position = new Point(localX, localY);
this.handle_image_dragging(current_position);
this.last_pointer_position = current_position;
e.preventDefault();
e.stopPropagation();
};
this.dom_mouseup_handler = (e: MouseEvent) => {
if (this.current_subtool !== "size") return;
if (this.original_state && this.image_editor_context.background_image) {
const bg = this.image_editor_context.background_image;
const new_state = {
width: bg.width,
height: bg.height,
x: bg.position.x,
y: bg.position.y
};
if (
this.original_state.width !== new_state.width ||
this.original_state.height !== new_state.height ||
this.original_state.x !== new_state.x ||
this.original_state.y !== new_state.y
) {
const resize_command = new ResizeCommand(
this.image_editor_context,
this.original_state,
new_state,
this.update_resize_ui.bind(this)
);
this.image_editor_context.execute_command(resize_command);
}
this.original_state = null;
}
this.is_moving = false;
this.last_pointer_position = null;
this.update_resize_ui();
e.preventDefault();
e.stopPropagation();
};
canvas.addEventListener("mousedown", this.dom_mousedown_handler);
window.addEventListener("mousemove", this.dom_mousemove_handler);
window.addEventListener("mouseup", this.dom_mouseup_handler);
}
/**
* Cleans up DOM event listeners
*/
private cleanup_dom_event_listeners(): void {
const canvas = this.image_editor_context.app.canvas;
if (this.dom_mousedown_handler) {
canvas.removeEventListener("mousedown", this.dom_mousedown_handler);
this.dom_mousedown_handler = null;
}
if (this.dom_mousemove_handler) {
window.removeEventListener("mousemove", this.dom_mousemove_handler);
this.dom_mousemove_handler = null;
}
if (this.dom_mouseup_handler) {
window.removeEventListener("mouseup", this.dom_mouseup_handler);
this.dom_mouseup_handler = null;
}
}
/**
* removes event listeners for the resize tool
*/
private cleanup_event_listeners(): void {
const stage = this.image_editor_context.app.stage;
stage.off("pointermove", this.handle_pointer_move.bind(this));
stage.off("pointerup", this.handle_pointer_up.bind(this));
stage.off("pointerupoutside", this.handle_pointer_up.bind(this));
this.cleanup_dom_event_listeners();
}
/**
* Handles pointer move events
*/
private handle_pointer_move(event: FederatedPointerEvent): void {
if (this.current_subtool !== "size") return;
const local_pos = this.image_editor_context.image_container.toLocal(
event.global
);
const current_position = new Point(local_pos.x, local_pos.y);
if (this.is_moving && this.last_pointer_position) {
this.handle_image_dragging(current_position);
this.last_pointer_position = current_position;
} else if (this.is_dragging && this.selected_handle) {
if (!this.image_editor_context.background_image) return;
const bg = this.image_editor_context.background_image;
const maintain_aspect_ratio =
this.active_corner_index !== -1 && !event.shiftKey;
const original_width = bg.width;
const original_height = bg.height;
const original_x = bg.position.x;
const original_y = bg.position.y;
const original_aspect_ratio = original_width / original_height;
let dimensions: { width: number; height: number; x: number; y: number };
if (this.active_corner_index !== -1) {
dimensions = this.handle_direct_corner_resize(
current_position,
original_width,
original_height,
original_x,
original_y,
original_aspect_ratio,
maintain_aspect_ratio
);
} else if (this.active_edge_index !== -1) {
dimensions = this.handle_direct_edge_resize(
current_position,
original_width,
original_height,
original_x,
original_y
);
} else {
return;
}
dimensions = this.limit_dimensions_to_container(
dimensions,
original_x,
original_y,
original_width,
original_height,
maintain_aspect_ratio
);
bg.width = dimensions.width;
bg.height = dimensions.height;
bg.position.set(dimensions.x, dimensions.y);
this.update_resize_ui();
this.last_pointer_position = current_position;
}
}
/**
* handles pointer up events
*/
private handle_pointer_up(): void {
if (this.current_subtool !== "size") return;
this.is_dragging = false;
this.is_moving = false;
this.selected_handle = null;
this.last_pointer_position = null;
this.active_corner_index = -1;
this.active_edge_index = -1;
this.update_resize_ui();
this.notify("change");
}
/**
* Handles dragging of the background image
* @param current_position - The current pointer position
* @private
*/
private handle_image_dragging(current_position: Point): void {
if (
!this.last_pointer_position ||
!this.image_editor_context.background_image
) {
return;
}
const bg = this.image_editor_context.background_image;
const image_container =
this.image_editor_context.image_container.getLocalBounds();
const dx = current_position.x - this.last_pointer_position.x;
const dy = current_position.y - this.last_pointer_position.y;
let new_x = bg.position.x + dx;
let new_y = bg.position.y + dy;
const container_width = image_container.width;
const container_height = image_container.height;
const bg_width = bg.width;
const bg_height = bg.height;
const effectiveWidth = container_width - this.borderRegion * 2;
const effectiveHeight = container_height - this.borderRegion * 2;
if (bg_width > effectiveWidth) {
const min_x = -(bg_width - effectiveWidth) + this.borderRegion;
const max_x = this.borderRegion;
new_x = Math.max(min_x, Math.min(max_x, new_x));
} else {
const min_x = this.borderRegion;
const max_x = container_width - bg_width - this.borderRegion;
new_x = Math.max(min_x, Math.min(max_x, new_x));
}
if (bg_height > effectiveHeight) {
const min_y = -(bg_height - effectiveHeight) + this.borderRegion;
const max_y = this.borderRegion;
new_y = Math.max(min_y, Math.min(max_y, new_y));
} else {
const min_y = this.borderRegion;
const max_y = container_height - bg_height - this.borderRegion;
new_y = Math.max(min_y, Math.min(max_y, new_y));
}
bg.position.set(new_x, new_y);
this.update_resize_ui();
}
/**
* Handles corner resizing
* @param current_position - The current mouse position
* @param original_width - The original width
* @param original_height - The original height
* @param original_x - The original x position
* @param original_y - The original y position
* @param original_aspect_ratio - The original aspect ratio
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
* @returns The new dimensions and position
* @private
*/
private handle_direct_corner_resize(
current_position: Point,
original_width: number,
original_height: number,
original_x: number,
original_y: number,
original_aspect_ratio: number,
maintain_aspect_ratio: boolean
): { width: number; height: number; x: number; y: number } {
let raw_width = 0;
let raw_height = 0;
let new_x = original_x;
let new_y = original_y;
switch (this.active_corner_index) {
case 0:
raw_width = original_x + original_width - current_position.x;
raw_height = original_y + original_height - current_position.y;
break;
case 1:
raw_width = current_position.x - original_x;
raw_height = original_y + original_height - current_position.y;
break;
case 2:
raw_width = original_x + original_width - current_position.x;
raw_height = current_position.y - original_y;
break;
case 3:
raw_width = current_position.x - original_x;
raw_height = current_position.y - original_y;
break;
}
raw_width = Math.max(20, raw_width);
raw_height = Math.max(20, raw_height);
let new_width = raw_width;
let new_height = raw_height;
if (maintain_aspect_ratio) {
const diagonal_vector = {
x: current_position.x - (original_x + original_width / 2),
y: current_position.y - (original_y + original_height / 2)
};
const angle = Math.abs(Math.atan2(diagonal_vector.y, diagonal_vector.x));
if (angle < Math.PI / 4 || angle > (3 * Math.PI) / 4) {
new_height = new_width / original_aspect_ratio;
} else {
new_width = new_height * original_aspect_ratio;
}
}
switch (this.active_corner_index) {
case 0:
new_x = original_x + original_width - new_width;
new_y = original_y + original_height - new_height;
break;
case 1:
new_y = original_y + original_height - new_height;
break;
case 2:
new_x = original_x + original_width - new_width;
break;
case 3:
break;
}
return { width: new_width, height: new_height, x: new_x, y: new_y };
}
/**
* Handles edge resizing
* @param current_position - The current mouse position
* @param original_width - The original width
* @param original_height - The original height
* @param original_x - The original x position
* @param original_y - The original y position
* @returns The new dimensions and position
* @private
*/
private handle_direct_edge_resize(
current_position: Point,
original_width: number,
original_height: number,
original_x: number,
original_y: number
): { width: number; height: number; x: number; y: number } {
let new_width = original_width;
let new_height = original_height;
let new_x = original_x;
let new_y = original_y;
switch (this.active_edge_index) {
case 0:
new_height = Math.max(
20,
original_y + original_height - current_position.y
);
new_y = current_position.y;
break;
case 1:
new_height = Math.max(20, current_position.y - original_y);
break;
case 2:
new_width = Math.max(
20,
original_x + original_width - current_position.x
);
new_x = current_position.x;
break;
case 3:
new_width = Math.max(20, current_position.x - original_x);
break;
}
return { width: new_width, height: new_height, x: new_x, y: new_y };
}
/**
* Limits dimensions to container size
* @param dimensions - The dimensions to limit
* @param original_x - The original x position
* @param original_y - The original y position
* @param original_width - The original width
* @param original_height - The original height
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
* @returns The limited dimensions and position
* @private
*/
private limit_dimensions_to_container(
dimensions: { width: number; height: number; x: number; y: number },
original_x: number,
original_y: number,
original_width: number,
original_height: number,
maintain_aspect_ratio: boolean
): { width: number; height: number; x: number; y: number } {
let {
width: new_width,
height: new_height,
x: new_x,
y: new_y
} = dimensions;
const effective_container_width =
this.dimensions.width - this.borderRegion * 2;
const effective_container_height =
this.dimensions.height - this.borderRegion * 2;
const pre_limit_width = new_width;
const pre_limit_height = new_height;
const size_limited = this.apply_size_limits(
new_width,
new_height,
effective_container_width,
effective_container_height,
maintain_aspect_ratio,
pre_limit_width / pre_limit_height
);
new_width = size_limited.width;
new_height = size_limited.height;
const position_adjusted = this.adjust_position_for_resizing(
new_width,
new_height,
original_x,
original_y,
original_width,
original_height
);
new_x = position_adjusted.x;
new_y = position_adjusted.y;
const border_constrained = this.apply_border_constraints(
new_width,
new_height,
new_x,
new_y,
maintain_aspect_ratio,
pre_limit_width / pre_limit_height
);
return border_constrained;
}
/**
* Apply size limits to ensure dimensions don't exceed container
* @private
*/
private apply_size_limits(
width: number,
height: number,
max_width: number,
max_height: number,
maintain_aspect_ratio: boolean,
aspect_ratio: number
): { width: number; height: number } {
let new_width = width;
let new_height = height;
const needs_width_limit = new_width > max_width;
const needs_height_limit = new_height > max_height;
if (needs_width_limit && needs_height_limit) {
const width_scale = max_width / new_width;
const height_scale = max_height / new_height;
if (width_scale < height_scale) {
new_width = max_width;
if (maintain_aspect_ratio) {
new_height = new_width / aspect_ratio;
}
} else {
new_height = max_height;
if (maintain_aspect_ratio) {
new_width = new_height * aspect_ratio;
}
}
} else if (needs_width_limit) {
new_width = max_width;
if (maintain_aspect_ratio) {
new_height = new_width / aspect_ratio;
}
} else if (needs_height_limit) {
new_height = max_height;
if (maintain_aspect_ratio) {
new_width = new_height * aspect_ratio;
}
}
return { width: new_width, height: new_height };
}
/**
* Adjust position based on which handles are being dragged
* @private
*/
private adjust_position_for_resizing(
new_width: number,
new_height: number,
original_x: number,
original_y: number,
original_width: number,
original_height: number
): { x: number; y: number } {
let new_x = original_x;
let new_y = original_y;
switch (this.active_corner_index) {
case 0:
new_x = original_x + original_width - new_width;
new_y = original_y + original_height - new_height;
break;
case 1:
new_y = original_y + original_height - new_height;
break;
case 2:
new_x = original_x + original_width - new_width;
break;
case 3:
break;
}
if (this.active_edge_index !== -1) {
switch (this.active_edge_index) {
case 0:
new_y = original_y + original_height - new_height;
break;
case 2:
new_x = original_x + original_width - new_width;
break;
}
}
return { x: new_x, y: new_y };
}
/**
* Apply border region constraints to prevent image from intruding on border
* @private
*/
private apply_border_constraints(
width: number,
height: number,
x: number,
y: number,
maintain_aspect_ratio: boolean,
aspect_ratio: number
): { width: number; height: number; x: number; y: number } {
const top_left_constrained = this.apply_top_left_constraints(
width,
height,
x,
y,
maintain_aspect_ratio,
aspect_ratio
);
const all_constrained = this.apply_bottom_right_constraints(
top_left_constrained.width,
top_left_constrained.height,
top_left_constrained.x,
top_left_constrained.y,
maintain_aspect_ratio,
aspect_ratio
);
all_constrained.width = Math.max(20, all_constrained.width);
all_constrained.height = Math.max(20, all_constrained.height);
return all_constrained;
}
/**
* Apply top and left border constraints
* @private
*/
private apply_top_left_constraints(
width: number,
height: number,
x: number,
y: number,
maintain_aspect_ratio: boolean,
aspect_ratio: number
): { width: number; height: number; x: number; y: number } {
let new_width = width;
let new_height = height;
let new_x = x;
let new_y = y;
if (new_x < this.borderRegion) {
if (
this.active_corner_index === 0 ||
this.active_corner_index === 2 ||
this.active_edge_index === 2
) {
new_width -= this.borderRegion - new_x;
if (maintain_aspect_ratio) {
new_height = new_width / aspect_ratio;
}
}
new_x = this.borderRegion;
}
if (new_y < this.borderRegion) {
if (
this.active_corner_index === 0 ||
this.active_corner_index === 1 ||
this.active_edge_index === 0
) {
new_height -= this.borderRegion - new_y;
if (maintain_aspect_ratio) {
new_width = new_height * aspect_ratio;
}
}
new_y = this.borderRegion;
}
return { width: new_width, height: new_height, x: new_x, y: new_y };
}
/**
* Apply bottom and right border constraints
* @private
*/
private apply_bottom_right_constraints(
width: number,
height: number,
x: number,
y: number,
maintain_aspect_ratio: boolean,
aspect_ratio: number
): { width: number; height: number; x: number; y: number } {
let new_width = width;
let new_height = height;
let new_x = x;
let new_y = y;
if (new_x + new_width > this.dimensions.width - this.borderRegion) {
if (
this.active_corner_index === 1 ||
this.active_corner_index === 3 ||
this.active_edge_index === 3
) {
new_width = this.dimensions.width - this.borderRegion - new_x;
if (maintain_aspect_ratio) {
new_height = new_width / aspect_ratio;
}
} else {
const right_edge = new_x + new_width;
const excess = right_edge - (this.dimensions.width - this.borderRegion);
new_x = Math.max(this.borderRegion, new_x - excess);
}
}
if (new_y + new_height > this.dimensions.height - this.borderRegion) {
if (
this.active_corner_index === 2 ||
this.active_corner_index === 3 ||
this.active_edge_index === 1
) {
new_height = this.dimensions.height - this.borderRegion - new_y;
if (maintain_aspect_ratio) {
new_width = new_height * aspect_ratio;
}
} else {
const bottom_edge = new_y + new_height;
const excess =
bottom_edge - (this.dimensions.height - this.borderRegion);
new_y = Math.max(this.borderRegion, new_y - excess);
}
}
return { width: new_width, height: new_height, x: new_x, y: new_y };
}
/**
* Shows the resize UI
* @private
*/
private show_resize_ui(): void {
if (this.resize_ui_container) {
this.resize_ui_container.visible = true;
this.update_resize_ui();
}
if (this.image_editor_context.background_image) {
const bg = this.image_editor_context.background_image;
this.current_cursor = bg.cursor;
bg.cursor = "move";
}
this.setup_dom_event_listeners();
}
/**
* Hides the resize UI
* @private
*/
private hide_resize_ui(): void {
if (this.resize_ui_container) {
this.resize_ui_container.visible = false;
}
if (this.image_editor_context.background_image) {
const bg = this.image_editor_context.background_image;
bg.cursor = this.current_cursor;
}
this.cleanup_dom_event_listeners();
}
on<T extends string>(event: T, callback: () => void): void {
this.event_callbacks.set(event, [
...(this.event_callbacks.get(event) || []),
callback
]);
}
off<T extends string>(event: T, callback: () => void): void {
this.event_callbacks.set(
event,
this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || []
);
}
private notify<T extends string>(event: T): void {
for (const callback of this.event_callbacks.get(event) || []) {
callback();
}
}
/**
* Sets the border region explicitly
* @param borderRegion The border region size in pixels
*/
public set_border_region(borderRegion: number): void {
this.borderRegion = borderRegion;
if (this.image_editor_context?.background_image) {
(this.image_editor_context.background_image as any).borderRegion =
borderRegion;
}
if (this.resize_ui_container) {
this.update_resize_ui();
}
if (
this.current_subtool === "size" &&
this.image_editor_context?.background_image
) {
this.apply_boundary_constraints();
}
}
}
|