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 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27253x 27253x 27253x 27253x 24314x 24314x 27253x 669x 669x 2939x 1678x 6x 6x 1678x 1532x 1532x 1672x 130x 130x 130x 130x 130x 1678x 27253x 602x 602x 602x 602x 602x 602x 602x 502x 502x 502x 502x 241x 241x 241x 502x 303x 303x 303x 602x 100x 100x 100x 100x 602x 602x 602x 27253x 27253x 27253x 27253x 27253x 1x 1x 27623x 27623x 27623x 27623x 27623x 27623x 1x 5535x 5535x 1x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 63x 63x 63x 63x 1x 1014x 1014x 1014x 1x 1x 1x 106x 106x 106x 106x 106x 106x 22x 22x 106x 22x 22x 106x 106x 106x 106x 1x 1041x 1041x 1x 33x 10x 10x 10x 33x 33x 33x 1x 1031x 1031x 1x 33x 9x 9x 9x 33x 33x 33x 1x 993x 993x 1x 30x 12x 12x 12x 30x 30x 30x 30x 1x 992x 992x 1x 30x 13x 13x 13x 30x 30x 30x 30x 1x 945x 945x 1x 1347x 1343x 1347x 4x 2x 2x 4x 4x 1347x 1347x 1347x 1x 956x 956x 1x 1347x 1343x 1347x 4x 2x 2x 4x 4x 1347x 1347x 1347x 1x 925x 925x 925x 1x 1333x 1333x 1333x 1333x 1333x 1x 1x 876x 876x 1x 1224x 14x 1210x 1224x 1x 1612x 1612x 1612x 1612x 1x 14x 14x 14x 14x 14x 1x 222x 222x 222x 222x 216x 216x 210x 222x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1x 13303x 13303x 13303x 13303x 13303x 13303x 13303x 1x 423x 423x 423x 423x 423x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 1456x 1456x 1456x 288x 72x 72x 72x 72x 288x 1x 1x 1x 1x 1x 1x 1x 1x 1399x 1399x 1399x 1399x 1x 1399x 1399x 1399x 1399x 1399x 1399x 1399x 1x 1x 36x 36x 36x 36x 144x 144x 144x 6109x 6109x 144x 144x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 612x 612x 612x 36x 36x 36x 2x 2x 2x 2x 2x 36x 36x 36x 1x 1x 1x | /* * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey * http://juerglehni.com/ & https://puckey.studio/ * * Distributed under the MIT license. See LICENSE file for details. * * All rights reserved. */ // TODO: remove eslint-disable comment and deal with errors over time /* eslint-disable */ import { ref } from '~/globals'; import { Base } from '~/straps'; import { Point, LinkedPoint } from './Point'; import { Formatter } from '~/util/Formatter'; import { ItemSelection } from '~/item/ItemSelection'; import { Size, LinkedSize } from '~/basic/Size'; /** * @name Rectangle * * @class A Rectangle specifies an area that is enclosed by it's top-left * point (x, y), its width, and its height. It should not be confused with a * rectangular path, it is not an item. */ export const Rectangle = Base.extend( /** @lends Rectangle# */ { _class: 'Rectangle', // Tell Base.read that the Rectangle constructor supports reading with index _readIndex: true, // Enforce creation of beans, as bean getters have hidden parameters. // See #getPoint() below. beans: true, /** * Creates a Rectangle object. * * @name Rectangle#initialize * @param {Point} point the top-left point of the rectangle * @param {Size} size the size of the rectangle */ /** * Creates a Rectangle object. * * @name Rectangle#initialize * @param {Object} object an object containing properties to be set on the * rectangle * * @example // Create a rectangle between {x: 20, y: 20} and {x: 80, y:80} * var rectangle = new Rectangle({ * point: [20, 20], * size: [60, 60] * }); * * @example // Create a rectangle between {x: 20, y: 20} and {x: 80, y:80} * var rectangle = new Rectangle({ * from: [20, 20], * to: [80, 80] * }); */ /** * Creates a rectangle object. * * @name Rectangle#initialize * @param {Number} x the left coordinate * @param {Number} y the top coordinate * @param {Number} width * @param {Number} height */ /** * Creates a rectangle object from the passed points. These do not * necessarily need to be the top left and bottom right corners, the * constructor figures out how to fit a rectangle between them. * * @name Rectangle#initialize * @param {Point} from the first point defining the rectangle * @param {Point} to the second point defining the rectangle */ /** * Creates a new rectangle object from the passed rectangle object. * * @name Rectangle#initialize * @param {Rectangle} rectangle */ initialize: function Rectangle(arg0, arg1, arg2, arg3) { var args = arguments, type = typeof arg0, read; if (type === 'number') { // new Rectangle(x, y, width, height) this._set(arg0, arg1, arg2, arg3); read = 4; } else if (type === 'undefined' || arg0 === null) { // new Rectangle(), new Rectangle(null) this._set(0, 0, 0, 0); read = arg0 === null ? 1 : 0; } else if (args.length === 1) { // This can either be an array, or an object literal. if (Array.isArray(arg0)) { this._set.apply(this, arg0); read = 1; } else if (arg0.x !== undefined || arg0.width !== undefined) { // Another rectangle or a simple object literal // describing one. Use duck typing, and 0 as defaults. this._set(arg0.x || 0, arg0.y || 0, arg0.width || 0, arg0.height || 0); read = 1; } else if (arg0.from === undefined && arg0.to === undefined) { // Use `Base.readSupported()` to read and consume whatever // property the rectangle can receive, but handle `from` / `to` // separately below. this._set(0, 0, 0, 0); if (Base.readSupported(args, this)) { read = 1; } } } if (read === undefined) { // Read a point argument and look at the next value to see whether // it's a size or a point, then read accordingly. // We're supporting both reading from a normal arguments list and // covering the Rectangle({ from: , to: }) constructor, through // Point.readNamed(). var frm = Point.readNamed(args, 'from'), next = Base.peek(args), x = frm.x, y = frm.y, width, height; if ((next && next.x !== undefined) || Base.hasNamed(args, 'to')) { // new Rectangle(from, to) // Read above why we can use readNamed() to cover both cases. var to = Point.readNamed(args, 'to'); width = to.x - x; height = to.y - y; // Check if horizontal or vertical order needs to be reversed. if (width < 0) { x = to.x; width = -width; } if (height < 0) { y = to.y; height = -height; } } else { // new Rectangle(point, size) var size = Size.read(args); width = size.width; height = size.height; } this._set(x, y, width, height); read = (args as any).__index; } // arguments.__filtered wouldn't survive the function call even if a // previous arguments list was passed through Function#apply(). // Return it on the object instead, see Base.read() var filtered = (args as any).__filtered; if (filtered) this.__filtered = filtered; if (this.__read) this.__read = read; return this; }, /** * Sets the rectangle to the passed values. Note that any sequence of * parameters that is supported by the various {@link Rectangle()} * constructors also work for calls of `set()`. * * @function * @param {...*} values * @return {Rectangle} */ set: '#initialize', // See Point#_set() for an explanation of #_set(): _set: function (x, y, width, height) { this.x = x; this.y = y; this.width = width; this.height = height; return this; }, /** * The x position of the rectangle. * * @name Rectangle#x * @type Number */ /** * The y position of the rectangle. * * @name Rectangle#y * @type Number */ /** * The width of the rectangle. * * @name Rectangle#width * @type Number */ /** * The height of the rectangle. * * @name Rectangle#height * @type Number */ /** * Returns a copy of the rectangle. * @return {Rectangle} */ clone: function () { return new Rectangle(this.x, this.y, this.width, this.height); }, /** * Checks whether the coordinates and size of the rectangle are equal to * that of the supplied rectangle. * * @param {Rectangle} rect * @return {Boolean} {@true if the rectangles are equal} */ equals: function (rect) { var rt = Base.isPlainValue(rect) ? Rectangle.read(arguments) : rect; return ( rt === this || (rt && this.x === rt.x && this.y === rt.y && this.width === rt.width && this.height === rt.height) || false ); }, /** * @return {String} a string representation of this rectangle */ toString: function () { var f = Formatter.instance; return ( '{ x: ' + f.number(this.x) + ', y: ' + f.number(this.y) + ', width: ' + f.number(this.width) + ', height: ' + f.number(this.height) + ' }' ); }, _serialize: function (options) { var f = options.formatter; // See Point#_serialize() return [f.number(this.x), f.number(this.y), f.number(this.width), f.number(this.height)]; }, /** * The top-left point of the rectangle * * @bean * @type Point */ getPoint: function (_dontLink) { var ctor = _dontLink ? Point : LinkedPoint; return new ctor(this.x, this.y, this, 'setPoint'); }, setPoint: function (/* point */) { var point = Point.read(arguments); this.x = point.x; this.y = point.y; }, /** * The size of the rectangle * * @bean * @type Size */ getSize: function (_dontLink) { var ctor = _dontLink ? Size : LinkedSize; return new ctor(this.width, this.height, this, 'setSize'); }, // properties to keep track of fix-width / height: They are on by default, // and switched off once properties are used that change the outside of the // rectangle, so combinations of: left / top / right / bottom. _fw: 1, _fh: 1, setSize: function (/* size */) { var size = Size.read(arguments), sx = this._sx, sy = this._sy, w = size.width, h = size.height; // Keep track of how dimensions were specified through this._s* // attributes. // _sx / _sy can either be 0 (left), 0.5 (center) or 1 (right), and is // used as direct factors to calculate the x / y adjustments from the // size differences. // _fw / _fh can either be 0 (off) or 1 (on), and is used to protect // width / height values against changes. if (sx) { this.x += (this.width - w) * sx; } if (sy) { this.y += (this.height - h) * sy; } this.width = w; this.height = h; this._fw = this._fh = 1; }, /** * {@grouptitle Side Positions} * * The position of the left hand side of the rectangle. Note that this * doesn't move the whole rectangle; the right hand side stays where it was. * * @bean * @type Number */ getLeft: function () { return this.x; }, setLeft: function (left) { if (!this._fw) { var amount = left - this.x; this.width -= this._sx === 0.5 ? amount * 2 : amount; } this.x = left; this._sx = this._fw = 0; }, /** * The top coordinate of the rectangle. Note that this doesn't move the * whole rectangle: the bottom won't move. * * @bean * @type Number */ getTop: function () { return this.y; }, setTop: function (top) { if (!this._fh) { var amount = top - this.y; this.height -= this._sy === 0.5 ? amount * 2 : amount; } this.y = top; this._sy = this._fh = 0; }, /** * The position of the right hand side of the rectangle. Note that this * doesn't move the whole rectangle; the left hand side stays where it was. * * @bean * @type Number */ getRight: function () { return this.x + this.width; }, setRight: function (right) { if (!this._fw) { var amount = right - this.x; this.width = this._sx === 0.5 ? amount * 2 : amount; } this.x = right - this.width; this._sx = 1; this._fw = 0; }, /** * The bottom coordinate of the rectangle. Note that this doesn't move the * whole rectangle: the top won't move. * * @bean * @type Number */ getBottom: function () { return this.y + this.height; }, setBottom: function (bottom) { if (!this._fh) { var amount = bottom - this.y; this.height = this._sy === 0.5 ? amount * 2 : amount; } this.y = bottom - this.height; this._sy = 1; this._fh = 0; }, /** * The center-x coordinate of the rectangle. * * @bean * @type Number * @ignore */ getCenterX: function () { return this.x + this.width / 2; }, setCenterX: function (x) { // If we're asked to fix the width or if _sx is already in center mode, // just keep moving the center. if (this._fw || this._sx === 0.5) { this.x = x - this.width / 2; } else { if (this._sx) { this.x += (x - this.x) * 2 * this._sx; } this.width = (x - this.x) * 2; } this._sx = 0.5; this._fw = 0; }, /** * The center-y coordinate of the rectangle. * * @bean * @type Number * @ignore */ getCenterY: function () { return this.y + this.height / 2; }, setCenterY: function (y) { // If we're asked to fix the height or if _sy is already in center mode, // just keep moving the center. if (this._fh || this._sy === 0.5) { this.y = y - this.height / 2; } else { if (this._sy) { this.y += (y - this.y) * 2 * this._sy; } this.height = (y - this.y) * 2; } this._sy = 0.5; this._fh = 0; }, /** * {@grouptitle Corner and Center Point Positions} * * The center point of the rectangle. * * @bean * @type Point */ getCenter: function (_dontLink) { var ctor = _dontLink ? Point : LinkedPoint; return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, setCenter: function (/* point */) { var point = Point.read(arguments); this.setCenterX(point.x); this.setCenterY(point.y); // A special setter where we allow chaining, because it comes in handy // in a couple of places in core. return this; }, /** * The top-left point of the rectangle. * * @name Rectangle#topLeft * @type Point */ /** * The top-right point of the rectangle. * * @name Rectangle#topRight * @type Point */ /** * The bottom-left point of the rectangle. * * @name Rectangle#bottomLeft * @type Point */ /** * The bottom-right point of the rectangle. * * @name Rectangle#bottomRight * @type Point */ /** * The left-center point of the rectangle. * * @name Rectangle#leftCenter * @type Point */ /** * The top-center point of the rectangle. * * @name Rectangle#topCenter * @type Point */ /** * The right-center point of the rectangle. * * @name Rectangle#rightCenter * @type Point */ /** * The bottom-center point of the rectangle. * * @name Rectangle#bottomCenter * @type Point */ /** * The area of the rectangle. * * @bean * @type Number */ getArea: function () { return this.width * this.height; }, /** * @return {Boolean} {@true if the rectangle is empty} */ isEmpty: function () { return this.width === 0 || this.height === 0; }, /** * {@grouptitle Geometric Tests} * * Tests if the specified point is inside the boundary of the rectangle. * * @name Rectangle#contains * @function * @param {Point} point the specified point * @return {Boolean} {@true if the point is inside the rectangle's boundary} * * @example {@paperscript} * // Checking whether the mouse position falls within the bounding * // rectangle of an item: * * // Create a circle shaped path at {x: 80, y: 50} * // with a radius of 30. * var circle = new Path.Circle(new Point(80, 50), 30); * circle.fillColor = 'red'; * * function onMouseMove(event) { * // Check whether the mouse position intersects with the * // bounding box of the item: * if (circle.bounds.contains(event.point)) { * // If it intersects, fill it with green: * circle.fillColor = 'green'; * } else { * // If it doesn't intersect, fill it with red: * circle.fillColor = 'red'; * } * } */ /** * Tests if the interior of the rectangle entirely contains the specified * rectangle. * * @name Rectangle#contains * @function * @param {Rectangle} rect the specified rectangle * @return {Boolean} {@true if the rectangle entirely contains the specified * rectangle} * * @example {@paperscript} * // Checking whether the bounding box of one item is contained within * // that of another item: * * // All newly created paths will inherit these styles: * project.currentStyle = { * fillColor: 'green', * strokeColor: 'black' * }; * * // Create a circle shaped path at {x: 80, y: 50} * // with a radius of 45. * var largeCircle = new Path.Circle(new Point(80, 50), 45); * * // Create a smaller circle shaped path in the same position * // with a radius of 30. * var circle = new Path.Circle(new Point(80, 50), 30); * * function onMouseMove(event) { * // Move the circle to the position of the mouse: * circle.position = event.point; * * // Check whether the bounding box of the smaller circle * // is contained within the bounding box of the larger item: * if (largeCircle.bounds.contains(circle.bounds)) { * // If it does, fill it with green: * circle.fillColor = 'green'; * largeCircle.fillColor = 'green'; * } else { * // If doesn't, fill it with red: * circle.fillColor = 'red'; * largeCircle.fillColor = 'red'; * } * } */ contains: function (arg) { // Detect rectangles either by checking for 'width' on the passed object // or by looking at the amount of elements in the arguments list, // or the passed array: return (arg && arg.width !== undefined) || (Array.isArray(arg) ? arg : arguments).length === 4 ? this._containsRectangle(Rectangle.read(arguments)) : this._containsPoint(Point.read(arguments)); }, _containsPoint: function (point) { var x = point.x, y = point.y; return x >= this.x && y >= this.y && x <= this.x + this.width && y <= this.y + this.height; }, _containsRectangle: function (rect) { var x = rect.x, y = rect.y; return ( x >= this.x && y >= this.y && x + rect.width <= this.x + this.width && y + rect.height <= this.y + this.height ); }, /** * Tests if the interior of this rectangle intersects the interior of * another rectangle. Rectangles just touching each other are considered as * non-intersecting, except if a `epsilon` value is specified by which this * rectangle's dimensions are increased before comparing. * * @param {Rectangle} rect the specified rectangle * @param {Number} [epsilon=0] the epsilon against which to compare the * rectangle's dimensions * @return {Boolean} {@true if the rectangle and the specified rectangle * intersect each other} * * @example {@paperscript} * // Checking whether the bounding box of one item intersects with * // that of another item: * * // All newly created paths will inherit these styles: * project.currentStyle = { * fillColor: 'green', * strokeColor: 'black' * }; * * // Create a circle shaped path at {x: 80, y: 50} * // with a radius of 45. * var largeCircle = new Path.Circle(new Point(80, 50), 45); * * // Create a smaller circle shaped path in the same position * // with a radius of 30. * var circle = new Path.Circle(new Point(80, 50), 30); * * function onMouseMove(event) { * // Move the circle to the position of the mouse: * circle.position = event.point; * * // Check whether the bounding box of the two circle * // shaped paths intersect: * if (largeCircle.bounds.intersects(circle.bounds)) { * // If it does, fill it with green: * circle.fillColor = 'green'; * largeCircle.fillColor = 'green'; * } else { * // If doesn't, fill it with red: * circle.fillColor = 'red'; * largeCircle.fillColor = 'red'; * } * } */ intersects: function (/* rect, epsilon */) { var rect = Rectangle.read(arguments), epsilon = Base.read(arguments) || 0; return ( rect.x + rect.width > this.x - epsilon && rect.y + rect.height > this.y - epsilon && rect.x < this.x + this.width + epsilon && rect.y < this.y + this.height + epsilon ); }, /** * {@grouptitle Boolean Operations} * * Returns a new rectangle representing the intersection of this rectangle * with the specified rectangle. * * @param {Rectangle} rect the rectangle to be intersected with this * rectangle * @return {Rectangle} the largest rectangle contained in both the specified * rectangle and in this rectangle * * @example {@paperscript} * // Intersecting two rectangles and visualizing the result using rectangle * // shaped paths: * * // Create two rectangles that overlap each other * var size = new Size(50, 50); * var rectangle1 = new Rectangle(new Point(25, 15), size); * var rectangle2 = new Rectangle(new Point(50, 40), size); * * // The rectangle that represents the intersection of the * // two rectangles: * var intersected = rectangle1.intersect(rectangle2); * * // To visualize the intersecting of the rectangles, we will * // create rectangle shaped paths using the Path.Rectangle * // constructor. * * // Have all newly created paths inherit a black stroke: * project.currentStyle.strokeColor = 'black'; * * // Create two rectangle shaped paths using the abstract rectangles * // we created before: * new Path.Rectangle(rectangle1); * new Path.Rectangle(rectangle2); * * // Create a path that represents the intersected rectangle, * // and fill it with red: * var intersectionPath = new Path.Rectangle(intersected); * intersectionPath.fillColor = 'red'; */ intersect: function (/* rect */) { var rect = Rectangle.read(arguments), x1 = Math.max(this.x, rect.x), y1 = Math.max(this.y, rect.y), x2 = Math.min(this.x + this.width, rect.x + rect.width), y2 = Math.min(this.y + this.height, rect.y + rect.height); return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, /** * Returns a new rectangle representing the union of this rectangle with the * specified rectangle. * * @param {Rectangle} rect the rectangle to be combined with this rectangle * @return {Rectangle} the smallest rectangle containing both the specified * rectangle and this rectangle */ unite: function (/* rect */) { var rect = Rectangle.read(arguments), x1 = Math.min(this.x, rect.x), y1 = Math.min(this.y, rect.y), x2 = Math.max(this.x + this.width, rect.x + rect.width), y2 = Math.max(this.y + this.height, rect.y + rect.height); return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, /** * Adds a point to this rectangle. The resulting rectangle is the smallest * rectangle that contains both the original rectangle and the specified * point. * * After adding a point, a call to {@link #contains(point)} with the added * point as an argument does not necessarily return `true`. The {@link * Rectangle#contains(point)} method does not return `true` for points on * the right or bottom edges of a rectangle. Therefore, if the added point * falls on the left or bottom edge of the enlarged rectangle, {@link * Rectangle#contains(point)} returns `false` for that point. * * @param {Point} point * @return {Rectangle} the smallest rectangle that contains both the * original rectangle and the specified point */ include: function (/* point */) { var point = Point.read(arguments); var x1 = Math.min(this.x, point.x), y1 = Math.min(this.y, point.y), x2 = Math.max(this.x + this.width, point.x), y2 = Math.max(this.y + this.height, point.y); return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, /** * Returns a new rectangle expanded by the specified amount in horizontal * and vertical directions. * * @name Rectangle#expand * @function * @param {Number|Size|Point} amount the amount to expand the rectangle in * both directions * @return {Rectangle} the expanded rectangle */ /** * Returns a new rectangle expanded by the specified amounts in horizontal * and vertical directions. * * @name Rectangle#expand * @function * @param {Number} hor the amount to expand the rectangle in horizontal * direction * @param {Number} ver the amount to expand the rectangle in vertical * direction * @return {Rectangle} the expanded rectangle */ expand: function (/* amount */) { var amount = Size.read(arguments), hor = amount.width, ver = amount.height; return new Rectangle(this.x - hor / 2, this.y - ver / 2, this.width + hor, this.height + ver); }, /** * Returns a new rectangle scaled by the specified amount from its center. * * @name Rectangle#scale * @function * @param {Number} amount * @return {Rectangle} the scaled rectangle */ /** * Returns a new rectangle scaled in horizontal direction by the specified * `hor` amount and in vertical direction by the specified `ver` amount * from its center. * * @name Rectangle#scale * @function * @param {Number} hor * @param {Number} ver * @return {Rectangle} the scaled rectangle */ scale: function (hor, ver) { return this.expand(this.width * hor - this.width, this.height * (ver === undefined ? hor : ver) - this.height); }, }, Base.each( [ ['Top', 'Left'], ['Top', 'Right'], ['Bottom', 'Left'], ['Bottom', 'Right'], ['Left', 'Center'], ['Top', 'Center'], ['Right', 'Center'], ['Bottom', 'Center'], ], function (parts, index) { var part = parts.join(''), // find out if the first of the pair is an x or y property, // by checking the first character for [R]ight or [L]eft; xFirst = /^[RL]/.test(part); // Rename Center to CenterX or CenterY: if (index >= 4) parts[1] += xFirst ? 'Y' : 'X'; var x = parts[xFirst ? 0 : 1], y = parts[xFirst ? 1 : 0], getX = 'get' + x, getY = 'get' + y, setX = 'set' + x, setY = 'set' + y, get = 'get' + part, set = 'set' + part; this[get] = function (_dontLink) { var ctor = _dontLink ? Point : LinkedPoint; return new ctor(this[getX](), this[getY](), this, set); }; this[set] = function (/* point */) { var point = Point.read(arguments); this[setX](point.x); this[setY](point.y); }; }, { // Enforce creation of beans, as bean getters have hidden parameters // See _dontLink argument above. beans: true, } ) ); /** * @name LinkedRectangle * * @class An internal version of Rectangle that notifies its owner of each * change through setting itself again on the setter that corresponds to the * getter that produced this LinkedRectangle. * * @private */ export const LinkedRectangle = Rectangle.extend( { // Have LinkedRectangle appear as a normal Rectangle in debugging initialize: function Rectangle(x, y, width, height, owner, setter) { this._set(x, y, width, height, true); this._owner = owner; this._setter = setter; }, // See Point#_set() for an explanation of #_set(): _set: function (x, y, width, height, _dontNotify) { this._x = x; this._y = y; this._width = width; this._height = height; if (!_dontNotify) this._owner[this._setter](this); return this; }, }, // @ts-expect-error = 'new' expression, whose target lacks a construct signature new (function () { var proto = Rectangle.prototype; return Base.each( ['x', 'y', 'width', 'height'], function (key) { var part = Base.capitalize(key), internal = '_' + key; this['get' + part] = function () { return this[internal]; }; this['set' + part] = function (value) { this[internal] = value; // Check if this setter is called from another one which sets // _dontNotify, as it will notify itself if (!this._dontNotify) this._owner[this._setter](this); }; }, Base.each( [ 'Point', 'Size', 'Center', 'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY', 'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter', ], function (key) { var name = 'set' + key; this[name] = function (/* value */) { // Make sure the above setters of x, y, width, height do not // each notify the owner, as we're going to take care of this // afterwards here, only once per change. this._dontNotify = true; proto[name].apply(this, arguments); this._dontNotify = false; this._owner[this._setter](this); }; }, /** @lends Rectangle# */ { /** * {@grouptitle Item Bounds} * * Specifies whether an item's bounds are to appear as selected. * * Paper.js draws the bounds of items with selected bounds on top of * your project. This is very useful when debugging. * * @bean * @type Boolean * @default false * * @example {@paperscript} * var path = new Path.Circle({ * center: [80, 50], * radius: 40, * selected: true * }); * * path.bounds.selected = true; */ isSelected: function () { return !!(this._owner._selection & /*#=*/ ItemSelection.BOUNDS); }, setSelected: function (selected) { var owner = this._owner; if (owner._changeSelection) { owner._changeSelection(/*#=*/ ItemSelection.BOUNDS, selected); } }, } ) ); })() ); ref.Rectangle = Rectangle; |