refactor: script globals

This commit is contained in:
cha0s 2024-07-04 09:08:47 -05:00
parent 48ef8a6cbd
commit ec3cef8ee7
2 changed files with 57 additions and 2 deletions

View File

@ -1,3 +1,47 @@
export const {
abs,
acos,
acosh,
asin,
asinh,
atan,
atanh,
atan2,
ceil,
cbrt,
expm1,
clz32,
cos,
cosh,
exp,
floor,
fround,
hypot,
imul,
log,
log1p,
log2,
log10,
max,
min,
round,
sign,
sin,
sinh,
sqrt,
tan,
tanh,
trunc,
E,
LN10,
LN2,
LOG10E,
LOG2E,
PI,
SQRT1_2,
SQRT2,
} = Math;
export function clamp(n, min, max) {
return Math.max(min, Math.min(max, n));
}
@ -23,3 +67,7 @@ export function normalizeVector({x, y}) {
const k = 1 / Math.sqrt(x * x + y * y);
return {x: x * k, y: y * k};
}
export function random() {
return Math.random();
}

View File

@ -3,6 +3,10 @@ import {LRUCache} from 'lru-cache';
import Sandbox from '@/astride/sandbox.js';
import TickingPromise from '@/util/ticking-promise.js';
import delta from '@/util/delta.js';
import lfo from '@/util/lfo.js';
import * as MathUtil from '@/util/math.js';
import transition from '@/util/transition.js';
function parse(code, options = {}) {
return acornParse(code, {
@ -31,10 +35,13 @@ export default class Script {
static contextDefaults() {
return {
console,
Array,
Math,
console,
delta,
lfo,
Math: MathUtil,
Promise,
transition,
wait: (seconds) => (
new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);