UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 6.12 kB
export const _Eps=1e-5;export class Vector{x;y;z;constructor(t,e,i){this.x=t,this.y=e,this.z=i}length(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)}normalize(){const t=this.length();t<=_Eps||(this.x/=t,this.y/=t,this.z/=t)}}export class Point{x;y;constructor(t,e){this.x=t,this.y=e}distanceTo(t){return Math.sqrt(Math.pow(t.x-this.x,2)+Math.pow(t.y-this.y,2))}projectOn(t){return 0===t.x&&0===t.y?new Point(0,0):t.scale((this.x*t.x+this.y*t.y)/(Math.pow(t.x,2)+Math.pow(t.y,2)))}scale(t){return new Point(this.x*t,this.y*t)}toString(){return Math.round(100*this.x)/100+", "+Math.round(100*this.y)/100}}export class CubicBezier{controlPoints;constructor(t,e){this.controlPoints=[t,e]}static parse(t){const e=CubicBezier.KeywordValues,i=t.toLowerCase().replace(/\s+/g,"");if(e.has(i))return CubicBezier.parse(e.get(i));const r=i.match(/^cubic-bezier\(([^,]+),([^,]+),([^,]+),([^,]+)\)$/);if(r){const t=new Point(parseFloat(r[1]),parseFloat(r[2])),e=new Point(parseFloat(r[3]),parseFloat(r[4]));return new CubicBezier(t,e)}return null}evaluateAt(t){function e(t,e,i){return 3*(1-i)*(1-i)*i*t+3*(1-i)*i*i*e+Math.pow(i,3)}const i=e(this.controlPoints[0].x,this.controlPoints[1].x,t),r=e(this.controlPoints[0].y,this.controlPoints[1].y,t);return new Point(i,r)}asCSSText(){const t="cubic-bezier("+this.controlPoints.join(", ")+")",e=CubicBezier.KeywordValues;for(const[i,r]of e)if(t===r&&"linear"!==i)return i;return t}static Regex=/((cubic-bezier\([^)]+\))|\b(linear(?![-\(])|ease-in-out|ease-in|ease-out|ease)\b)|(linear\([^)]+\))/g;static KeywordValues=new Map([["linear","cubic-bezier(0, 0, 1, 1)"],["ease","cubic-bezier(0.25, 0.1, 0.25, 1)"],["ease-in","cubic-bezier(0.42, 0, 1, 1)"],["ease-in-out","cubic-bezier(0.42, 0, 0.58, 1)"],["ease-out","cubic-bezier(0, 0, 0.58, 1)"]])}export const LINEAR_BEZIER=new CubicBezier(new Point(0,0),new Point(1,1));export class EulerAngles{alpha;beta;gamma;constructor(t,e,i){this.alpha=t,this.beta=e,this.gamma=i}static fromDeviceOrientationRotationMatrix(t){let e,i,r;return Math.abs(t.m33)<_Eps?Math.abs(t.m13)<_Eps?(e=Math.atan2(t.m12,t.m11),i=t.m23>0?Math.PI/2:-Math.PI/2,r=0):t.m13>0?(e=Math.atan2(-t.m21,t.m22),i=Math.asin(t.m23),r=-Math.PI/2):(e=Math.atan2(t.m21,-t.m22),i=-Math.asin(t.m23),i+=i>0||Math.abs(i)<_Eps?-Math.PI:Math.PI,r=-Math.PI/2):t.m33>0?(e=Math.atan2(-t.m21,t.m22),i=Math.asin(t.m23),r=Math.atan2(-t.m13,t.m33)):(e=Math.atan2(t.m21,-t.m22),i=-Math.asin(t.m23),i+=i>0||Math.abs(i)<_Eps?-Math.PI:Math.PI,r=Math.atan2(t.m13,-t.m33)),e<-_Eps&&(e+=2*Math.PI),e=Number(radiansToDegrees(e).toFixed(6)),i=Number(radiansToDegrees(i).toFixed(6)),r=Number(radiansToDegrees(r).toFixed(6)),new EulerAngles(e,i,r)}}export const scalarProduct=function(t,e){return t.x*e.x+t.y*e.y+t.z*e.z};export const crossProduct=function(t,e){const i=t.y*e.z-t.z*e.y,r=t.z*e.x-t.x*e.z,n=t.x*e.y-t.y*e.x;return new Vector(i,r,n)};export const subtract=function(t,e){const i=t.x-e.x,r=t.y-e.y,n=t.z-e.z;return new Vector(i,r,n)};export const multiplyVectorByMatrixAndNormalize=function(t,e){const i=t.x*e.m14+t.y*e.m24+t.z*e.m34+e.m44,r=(t.x*e.m11+t.y*e.m21+t.z*e.m31+e.m41)/i,n=(t.x*e.m12+t.y*e.m22+t.z*e.m32+e.m42)/i,s=(t.x*e.m13+t.y*e.m23+t.z*e.m33+e.m43)/i;return new Vector(r,n,s)};export const calculateAngle=function(t,e){const i=t.length(),r=e.length();if(i<=_Eps||r<=_Eps)return 0;const n=scalarProduct(t,e)/i/r;return Math.abs(n)>1?0:radiansToDegrees(Math.acos(n))};export const degreesToRadians=function(t){return t*Math.PI/180};export const degreesToGradians=function(t){return t/9*10};export const degreesToTurns=function(t){return t/360};export const radiansToDegrees=function(t){return 180*t/Math.PI};export const radiansToGradians=function(t){return 200*t/Math.PI};export const radiansToTurns=function(t){return t/(2*Math.PI)};export const gradiansToRadians=function(t){return t*Math.PI/200};export const turnsToRadians=function(t){return 2*t*Math.PI};export const boundsForTransformedPoints=function(t,e,i){i||(i={minX:1/0,maxX:-1/0,minY:1/0,maxY:-1/0}),e.length%3&&console.warn("Invalid size of points array");for(let r=0;r<e.length;r+=3){let n=new Vector(e[r],e[r+1],e[r+2]);n=multiplyVectorByMatrixAndNormalize(n,t),i.minX=Math.min(i.minX,n.x),i.maxX=Math.max(i.maxX,n.x),i.minY=Math.min(i.minY,n.y),i.maxY=Math.max(i.maxY,n.y)}return i};export class Size{width;height;constructor(t,e){this.width=t,this.height=e}clipTo(t){return t?new Size(Math.min(this.width,t.width),Math.min(this.height,t.height)):this}scale(t){return new Size(this.width*t,this.height*t)}isEqual(t){return null!==t&&this.width===t.width&&this.height===t.height}widthToMax(t){return new Size(Math.max(this.width,"number"==typeof t?t:t.width),this.height)}addWidth(t){return new Size(this.width+("number"==typeof t?t:t.width),this.height)}heightToMax(t){return new Size(this.width,Math.max(this.height,"number"==typeof t?t:t.height))}addHeight(t){return new Size(this.width,this.height+("number"==typeof t?t:t.height))}}export class Constraints{minimum;preferred;constructor(t,e){if(this.minimum=t||new Size(0,0),this.preferred=e||this.minimum,this.minimum.width>this.preferred.width||this.minimum.height>this.preferred.height)throw new Error("Minimum size is greater than preferred.")}isEqual(t){return null!==t&&this.minimum.isEqual(t.minimum)&&this.preferred.isEqual(t.preferred)}widthToMax(t){return"number"==typeof t?new Constraints(this.minimum.widthToMax(t),this.preferred.widthToMax(t)):new Constraints(this.minimum.widthToMax(t.minimum),this.preferred.widthToMax(t.preferred))}addWidth(t){return"number"==typeof t?new Constraints(this.minimum.addWidth(t),this.preferred.addWidth(t)):new Constraints(this.minimum.addWidth(t.minimum),this.preferred.addWidth(t.preferred))}heightToMax(t){return"number"==typeof t?new Constraints(this.minimum.heightToMax(t),this.preferred.heightToMax(t)):new Constraints(this.minimum.heightToMax(t.minimum),this.preferred.heightToMax(t.preferred))}addHeight(t){return"number"==typeof t?new Constraints(this.minimum.addHeight(t),this.preferred.addHeight(t)):new Constraints(this.minimum.addHeight(t.minimum),this.preferred.addHeight(t.preferred))}}