@zag-js/rect-utils 0.53.0 → 0.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,2 +1,857 @@
1
- "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __defNormalProp=(obj,key,value)=>key in obj?__defProp(obj,key,{enumerable:true,configurable:true,writable:true,value}):obj[key]=value;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var __publicField=(obj,key,value)=>{__defNormalProp(obj,typeof key!=="symbol"?key+"":key,value);return value};var src_exports={};__export(src_exports,{AffineTransform:()=>AffineTransform,addPoints:()=>addPoints,alignRect:()=>alignRect,clampPoint:()=>clampPoint,clampSize:()=>clampSize,closest:()=>closest,closestSideToPoint:()=>closestSideToPoint,closestSideToRect:()=>closestSideToRect,collisions:()=>collisions,constrainRect:()=>constrainRect,contains:()=>contains,containsPoint:()=>containsPoint,containsRect:()=>containsRect,createPoint:()=>createPoint,createRect:()=>createRect,debugPolygon:()=>debugPolygon,distance:()=>distance,distanceBtwEdges:()=>distanceBtwEdges,distanceFromPoint:()=>distanceFromPoint,distanceFromRect:()=>distanceFromRect,expand:()=>expand,fromRange:()=>fromRange,getElementPolygon:()=>getElementPolygon,getElementRect:()=>getElementRect,getRectCenters:()=>getRectCenters,getRectCorners:()=>getRectCorners,getRectEdges:()=>getRectEdges,getRectFromPoints:()=>getRectFromPoints,getRotationRect:()=>getRotationRect,getViewportRect:()=>getViewportRect,getWindowRect:()=>getWindowRect,inset:()=>inset,intersection:()=>intersection,intersects:()=>intersects,isPoint:()=>isPoint,isPointEqual:()=>isPointEqual,isPointInPolygon:()=>isPointInPolygon,isRect:()=>isRect,isRectEqual:()=>isRectEqual,isSizeEqual:()=>isSizeEqual,isSymmetric:()=>isSymmetric,resizeRect:()=>resizeRect,rotate:()=>rotate,shift:()=>shift,shrink:()=>shrink,subtractPoints:()=>subtractPoints,toRad:()=>toRad,union:()=>union});module.exports=__toCommonJS(src_exports);var AffineTransform=class _AffineTransform{constructor([m00,m01,m02,m10,m11,m12]=[0,0,0,0,0,0]){__publicField(this,"m00");__publicField(this,"m01");__publicField(this,"m02");__publicField(this,"m10");__publicField(this,"m11");__publicField(this,"m12");__publicField(this,"rotate",(...args)=>{return this.prepend(_AffineTransform.rotate(...args))});__publicField(this,"scale",(...args)=>{return this.prepend(_AffineTransform.scale(...args))});__publicField(this,"translate",(...args)=>{return this.prepend(_AffineTransform.translate(...args))});this.m00=m00;this.m01=m01;this.m02=m02;this.m10=m10;this.m11=m11;this.m12=m12}applyTo(point){const{x,y}=point;const{m00,m01,m02,m10,m11,m12}=this;return{x:m00*x+m01*y+m02,y:m10*x+m11*y+m12}}prepend(other){return new _AffineTransform([this.m00*other.m00+this.m01*other.m10,this.m00*other.m01+this.m01*other.m11,this.m00*other.m02+this.m01*other.m12+this.m02,this.m10*other.m00+this.m11*other.m10,this.m10*other.m01+this.m11*other.m11,this.m10*other.m02+this.m11*other.m12+this.m12])}append(other){return new _AffineTransform([other.m00*this.m00+other.m01*this.m10,other.m00*this.m01+other.m01*this.m11,other.m00*this.m02+other.m01*this.m12+other.m02,other.m10*this.m00+other.m11*this.m10,other.m10*this.m01+other.m11*this.m11,other.m10*this.m02+other.m11*this.m12+other.m12])}get determinant(){return this.m00*this.m11-this.m01*this.m10}get isInvertible(){const det=this.determinant;return isFinite(det)&&isFinite(this.m02)&&isFinite(this.m12)&&det!==0}invert(){const det=this.determinant;return new _AffineTransform([this.m11/det,-this.m01/det,(this.m01*this.m12-this.m11*this.m02)/det,-this.m10/det,this.m00/det,(this.m10*this.m02-this.m00*this.m12)/det])}get array(){return[this.m00,this.m01,this.m02,this.m10,this.m11,this.m12,0,0,1]}get float32Array(){return new Float32Array(this.array)}static get identity(){return new _AffineTransform([1,0,0,0,1,0])}static rotate(theta,origin){const rotation=new _AffineTransform([Math.cos(theta),-Math.sin(theta),0,Math.sin(theta),Math.cos(theta),0]);if(origin&&(origin.x!==0||origin.y!==0)){return _AffineTransform.multiply(_AffineTransform.translate(origin.x,origin.y),rotation,_AffineTransform.translate(-origin.x,-origin.y))}return rotation}static scale(sx,sy=sx,origin={x:0,y:0}){const scale=new _AffineTransform([sx,0,0,0,sy,0]);if(origin.x!==0||origin.y!==0){return _AffineTransform.multiply(_AffineTransform.translate(origin.x,origin.y),scale,_AffineTransform.translate(-origin.x,-origin.y))}return scale}static translate(tx,ty){return new _AffineTransform([1,0,tx,0,1,ty])}static multiply(...[first,...rest]){if(!first)return _AffineTransform.identity;return rest.reduce((result,item)=>result.prepend(item),first)}get a(){return this.m00}get b(){return this.m10}get c(){return this.m01}get d(){return this.m11}get tx(){return this.m02}get ty(){return this.m12}get scaleComponents(){return{x:this.a,y:this.d}}get translationComponents(){return{x:this.tx,y:this.ty}}get skewComponents(){return{x:this.c,y:this.b}}toString(){return`matrix(${this.a}, ${this.b}, ${this.c}, ${this.d}, ${this.tx}, ${this.ty})`}};function hAlign(a,ref,h){let x=ref.minX;if(h==="left-inside")x=ref.minX;if(h==="left-outside")x=ref.minX-ref.width;if(h==="right-inside")x=ref.maxX-ref.width;if(h==="right-outside")x=ref.maxX;if(h==="center")x=ref.midX-ref.width/2;return{...a,x}}function vAlign(a,ref,v){let y=ref.minY;if(v==="top-inside")y=ref.minY;if(v==="top-outside")y=ref.minY-a.height;if(v==="bottom-inside")y=ref.maxY-a.height;if(v==="bottom-outside")y=ref.maxY;if(v==="center")y=ref.midY-a.height/2;return{...a,y}}function alignRect(a,ref,options){const{h,v}=options;return vAlign(hAlign(a,ref,h),ref,v)}var clamp=(value,min3,max2)=>Math.min(Math.max(value,min3),max2);var clampPoint=(position,size,boundaryRect)=>{const x=clamp(position.x,boundaryRect.x,boundaryRect.x+boundaryRect.width-size.width);const y=clamp(position.y,boundaryRect.y,boundaryRect.y+boundaryRect.height-size.height);return{x,y}};var defaultMinSize={width:0,height:0};var defaultMaxSize={width:Infinity,height:Infinity};var clampSize=(size,minSize=defaultMinSize,maxSize=defaultMaxSize)=>{return{width:Math.min(Math.max(size.width,minSize.width),maxSize.width),height:Math.min(Math.max(size.height,minSize.height),maxSize.height)}};var createPoint=(x,y)=>({x,y});var subtractPoints=(a,b)=>createPoint(a.x-b.x,a.y-b.y);var addPoints=(a,b)=>createPoint(a.x+b.x,a.y+b.y);function isPoint(v){return Reflect.has(v,"x")&&Reflect.has(v,"y")}function createRect(r){const{x,y,width,height}=r;const midX=x+width/2;const midY=y+height/2;return{x,y,width,height,minX:x,minY:y,maxX:x+width,maxY:y+height,midX,midY,center:createPoint(midX,midY)}}function isRect(v){return Reflect.has(v,"x")&&Reflect.has(v,"y")&&Reflect.has(v,"width")&&Reflect.has(v,"height")}function getRectCenters(v){const top=createPoint(v.midX,v.minY);const right=createPoint(v.maxX,v.midY);const bottom=createPoint(v.midX,v.maxY);const left=createPoint(v.minX,v.midY);return{top,right,bottom,left}}function getRectCorners(v){const top=createPoint(v.minX,v.minY);const right=createPoint(v.maxX,v.minY);const bottom=createPoint(v.maxX,v.maxY);const left=createPoint(v.minX,v.maxY);return{top,right,bottom,left}}function getRectEdges(v){const c=getRectCorners(v);const top=[c.top,c.right];const right=[c.right,c.bottom];const bottom=[c.left,c.bottom];const left=[c.top,c.left];return{top,right,bottom,left}}function intersects(a,b){return a.x<b.maxX&&a.y<b.maxY&&a.maxX>b.x&&a.maxY>b.y}function intersection(a,b){const x=Math.max(a.x,b.x);const y=Math.max(a.y,b.y);const x2=Math.min(a.x+a.width,b.x+b.width);const y2=Math.min(a.y+a.height,b.y+b.height);return createRect({x,y,width:x2-x,height:y2-y})}function collisions(a,b){return{top:a.minY<=b.minY,right:a.maxX>=b.maxX,bottom:a.maxY>=b.maxY,left:a.minX<=b.minX}}function distance(a,b={x:0,y:0}){return Math.sqrt(Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2))}function distanceFromPoint(r,p){let x=0;let y=0;if(p.x<r.x)x=r.x-p.x;else if(p.x>r.maxX)x=p.x-r.maxX;if(p.y<r.y)y=r.y-p.y;else if(p.y>r.maxY)y=p.y-r.maxY;return{x,y,value:distance({x,y})}}function distanceFromRect(a,b){if(intersects(a,b))return{x:0,y:0,value:0};const left=a.x<b.x?a:b;const right=b.x<a.x?a:b;const upper=a.y<b.y?a:b;const lower=b.y<a.y?a:b;let x=left.x===right.x?0:right.x-left.maxX;x=Math.max(0,x);let y=upper.y===lower.y?0:lower.y-upper.maxY;y=Math.max(0,y);return{x,y,value:distance({x,y})}}function distanceBtwEdges(a,b){return{left:b.x-a.x,top:b.y-a.y,right:a.maxX-b.maxX,bottom:a.maxY-b.maxY}}function closest(...pts){return a=>{const ds=pts.map(b=>distance(b,a));const c=Math.min.apply(Math,ds);return pts[ds.indexOf(c)]}}function closestSideToRect(ref,r){if(r.maxX<=ref.minX)return"left";if(r.minX>=ref.maxX)return"right";if(r.maxY<=ref.minY)return"top";if(r.minY>=ref.maxY)return"bottom";return"left"}function closestSideToPoint(ref,p){const{x,y}=p;const dl=x-ref.minX;const dr=ref.maxX-x;const dt=y-ref.minY;const db=ref.maxY-y;let closest2=dl;let side="left";if(dr<closest2){closest2=dr;side="right"}if(dt<closest2){closest2=dt;side="top"}if(db<closest2){side="bottom"}return side}var constrainRect=(rect,boundary)=>{const left=Math.max(boundary.x,Math.min(rect.x,boundary.x+boundary.width-rect.width));const top=Math.max(boundary.y,Math.min(rect.y,boundary.y+boundary.height-rect.height));return{x:left,y:top,width:Math.min(rect.width,boundary.width),height:Math.min(rect.height,boundary.height)}};function containsPoint(r,p){return r.minX<=p.x&&p.x<=r.maxX&&r.minY<=p.y&&p.y<=r.maxY}function containsRect(a,b){return Object.values(getRectCorners(b)).every(c=>containsPoint(a,c))}function contains(r,v){return isRect(v)?containsRect(r,v):containsPoint(r,v)}var isSizeEqual=(a,b)=>{return a.width===b.width&&a.height===b.height};var isPointEqual=(a,b)=>{return a.x===b.x&&a.y===b.y};var isRectEqual=(a,b)=>{return isPointEqual(a,b)&&isSizeEqual(a,b)};var styleCache=new WeakMap;function getCacheComputedStyle(el){if(!styleCache.has(el)){const win=el.ownerDocument.defaultView||window;styleCache.set(el,win.getComputedStyle(el))}return styleCache.get(el)}function getElementRect(el,opts={}){return createRect(getClientRect(el,opts))}function getClientRect(el,opts={}){const{excludeScrollbar=false,excludeBorders=false}=opts;const{x,y,width,height}=el.getBoundingClientRect();const r={x,y,width,height};const style=getCacheComputedStyle(el);const{borderLeftWidth,borderTopWidth,borderRightWidth,borderBottomWidth}=style;const borderXWidth=sum(borderLeftWidth,borderRightWidth);const borderYWidth=sum(borderTopWidth,borderBottomWidth);if(excludeBorders){r.width-=borderXWidth;r.height-=borderYWidth;r.x+=px(borderLeftWidth);r.y+=px(borderTopWidth)}if(excludeScrollbar){const scrollbarWidth=el.offsetWidth-el.clientWidth-borderXWidth;const scrollbarHeight=el.offsetHeight-el.clientHeight-borderYWidth;r.width-=scrollbarWidth;r.height-=scrollbarHeight}return r}var px=v=>parseFloat(v.replace("px",""));var sum=(...vals)=>vals.reduce((sum2,v)=>sum2+(v?px(v):0),0);function getRectFromPoints(...pts){const xs=pts.map(p=>p.x);const ys=pts.map(p=>p.y);const x=Math.min(...xs);const y=Math.min(...ys);const width=Math.max(...xs)-x;const height=Math.max(...ys)-y;return createRect({x,y,width,height})}var{min,max}=Math;function union(...rs){const pMin={x:min(...rs.map(r=>r.minX)),y:min(...rs.map(r=>r.minY))};const pMax={x:max(...rs.map(r=>r.maxX)),y:max(...rs.map(r=>r.maxY))};return getRectFromPoints(pMin,pMax)}function fromRange(range){let rs=[];const rects=Array.from(range.getClientRects());if(rects.length){rs=rs.concat(rects.map(createRect));return union.apply(void 0,rs)}let start=range.startContainer;if(start.nodeType===Node.TEXT_NODE){start=start.parentNode}if(start instanceof HTMLElement){const r=getElementRect(start);rs.push({...r,x:r.maxX,width:0})}return union.apply(void 0,rs)}function toRad(d){return d%360*Math.PI/180}function rotate(a,d,c){const r=toRad(d);const sin=Math.sin(r);const cos=Math.cos(r);const x=a.x-c.x;const y=a.y-c.y;return{x:c.x+x*cos-y*sin,y:c.y+x*sin+y*cos}}function getRotationRect(r,deg){const rr=Object.values(getRectCorners(r)).map(p=>rotate(p,deg,r.center));const xs=rr.map(p=>p.x);const ys=rr.map(p=>p.y);const minX=Math.min(...xs);const minY=Math.min(...ys);const maxX=Math.max(...xs);const maxY=Math.max(...ys);return createRect({x:minX,y:minY,width:maxX-minX,height:maxY-minY})}function getWindowRect(win,opts={}){return createRect(getViewportRect(win,opts))}function getViewportRect(win,opts){const{excludeScrollbar=false}=opts;const{innerWidth,innerHeight,document:doc,visualViewport}=win;const width=visualViewport?.width||innerWidth;const height=visualViewport?.height||innerHeight;const rect={x:0,y:0,width,height};if(excludeScrollbar){const scrollbarWidth=innerWidth-doc.documentElement.clientWidth;const scrollbarHeight=innerHeight-doc.documentElement.clientHeight;rect.width-=scrollbarWidth;rect.height-=scrollbarHeight}return rect}var isSymmetric=v=>"dx"in v||"dy"in v;function inset(r,i){const v=isSymmetric(i)?{left:i.dx,right:i.dx,top:i.dy,bottom:i.dy}:i;const{top=0,right=0,bottom=0,left=0}=v;return createRect({x:r.x+left,y:r.y+top,width:r.width-left-right,height:r.height-top-bottom})}function expand(r,v){const value=typeof v==="number"?{dx:-v,dy:-v}:v;return inset(r,value)}function shrink(r,v){const value=typeof v==="number"?{dx:-v,dy:-v}:v;return inset(r,value)}function shift(r,o){const{x=0,y=0}=o;return createRect({x:r.x+x,y:r.y+y,width:r.width,height:r.height})}function getElementPolygon(rectValue,placement){const rect=createRect(rectValue);const{top,right,left,bottom}=getRectCorners(rect);const[base]=placement.split("-");return{top:[left,top,right,bottom],right:[top,right,bottom,left],bottom:[top,left,bottom,right],left:[right,top,left,bottom]}[base]}function isPointInPolygon(polygon,point){const{x,y}=point;let c=false;for(let i=0,j=polygon.length-1;i<polygon.length;j=i++){const xi=polygon[i].x;const yi=polygon[i].y;const xj=polygon[j].x;const yj=polygon[j].y;if(yi>y!==yj>y&&x<(xj-xi)*(y-yi)/(yj-yi)+xi){c=!c}}return c}function createPolygonElement(){const id="debug-polygon";const existingPolygon=document.getElementById(id);if(existingPolygon){return existingPolygon}const svg=document.createElementNS("http://www.w3.org/2000/svg","svg");Object.assign(svg.style,{top:"0",left:"0",width:"100%",height:"100%",opacity:"0.15",position:"fixed",pointerEvents:"none",fill:"red"});const polygon=document.createElementNS("http://www.w3.org/2000/svg","polygon");polygon.setAttribute("id",id);polygon.setAttribute("points","0,0 0,0");svg.appendChild(polygon);document.body.appendChild(svg);return polygon}function debugPolygon(polygon){const el=createPolygonElement();const points=polygon.map(point=>`${point.x},${point.y}`).join(" ");el.setAttribute("points",points);return()=>{el.remove()}}var compassDirectionMap={n:{x:.5,y:0},ne:{x:1,y:0},e:{x:1,y:.5},se:{x:1,y:1},s:{x:.5,y:1},sw:{x:0,y:1},w:{x:0,y:.5},nw:{x:0,y:0}};var oppositeDirectionMap={n:"s",ne:"sw",e:"w",se:"nw",s:"n",sw:"ne",w:"e",nw:"se"};var{sign,abs,min:min2}=Math;function getRectExtentPoint(rect,direction){const{minX,minY,maxX,maxY,midX,midY}=rect;const x=direction.includes("w")?minX:direction.includes("e")?maxX:midX;const y=direction.includes("n")?minY:direction.includes("s")?maxY:midY;return{x,y}}function getOppositeDirection(direction){return oppositeDirectionMap[direction]}function resizeRect(rect,offset,direction,opts){const{scalingOriginMode,lockAspectRatio}=opts;const extent=getRectExtentPoint(rect,direction);const oppositeDirection=getOppositeDirection(direction);const oppositeExtent=getRectExtentPoint(rect,oppositeDirection);if(scalingOriginMode==="center"){offset={x:offset.x*2,y:offset.y*2}}const newExtent={x:extent.x+offset.x,y:extent.y+offset.y};const multiplier={x:compassDirectionMap[direction].x*2-1,y:compassDirectionMap[direction].y*2-1};const newSize={width:newExtent.x-oppositeExtent.x,height:newExtent.y-oppositeExtent.y};const scaleX=multiplier.x*newSize.width/rect.width;const scaleY=multiplier.y*newSize.height/rect.height;const largestMagnitude=abs(scaleX)>abs(scaleY)?scaleX:scaleY;const scale=lockAspectRatio?{x:largestMagnitude,y:largestMagnitude}:{x:extent.x===oppositeExtent.x?1:scaleX,y:extent.y===oppositeExtent.y?1:scaleY};if(extent.y===oppositeExtent.y){scale.y=abs(scale.y)}else if(sign(scale.y)!==sign(scaleY)){scale.y*=-1}if(extent.x===oppositeExtent.x){scale.x=abs(scale.x)}else if(sign(scale.x)!==sign(scaleX)){scale.x*=-1}switch(scalingOriginMode){case"extent":return transformRect(rect,AffineTransform.scale(scale.x,scale.y,oppositeExtent),false);case"center":return transformRect(rect,AffineTransform.scale(scale.x,scale.y,{x:rect.midX,y:rect.midY}),false)}}function createRectFromPoints(initialPoint,finalPoint,normalized=true){if(normalized){return{x:min2(finalPoint.x,initialPoint.x),y:min2(finalPoint.y,initialPoint.y),width:abs(finalPoint.x-initialPoint.x),height:abs(finalPoint.y-initialPoint.y)}}return{x:initialPoint.x,y:initialPoint.y,width:finalPoint.x-initialPoint.x,height:finalPoint.y-initialPoint.y}}function transformRect(rect,transform,normalized=true){const p1=transform.applyTo({x:rect.minX,y:rect.minY});const p2=transform.applyTo({x:rect.maxX,y:rect.maxY});return createRectFromPoints(p1,p2,normalized)}0&&(module.exports={AffineTransform,addPoints,alignRect,clampPoint,clampSize,closest,closestSideToPoint,closestSideToRect,collisions,constrainRect,contains,containsPoint,containsRect,createPoint,createRect,debugPolygon,distance,distanceBtwEdges,distanceFromPoint,distanceFromRect,expand,fromRange,getElementPolygon,getElementRect,getRectCenters,getRectCorners,getRectEdges,getRectFromPoints,getRotationRect,getViewportRect,getWindowRect,inset,intersection,intersects,isPoint,isPointEqual,isPointInPolygon,isRect,isRectEqual,isSizeEqual,isSymmetric,resizeRect,rotate,shift,shrink,subtractPoints,toRad,union});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
24
+
25
+ // src/index.ts
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ AffineTransform: () => AffineTransform,
29
+ addPoints: () => addPoints,
30
+ alignRect: () => alignRect,
31
+ clampPoint: () => clampPoint,
32
+ clampSize: () => clampSize,
33
+ closest: () => closest,
34
+ closestSideToPoint: () => closestSideToPoint,
35
+ closestSideToRect: () => closestSideToRect,
36
+ collisions: () => collisions,
37
+ constrainRect: () => constrainRect,
38
+ contains: () => contains,
39
+ containsPoint: () => containsPoint,
40
+ containsRect: () => containsRect,
41
+ createPoint: () => createPoint,
42
+ createRect: () => createRect,
43
+ debugPolygon: () => debugPolygon,
44
+ distance: () => distance,
45
+ distanceBtwEdges: () => distanceBtwEdges,
46
+ distanceFromPoint: () => distanceFromPoint,
47
+ distanceFromRect: () => distanceFromRect,
48
+ expand: () => expand,
49
+ fromRange: () => fromRange,
50
+ getElementPolygon: () => getElementPolygon,
51
+ getElementRect: () => getElementRect,
52
+ getRectCenters: () => getRectCenters,
53
+ getRectCorners: () => getRectCorners,
54
+ getRectEdges: () => getRectEdges,
55
+ getRectFromPoints: () => getRectFromPoints,
56
+ getRotationRect: () => getRotationRect,
57
+ getViewportRect: () => getViewportRect,
58
+ getWindowRect: () => getWindowRect,
59
+ inset: () => inset,
60
+ intersection: () => intersection,
61
+ intersects: () => intersects,
62
+ isPoint: () => isPoint,
63
+ isPointEqual: () => isPointEqual,
64
+ isPointInPolygon: () => isPointInPolygon,
65
+ isRect: () => isRect,
66
+ isRectEqual: () => isRectEqual,
67
+ isSizeEqual: () => isSizeEqual,
68
+ isSymmetric: () => isSymmetric,
69
+ resizeRect: () => resizeRect,
70
+ rotate: () => rotate,
71
+ shift: () => shift,
72
+ shrink: () => shrink,
73
+ subtractPoints: () => subtractPoints,
74
+ toRad: () => toRad,
75
+ union: () => union
76
+ });
77
+ module.exports = __toCommonJS(src_exports);
78
+
79
+ // src/affine-transform.ts
80
+ var AffineTransform = class _AffineTransform {
81
+ constructor([m00, m01, m02, m10, m11, m12] = [0, 0, 0, 0, 0, 0]) {
82
+ __publicField(this, "m00");
83
+ __publicField(this, "m01");
84
+ __publicField(this, "m02");
85
+ __publicField(this, "m10");
86
+ __publicField(this, "m11");
87
+ __publicField(this, "m12");
88
+ __publicField(this, "rotate", (...args) => {
89
+ return this.prepend(_AffineTransform.rotate(...args));
90
+ });
91
+ __publicField(this, "scale", (...args) => {
92
+ return this.prepend(_AffineTransform.scale(...args));
93
+ });
94
+ __publicField(this, "translate", (...args) => {
95
+ return this.prepend(_AffineTransform.translate(...args));
96
+ });
97
+ this.m00 = m00;
98
+ this.m01 = m01;
99
+ this.m02 = m02;
100
+ this.m10 = m10;
101
+ this.m11 = m11;
102
+ this.m12 = m12;
103
+ }
104
+ applyTo(point) {
105
+ const { x, y } = point;
106
+ const { m00, m01, m02, m10, m11, m12 } = this;
107
+ return {
108
+ x: m00 * x + m01 * y + m02,
109
+ y: m10 * x + m11 * y + m12
110
+ };
111
+ }
112
+ prepend(other) {
113
+ return new _AffineTransform([
114
+ this.m00 * other.m00 + this.m01 * other.m10,
115
+ // m00
116
+ this.m00 * other.m01 + this.m01 * other.m11,
117
+ // m01
118
+ this.m00 * other.m02 + this.m01 * other.m12 + this.m02,
119
+ // m02
120
+ this.m10 * other.m00 + this.m11 * other.m10,
121
+ // m10
122
+ this.m10 * other.m01 + this.m11 * other.m11,
123
+ // m11
124
+ this.m10 * other.m02 + this.m11 * other.m12 + this.m12
125
+ // m12
126
+ ]);
127
+ }
128
+ append(other) {
129
+ return new _AffineTransform([
130
+ other.m00 * this.m00 + other.m01 * this.m10,
131
+ // m00
132
+ other.m00 * this.m01 + other.m01 * this.m11,
133
+ // m01
134
+ other.m00 * this.m02 + other.m01 * this.m12 + other.m02,
135
+ // m02
136
+ other.m10 * this.m00 + other.m11 * this.m10,
137
+ // m10
138
+ other.m10 * this.m01 + other.m11 * this.m11,
139
+ // m11
140
+ other.m10 * this.m02 + other.m11 * this.m12 + other.m12
141
+ // m12
142
+ ]);
143
+ }
144
+ get determinant() {
145
+ return this.m00 * this.m11 - this.m01 * this.m10;
146
+ }
147
+ get isInvertible() {
148
+ const det = this.determinant;
149
+ return isFinite(det) && isFinite(this.m02) && isFinite(this.m12) && det !== 0;
150
+ }
151
+ invert() {
152
+ const det = this.determinant;
153
+ return new _AffineTransform([
154
+ this.m11 / det,
155
+ // m00
156
+ -this.m01 / det,
157
+ // m01
158
+ (this.m01 * this.m12 - this.m11 * this.m02) / det,
159
+ // m02
160
+ -this.m10 / det,
161
+ // m10
162
+ this.m00 / det,
163
+ // m11
164
+ (this.m10 * this.m02 - this.m00 * this.m12) / det
165
+ // m12
166
+ ]);
167
+ }
168
+ get array() {
169
+ return [this.m00, this.m01, this.m02, this.m10, this.m11, this.m12, 0, 0, 1];
170
+ }
171
+ get float32Array() {
172
+ return new Float32Array(this.array);
173
+ }
174
+ // Static
175
+ static get identity() {
176
+ return new _AffineTransform([1, 0, 0, 0, 1, 0]);
177
+ }
178
+ static rotate(theta, origin) {
179
+ const rotation = new _AffineTransform([Math.cos(theta), -Math.sin(theta), 0, Math.sin(theta), Math.cos(theta), 0]);
180
+ if (origin && (origin.x !== 0 || origin.y !== 0)) {
181
+ return _AffineTransform.multiply(
182
+ _AffineTransform.translate(origin.x, origin.y),
183
+ rotation,
184
+ _AffineTransform.translate(-origin.x, -origin.y)
185
+ );
186
+ }
187
+ return rotation;
188
+ }
189
+ static scale(sx, sy = sx, origin = { x: 0, y: 0 }) {
190
+ const scale = new _AffineTransform([sx, 0, 0, 0, sy, 0]);
191
+ if (origin.x !== 0 || origin.y !== 0) {
192
+ return _AffineTransform.multiply(
193
+ _AffineTransform.translate(origin.x, origin.y),
194
+ scale,
195
+ _AffineTransform.translate(-origin.x, -origin.y)
196
+ );
197
+ }
198
+ return scale;
199
+ }
200
+ static translate(tx, ty) {
201
+ return new _AffineTransform([1, 0, tx, 0, 1, ty]);
202
+ }
203
+ static multiply(...[first, ...rest]) {
204
+ if (!first)
205
+ return _AffineTransform.identity;
206
+ return rest.reduce((result, item) => result.prepend(item), first);
207
+ }
208
+ get a() {
209
+ return this.m00;
210
+ }
211
+ get b() {
212
+ return this.m10;
213
+ }
214
+ get c() {
215
+ return this.m01;
216
+ }
217
+ get d() {
218
+ return this.m11;
219
+ }
220
+ get tx() {
221
+ return this.m02;
222
+ }
223
+ get ty() {
224
+ return this.m12;
225
+ }
226
+ get scaleComponents() {
227
+ return { x: this.a, y: this.d };
228
+ }
229
+ get translationComponents() {
230
+ return { x: this.tx, y: this.ty };
231
+ }
232
+ get skewComponents() {
233
+ return { x: this.c, y: this.b };
234
+ }
235
+ toString() {
236
+ return `matrix(${this.a}, ${this.b}, ${this.c}, ${this.d}, ${this.tx}, ${this.ty})`;
237
+ }
238
+ };
239
+
240
+ // src/align.ts
241
+ function hAlign(a, ref, h) {
242
+ let x = ref.minX;
243
+ if (h === "left-inside")
244
+ x = ref.minX;
245
+ if (h === "left-outside")
246
+ x = ref.minX - ref.width;
247
+ if (h === "right-inside")
248
+ x = ref.maxX - ref.width;
249
+ if (h === "right-outside")
250
+ x = ref.maxX;
251
+ if (h === "center")
252
+ x = ref.midX - ref.width / 2;
253
+ return { ...a, x };
254
+ }
255
+ function vAlign(a, ref, v) {
256
+ let y = ref.minY;
257
+ if (v === "top-inside")
258
+ y = ref.minY;
259
+ if (v === "top-outside")
260
+ y = ref.minY - a.height;
261
+ if (v === "bottom-inside")
262
+ y = ref.maxY - a.height;
263
+ if (v === "bottom-outside")
264
+ y = ref.maxY;
265
+ if (v === "center")
266
+ y = ref.midY - a.height / 2;
267
+ return { ...a, y };
268
+ }
269
+ function alignRect(a, ref, options) {
270
+ const { h, v } = options;
271
+ return vAlign(hAlign(a, ref, h), ref, v);
272
+ }
273
+
274
+ // src/clamp.ts
275
+ var clamp = (value, min3, max2) => Math.min(Math.max(value, min3), max2);
276
+ var clampPoint = (position, size, boundaryRect) => {
277
+ const x = clamp(position.x, boundaryRect.x, boundaryRect.x + boundaryRect.width - size.width);
278
+ const y = clamp(position.y, boundaryRect.y, boundaryRect.y + boundaryRect.height - size.height);
279
+ return { x, y };
280
+ };
281
+ var defaultMinSize = {
282
+ width: 0,
283
+ height: 0
284
+ };
285
+ var defaultMaxSize = {
286
+ width: Infinity,
287
+ height: Infinity
288
+ };
289
+ var clampSize = (size, minSize = defaultMinSize, maxSize = defaultMaxSize) => {
290
+ return {
291
+ width: Math.min(Math.max(size.width, minSize.width), maxSize.width),
292
+ height: Math.min(Math.max(size.height, minSize.height), maxSize.height)
293
+ };
294
+ };
295
+
296
+ // src/rect.ts
297
+ var createPoint = (x, y) => ({ x, y });
298
+ var subtractPoints = (a, b) => createPoint(a.x - b.x, a.y - b.y);
299
+ var addPoints = (a, b) => createPoint(a.x + b.x, a.y + b.y);
300
+ function isPoint(v) {
301
+ return Reflect.has(v, "x") && Reflect.has(v, "y");
302
+ }
303
+ function createRect(r) {
304
+ const { x, y, width, height } = r;
305
+ const midX = x + width / 2;
306
+ const midY = y + height / 2;
307
+ return {
308
+ x,
309
+ y,
310
+ width,
311
+ height,
312
+ minX: x,
313
+ minY: y,
314
+ maxX: x + width,
315
+ maxY: y + height,
316
+ midX,
317
+ midY,
318
+ center: createPoint(midX, midY)
319
+ };
320
+ }
321
+ function isRect(v) {
322
+ return Reflect.has(v, "x") && Reflect.has(v, "y") && Reflect.has(v, "width") && Reflect.has(v, "height");
323
+ }
324
+ function getRectCenters(v) {
325
+ const top = createPoint(v.midX, v.minY);
326
+ const right = createPoint(v.maxX, v.midY);
327
+ const bottom = createPoint(v.midX, v.maxY);
328
+ const left = createPoint(v.minX, v.midY);
329
+ return { top, right, bottom, left };
330
+ }
331
+ function getRectCorners(v) {
332
+ const top = createPoint(v.minX, v.minY);
333
+ const right = createPoint(v.maxX, v.minY);
334
+ const bottom = createPoint(v.maxX, v.maxY);
335
+ const left = createPoint(v.minX, v.maxY);
336
+ return { top, right, bottom, left };
337
+ }
338
+ function getRectEdges(v) {
339
+ const c = getRectCorners(v);
340
+ const top = [c.top, c.right];
341
+ const right = [c.right, c.bottom];
342
+ const bottom = [c.left, c.bottom];
343
+ const left = [c.top, c.left];
344
+ return { top, right, bottom, left };
345
+ }
346
+
347
+ // src/intersection.ts
348
+ function intersects(a, b) {
349
+ return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
350
+ }
351
+ function intersection(a, b) {
352
+ const x = Math.max(a.x, b.x);
353
+ const y = Math.max(a.y, b.y);
354
+ const x2 = Math.min(a.x + a.width, b.x + b.width);
355
+ const y2 = Math.min(a.y + a.height, b.y + b.height);
356
+ return createRect({ x, y, width: x2 - x, height: y2 - y });
357
+ }
358
+ function collisions(a, b) {
359
+ return {
360
+ top: a.minY <= b.minY,
361
+ right: a.maxX >= b.maxX,
362
+ bottom: a.maxY >= b.maxY,
363
+ left: a.minX <= b.minX
364
+ };
365
+ }
366
+
367
+ // src/distance.ts
368
+ function distance(a, b = { x: 0, y: 0 }) {
369
+ return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
370
+ }
371
+ function distanceFromPoint(r, p) {
372
+ let x = 0;
373
+ let y = 0;
374
+ if (p.x < r.x)
375
+ x = r.x - p.x;
376
+ else if (p.x > r.maxX)
377
+ x = p.x - r.maxX;
378
+ if (p.y < r.y)
379
+ y = r.y - p.y;
380
+ else if (p.y > r.maxY)
381
+ y = p.y - r.maxY;
382
+ return { x, y, value: distance({ x, y }) };
383
+ }
384
+ function distanceFromRect(a, b) {
385
+ if (intersects(a, b))
386
+ return { x: 0, y: 0, value: 0 };
387
+ const left = a.x < b.x ? a : b;
388
+ const right = b.x < a.x ? a : b;
389
+ const upper = a.y < b.y ? a : b;
390
+ const lower = b.y < a.y ? a : b;
391
+ let x = left.x === right.x ? 0 : right.x - left.maxX;
392
+ x = Math.max(0, x);
393
+ let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
394
+ y = Math.max(0, y);
395
+ return { x, y, value: distance({ x, y }) };
396
+ }
397
+ function distanceBtwEdges(a, b) {
398
+ return {
399
+ left: b.x - a.x,
400
+ top: b.y - a.y,
401
+ right: a.maxX - b.maxX,
402
+ bottom: a.maxY - b.maxY
403
+ };
404
+ }
405
+
406
+ // src/closest.ts
407
+ function closest(...pts) {
408
+ return (a) => {
409
+ const ds = pts.map((b) => distance(b, a));
410
+ const c = Math.min.apply(Math, ds);
411
+ return pts[ds.indexOf(c)];
412
+ };
413
+ }
414
+ function closestSideToRect(ref, r) {
415
+ if (r.maxX <= ref.minX)
416
+ return "left";
417
+ if (r.minX >= ref.maxX)
418
+ return "right";
419
+ if (r.maxY <= ref.minY)
420
+ return "top";
421
+ if (r.minY >= ref.maxY)
422
+ return "bottom";
423
+ return "left";
424
+ }
425
+ function closestSideToPoint(ref, p) {
426
+ const { x, y } = p;
427
+ const dl = x - ref.minX;
428
+ const dr = ref.maxX - x;
429
+ const dt = y - ref.minY;
430
+ const db = ref.maxY - y;
431
+ let closest2 = dl;
432
+ let side = "left";
433
+ if (dr < closest2) {
434
+ closest2 = dr;
435
+ side = "right";
436
+ }
437
+ if (dt < closest2) {
438
+ closest2 = dt;
439
+ side = "top";
440
+ }
441
+ if (db < closest2) {
442
+ side = "bottom";
443
+ }
444
+ return side;
445
+ }
446
+
447
+ // src/constrain.ts
448
+ var constrainRect = (rect, boundary) => {
449
+ const left = Math.max(boundary.x, Math.min(rect.x, boundary.x + boundary.width - rect.width));
450
+ const top = Math.max(boundary.y, Math.min(rect.y, boundary.y + boundary.height - rect.height));
451
+ return {
452
+ x: left,
453
+ y: top,
454
+ width: Math.min(rect.width, boundary.width),
455
+ height: Math.min(rect.height, boundary.height)
456
+ };
457
+ };
458
+
459
+ // src/contains.ts
460
+ function containsPoint(r, p) {
461
+ return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
462
+ }
463
+ function containsRect(a, b) {
464
+ return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
465
+ }
466
+ function contains(r, v) {
467
+ return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
468
+ }
469
+
470
+ // src/equality.ts
471
+ var isSizeEqual = (a, b) => {
472
+ return a.width === b.width && a.height === b.height;
473
+ };
474
+ var isPointEqual = (a, b) => {
475
+ return a.x === b.x && a.y === b.y;
476
+ };
477
+ var isRectEqual = (a, b) => {
478
+ return isPointEqual(a, b) && isSizeEqual(a, b);
479
+ };
480
+
481
+ // src/from-element.ts
482
+ var styleCache = /* @__PURE__ */ new WeakMap();
483
+ function getCacheComputedStyle(el) {
484
+ if (!styleCache.has(el)) {
485
+ const win = el.ownerDocument.defaultView || window;
486
+ styleCache.set(el, win.getComputedStyle(el));
487
+ }
488
+ return styleCache.get(el);
489
+ }
490
+ function getElementRect(el, opts = {}) {
491
+ return createRect(getClientRect(el, opts));
492
+ }
493
+ function getClientRect(el, opts = {}) {
494
+ const { excludeScrollbar = false, excludeBorders = false } = opts;
495
+ const { x, y, width, height } = el.getBoundingClientRect();
496
+ const r = { x, y, width, height };
497
+ const style = getCacheComputedStyle(el);
498
+ const { borderLeftWidth, borderTopWidth, borderRightWidth, borderBottomWidth } = style;
499
+ const borderXWidth = sum(borderLeftWidth, borderRightWidth);
500
+ const borderYWidth = sum(borderTopWidth, borderBottomWidth);
501
+ if (excludeBorders) {
502
+ r.width -= borderXWidth;
503
+ r.height -= borderYWidth;
504
+ r.x += px(borderLeftWidth);
505
+ r.y += px(borderTopWidth);
506
+ }
507
+ if (excludeScrollbar) {
508
+ const scrollbarWidth = el.offsetWidth - el.clientWidth - borderXWidth;
509
+ const scrollbarHeight = el.offsetHeight - el.clientHeight - borderYWidth;
510
+ r.width -= scrollbarWidth;
511
+ r.height -= scrollbarHeight;
512
+ }
513
+ return r;
514
+ }
515
+ var px = (v) => parseFloat(v.replace("px", ""));
516
+ var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
517
+
518
+ // src/from-points.ts
519
+ function getRectFromPoints(...pts) {
520
+ const xs = pts.map((p) => p.x);
521
+ const ys = pts.map((p) => p.y);
522
+ const x = Math.min(...xs);
523
+ const y = Math.min(...ys);
524
+ const width = Math.max(...xs) - x;
525
+ const height = Math.max(...ys) - y;
526
+ return createRect({ x, y, width, height });
527
+ }
528
+
529
+ // src/union.ts
530
+ var { min, max } = Math;
531
+ function union(...rs) {
532
+ const pMin = {
533
+ x: min(...rs.map((r) => r.minX)),
534
+ y: min(...rs.map((r) => r.minY))
535
+ };
536
+ const pMax = {
537
+ x: max(...rs.map((r) => r.maxX)),
538
+ y: max(...rs.map((r) => r.maxY))
539
+ };
540
+ return getRectFromPoints(pMin, pMax);
541
+ }
542
+
543
+ // src/from-range.ts
544
+ function fromRange(range) {
545
+ let rs = [];
546
+ const rects = Array.from(range.getClientRects());
547
+ if (rects.length) {
548
+ rs = rs.concat(rects.map(createRect));
549
+ return union.apply(void 0, rs);
550
+ }
551
+ let start = range.startContainer;
552
+ if (start.nodeType === Node.TEXT_NODE) {
553
+ start = start.parentNode;
554
+ }
555
+ if (start instanceof HTMLElement) {
556
+ const r = getElementRect(start);
557
+ rs.push({ ...r, x: r.maxX, width: 0 });
558
+ }
559
+ return union.apply(void 0, rs);
560
+ }
561
+
562
+ // src/from-rotation.ts
563
+ function toRad(d) {
564
+ return d % 360 * Math.PI / 180;
565
+ }
566
+ function rotate(a, d, c) {
567
+ const r = toRad(d);
568
+ const sin = Math.sin(r);
569
+ const cos = Math.cos(r);
570
+ const x = a.x - c.x;
571
+ const y = a.y - c.y;
572
+ return {
573
+ x: c.x + x * cos - y * sin,
574
+ y: c.y + x * sin + y * cos
575
+ };
576
+ }
577
+ function getRotationRect(r, deg) {
578
+ const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
579
+ const xs = rr.map((p) => p.x);
580
+ const ys = rr.map((p) => p.y);
581
+ const minX = Math.min(...xs);
582
+ const minY = Math.min(...ys);
583
+ const maxX = Math.max(...xs);
584
+ const maxY = Math.max(...ys);
585
+ return createRect({
586
+ x: minX,
587
+ y: minY,
588
+ width: maxX - minX,
589
+ height: maxY - minY
590
+ });
591
+ }
592
+
593
+ // src/from-window.ts
594
+ function getWindowRect(win, opts = {}) {
595
+ return createRect(getViewportRect(win, opts));
596
+ }
597
+ function getViewportRect(win, opts) {
598
+ const { excludeScrollbar = false } = opts;
599
+ const { innerWidth, innerHeight, document: doc, visualViewport } = win;
600
+ const width = visualViewport?.width || innerWidth;
601
+ const height = visualViewport?.height || innerHeight;
602
+ const rect = { x: 0, y: 0, width, height };
603
+ if (excludeScrollbar) {
604
+ const scrollbarWidth = innerWidth - doc.documentElement.clientWidth;
605
+ const scrollbarHeight = innerHeight - doc.documentElement.clientHeight;
606
+ rect.width -= scrollbarWidth;
607
+ rect.height -= scrollbarHeight;
608
+ }
609
+ return rect;
610
+ }
611
+
612
+ // src/operations.ts
613
+ var isSymmetric = (v) => "dx" in v || "dy" in v;
614
+ function inset(r, i) {
615
+ const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
616
+ const { top = 0, right = 0, bottom = 0, left = 0 } = v;
617
+ return createRect({
618
+ x: r.x + left,
619
+ y: r.y + top,
620
+ width: r.width - left - right,
621
+ height: r.height - top - bottom
622
+ });
623
+ }
624
+ function expand(r, v) {
625
+ const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
626
+ return inset(r, value);
627
+ }
628
+ function shrink(r, v) {
629
+ const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
630
+ return inset(r, value);
631
+ }
632
+ function shift(r, o) {
633
+ const { x = 0, y = 0 } = o;
634
+ return createRect({
635
+ x: r.x + x,
636
+ y: r.y + y,
637
+ width: r.width,
638
+ height: r.height
639
+ });
640
+ }
641
+
642
+ // src/polygon.ts
643
+ function getElementPolygon(rectValue, placement) {
644
+ const rect = createRect(rectValue);
645
+ const { top, right, left, bottom } = getRectCorners(rect);
646
+ const [base] = placement.split("-");
647
+ return {
648
+ top: [left, top, right, bottom],
649
+ right: [top, right, bottom, left],
650
+ bottom: [top, left, bottom, right],
651
+ left: [right, top, left, bottom]
652
+ }[base];
653
+ }
654
+ function isPointInPolygon(polygon, point) {
655
+ const { x, y } = point;
656
+ let c = false;
657
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
658
+ const xi = polygon[i].x;
659
+ const yi = polygon[i].y;
660
+ const xj = polygon[j].x;
661
+ const yj = polygon[j].y;
662
+ if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
663
+ c = !c;
664
+ }
665
+ }
666
+ return c;
667
+ }
668
+ function createPolygonElement() {
669
+ const id = "debug-polygon";
670
+ const existingPolygon = document.getElementById(id);
671
+ if (existingPolygon) {
672
+ return existingPolygon;
673
+ }
674
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
675
+ Object.assign(svg.style, {
676
+ top: "0",
677
+ left: "0",
678
+ width: "100%",
679
+ height: "100%",
680
+ opacity: "0.15",
681
+ position: "fixed",
682
+ pointerEvents: "none",
683
+ fill: "red"
684
+ });
685
+ const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
686
+ polygon.setAttribute("id", id);
687
+ polygon.setAttribute("points", "0,0 0,0");
688
+ svg.appendChild(polygon);
689
+ document.body.appendChild(svg);
690
+ return polygon;
691
+ }
692
+ function debugPolygon(polygon) {
693
+ const el = createPolygonElement();
694
+ const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
695
+ el.setAttribute("points", points);
696
+ return () => {
697
+ el.remove();
698
+ };
699
+ }
700
+
701
+ // src/compass.ts
702
+ var compassDirectionMap = {
703
+ n: { x: 0.5, y: 0 },
704
+ ne: { x: 1, y: 0 },
705
+ e: { x: 1, y: 0.5 },
706
+ se: { x: 1, y: 1 },
707
+ s: { x: 0.5, y: 1 },
708
+ sw: { x: 0, y: 1 },
709
+ w: { x: 0, y: 0.5 },
710
+ nw: { x: 0, y: 0 }
711
+ };
712
+ var oppositeDirectionMap = {
713
+ n: "s",
714
+ ne: "sw",
715
+ e: "w",
716
+ se: "nw",
717
+ s: "n",
718
+ sw: "ne",
719
+ w: "e",
720
+ nw: "se"
721
+ };
722
+
723
+ // src/resize.ts
724
+ var { sign, abs, min: min2 } = Math;
725
+ function getRectExtentPoint(rect, direction) {
726
+ const { minX, minY, maxX, maxY, midX, midY } = rect;
727
+ const x = direction.includes("w") ? minX : direction.includes("e") ? maxX : midX;
728
+ const y = direction.includes("n") ? minY : direction.includes("s") ? maxY : midY;
729
+ return { x, y };
730
+ }
731
+ function getOppositeDirection(direction) {
732
+ return oppositeDirectionMap[direction];
733
+ }
734
+ function resizeRect(rect, offset, direction, opts) {
735
+ const { scalingOriginMode, lockAspectRatio } = opts;
736
+ const extent = getRectExtentPoint(rect, direction);
737
+ const oppositeDirection = getOppositeDirection(direction);
738
+ const oppositeExtent = getRectExtentPoint(rect, oppositeDirection);
739
+ if (scalingOriginMode === "center") {
740
+ offset = { x: offset.x * 2, y: offset.y * 2 };
741
+ }
742
+ const newExtent = {
743
+ x: extent.x + offset.x,
744
+ y: extent.y + offset.y
745
+ };
746
+ const multiplier = {
747
+ x: compassDirectionMap[direction].x * 2 - 1,
748
+ y: compassDirectionMap[direction].y * 2 - 1
749
+ };
750
+ const newSize = {
751
+ width: newExtent.x - oppositeExtent.x,
752
+ height: newExtent.y - oppositeExtent.y
753
+ };
754
+ const scaleX = multiplier.x * newSize.width / rect.width;
755
+ const scaleY = multiplier.y * newSize.height / rect.height;
756
+ const largestMagnitude = abs(scaleX) > abs(scaleY) ? scaleX : scaleY;
757
+ const scale = lockAspectRatio ? { x: largestMagnitude, y: largestMagnitude } : {
758
+ x: extent.x === oppositeExtent.x ? 1 : scaleX,
759
+ y: extent.y === oppositeExtent.y ? 1 : scaleY
760
+ };
761
+ if (extent.y === oppositeExtent.y) {
762
+ scale.y = abs(scale.y);
763
+ } else if (sign(scale.y) !== sign(scaleY)) {
764
+ scale.y *= -1;
765
+ }
766
+ if (extent.x === oppositeExtent.x) {
767
+ scale.x = abs(scale.x);
768
+ } else if (sign(scale.x) !== sign(scaleX)) {
769
+ scale.x *= -1;
770
+ }
771
+ switch (scalingOriginMode) {
772
+ case "extent":
773
+ return transformRect(rect, AffineTransform.scale(scale.x, scale.y, oppositeExtent), false);
774
+ case "center":
775
+ return transformRect(
776
+ rect,
777
+ AffineTransform.scale(scale.x, scale.y, {
778
+ x: rect.midX,
779
+ y: rect.midY
780
+ }),
781
+ false
782
+ );
783
+ }
784
+ }
785
+ function createRectFromPoints(initialPoint, finalPoint, normalized = true) {
786
+ if (normalized) {
787
+ return {
788
+ x: min2(finalPoint.x, initialPoint.x),
789
+ y: min2(finalPoint.y, initialPoint.y),
790
+ width: abs(finalPoint.x - initialPoint.x),
791
+ height: abs(finalPoint.y - initialPoint.y)
792
+ };
793
+ }
794
+ return {
795
+ x: initialPoint.x,
796
+ y: initialPoint.y,
797
+ width: finalPoint.x - initialPoint.x,
798
+ height: finalPoint.y - initialPoint.y
799
+ };
800
+ }
801
+ function transformRect(rect, transform, normalized = true) {
802
+ const p1 = transform.applyTo({ x: rect.minX, y: rect.minY });
803
+ const p2 = transform.applyTo({ x: rect.maxX, y: rect.maxY });
804
+ return createRectFromPoints(p1, p2, normalized);
805
+ }
806
+ // Annotate the CommonJS export names for ESM import in node:
807
+ 0 && (module.exports = {
808
+ AffineTransform,
809
+ addPoints,
810
+ alignRect,
811
+ clampPoint,
812
+ clampSize,
813
+ closest,
814
+ closestSideToPoint,
815
+ closestSideToRect,
816
+ collisions,
817
+ constrainRect,
818
+ contains,
819
+ containsPoint,
820
+ containsRect,
821
+ createPoint,
822
+ createRect,
823
+ debugPolygon,
824
+ distance,
825
+ distanceBtwEdges,
826
+ distanceFromPoint,
827
+ distanceFromRect,
828
+ expand,
829
+ fromRange,
830
+ getElementPolygon,
831
+ getElementRect,
832
+ getRectCenters,
833
+ getRectCorners,
834
+ getRectEdges,
835
+ getRectFromPoints,
836
+ getRotationRect,
837
+ getViewportRect,
838
+ getWindowRect,
839
+ inset,
840
+ intersection,
841
+ intersects,
842
+ isPoint,
843
+ isPointEqual,
844
+ isPointInPolygon,
845
+ isRect,
846
+ isRectEqual,
847
+ isSizeEqual,
848
+ isSymmetric,
849
+ resizeRect,
850
+ rotate,
851
+ shift,
852
+ shrink,
853
+ subtractPoints,
854
+ toRad,
855
+ union
856
+ });
2
857
  //# sourceMappingURL=index.js.map