Skip to content

Instantly share code, notes, and snippets.

@uxder
Last active May 23, 2019 03:49
Show Gist options
  • Save uxder/96c254d7486b6589ac1595dd5812b652 to your computer and use it in GitHub Desktop.
Save uxder/96c254d7486b6589ac1595dd5812b652 to your computer and use it in GitHub Desktop.
Mathf Temp
const mathf = {
getRandom : (min, max) => {
return Math.random() * (max - min) + min;
},
getRandomInt : (min, max)=>{
return Math.floor(Math.random() * (max - min + 1)) + min;
},
flipCoin: () => {
return getRandomInt(0, 1);
},
fixDigits: (value, digits)=> {
return +parseFloat(value).toFixed(digits);
},
normalize: (val, min, max)=> {
return (val - min) / (max - min);
},
/**
* Given, value: 5, min: 3, max: 10
* returns the percentage of value.
*/
normalizeValueToPercent: (val, min, max) => {
return (val - min) / (max - min);
},
/**
* Given, percent: 0.3, min: 3, max: 10
* returns the value at which is 0.3 of values between 3-10.
*/
normalizePercentToValue: (percent, min, max) => {
return ((max - min) * percent) + min
},
normalizeValue: (value, max)=> {
if (value < 0) {
return max + value;
}
if(value >= max) {
return value - max;
}
return value;
},
circumference: (radius)=> {
return 2 * Math.PI * radius;
},
clamp: (value, min, max)=> {
return Math.min(Math.max(value, min), max);
},
/**
* Clamping for degrees.
* For example, clamping a value between
* 15 degrees to 345 degrees.
* If 330 degrees is passed, you want 345.
* If 16 degrees is pass, you want 15.
* In this example, max1 = 15. min2 = 345.
* Normally just leave min1 as 0.
*/
clampAngle: (value, min1=0, max1, min2, max2=360)=> {
let midPoint = (max2 + max1) / 2;
let out;
if(value <= midPoint) {
out = Math.min(Math.max(value, min1), max1);
} else {
out = Math.min(max2, Math.max(value, min2));
}
return out;
},
lerp: (value1, value2, amount)=> {
amount = amount < 0 ? 0 : amount;
amount = amount > 1 ? 1 : amount;
return value1 + (value2 - value1) * amount;
},
convertFrameToDegree(frame, max) {
return frame * (360 / max);
},
convertDegreeToFrame(degree, max) {
return degree * max / 360;
},
/**
* Determine the short distance between two angles.
* angles should be in radians.
* angle0 - angle in radians
* angle1 - angle in radians
* max - in radians. Typically this would be 2 radian (360).
* @return distance in radians
*/
angleDistance(angle0 , angle1, max) {
if(!max) {
max = Math.PI*2;
}
let delta = (angle1 - angle0) % max;
return 2 * delta % max - delta;
},
radian(degree) {
return degree * Math.PI / 180;
},
degree(radian) {
return radian * 180 / Math.PI;
},
/**
* Calculate the angle between two points.
*/
angleRadians(x1, y1, x2, y2) {
return Math.atan2(y2 - y1, x2 - x1);
},
/**
* Calculate the angle between two points.
*/
angleDegree(x1, y1, x2, y2) {
return Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;
},
/**
* Calculates 360 angle between two vector points.
*/
angleDegree360(x1, y1, x2, y2) {
let angle = Math.atan((x2-x1)/(y2-y1));
let degree = angle * 180 / Math.PI
degree *= 2;
degree += 180;
return degree;
},
/**
* Used to calculate the center offset value for a given element.
* Imagine the below:
*
* -----------8 (px)----------
* | |
* | |--5(cx)--| |
* | |
* |---ox--| |
* ---------------------------
*
* You have a parent (px) and child (cx) where by, you want to
* center the child. You know px and cx but you want to know the
* value of ox (offset).
*
* In the example above, px = 8, cx - 5, would return ox of 1.5
*
* Can also be used to calculate the oy value to vertically center items.
*
* Example - example of center positioning something to the center of the window size:
* this.x = mathf.calculateCenterOffset(window.innerWidth, this.width);
* this.y = mathf.calculateCenterOffset(window.innerHeight, this.height);
*/
calculateCenterOffset(px, cx) {
const halfParent = px / 2;
const halfChild = cx / 2;
const ox = halfParent - halfChild;
return ox;
}
}
export default mathf;
const stringf = {
makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}
export default stringf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment