move src2 -> src, web2 -> web, remove old module system, fix deps

This commit is contained in:
Stewart Allen 2025-07-06 23:44:33 -04:00
commit ce4f5d6e78
488 changed files with 26179 additions and 26609 deletions

269
src.old/add/array.js Normal file
View file

@ -0,0 +1,269 @@
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
// extend Array, String, Number, Math
gapp.register("add.array", [], (root, exports) => {
let AP = {};
if (!AP.flat) {
AP.flat = function() {
try {
return [].concat.apply([], this);
} catch (e) {
// console.log({flat_error: e});
let out = [];
for (let e of this.slice()) {
out.push(...e);
}
return out;
}
};
}
AP.findWith = function(fn) {
for (let i=0, l=this.length; i<l; i++) {
if (fn(this[i])) {
return i;
}
}
return -1;
};
AP.equals = function(arr) {
if (!arr) return false;
if (arr.length !== this.length) return false;
for (let i = 0; i < this.length; i++) {
if (arr[i] !== this[i]) return false;
}
return true;
};
AP.peek = function(val = 1) {
return this[this.length - val];
};
AP.drop = function(pre = 0, post = 0) {
while (pre-- > 0) this.shift();
while (post-- > 0) this.pop();
return this;
};
/**
* allow chaining of push() calls
*/
AP.append = function(v, flat) {
if (flat) {
return this.appendAll(v);
} else {
this.push(v);
return this;
}
};
/**
* append all array elements to this array
*/
AP.appendAll = function(arr) {
if (arr && arr.length > 0) {
// avoid hitting stack limits
if (arr.length > 10000) {
for (let i = 0, il = arr.length; i < il; i++) {
this.push(arr[i]);
}
} else {
// this.push.apply(this,arr); (slower?)
this.push(...arr);
}
}
return this;
};
AP.addOnce = function(val) {
if (!this.contains(val)) {
this.push(val);
}
return this;
};
/**
* shallow cloning with clone(arg) call on each new element
*/
AP.clone = function() {
return this.map(v => v.clone(...arguments));
};
AP.xray = function(arg) {
const na = [],
ln = this.length;
for (let i = 0; i < ln; i++) {
na.push(this[i].xray(arg));
}
return na;
};
AP.xout = function(label, inset) {
if (label) console.log(`${label} [${this.length}]`);
if (!inset) inset = ' ';
this.forEach((el, i) => {
if (el.xout) {
el.xout(null, inset + i + inset);
} else if (el.xray) {
const info = el.xray();
console.log(inset, info);
Object.values(info).forEach(val => {
if (val.xout) {
val.xout(null, inset + inset);
}
});
}
});
};
/**
* remove and return element from array, if present
*/
AP.remove = function(val) {
let idx = this.indexOf(val);
if (idx >= 0) return this.splice(idx, 1);
return null;
};
AP.removeAll = function(array) {
for (let i = 0, il = array.length; i < il; i++) {
this.remove(array[i]);
}
};
/**
* return last array element if array length > 0
*/
AP.last = function() {
if (this.length === 0) return null;
return this[this.length - 1];
};
AP.contains = function(val) {
return this.indexOf(val) >= 0;
};
AP.toFloat32 = function() {
return new Float32Array(this);
};
AP.toUint32 = function() {
return Uint32Array.from(this);
};
AP.forEachPair = function(fn, incr) {
let scope = this,
idx = 0,
inc = incr || 2,
len = scope.length;
while (idx < len) {
fn(scope[idx], scope[(idx + 1) % len], idx);
idx += inc;
}
};
AP.show = function(fn, f) {
f = f || function(v) {
console.log(v)
};
this.forEach(function(av) {
f(av[fn]());
})
};
AP.uniq = function() {
return this.slice().sort().filter((x, i, a) => !i || x != a[i - 1]);
};
AP.hasNaN = function() {
return this.filter(v => isNaN(v)).length > 0;
};
// turn array into arrays of specified size
AP.group = function(size) {
let na = new Array(Math.ceil(this.length / size));
let nai = 0;
for (let i = 0; i < this.length; i += size) {
let grp = new Array(size);
for (let j = 0; j < size; j++) {
grp[j] = this[i + j];
}
na[nai++] = grp;
}
return na;
};
// wrap a function and unroll the first parameter into
// successive function calls if it is an array
Array.handle = function(fn) {
return function() {
let args = [...arguments];
let val = args.shift();
if (Array.isArray(val)) {
let rv = [];
for (let v of val.slice()) {
rv.push(fn(v, ...args));
}
return rv;
} else {
return fn(val, ...args);
}
}
};
for (let i in AP) {
Object.defineProperty(Array.prototype, i, {
value: AP[i],
enumerable: false
});
}
Float32Array.prototype.toShared = function() {
const newvert = new Float32Array(new SharedArrayBuffer(this.buffer.byteLength));
newvert.set(this);
return newvert;
};
Float32Array.prototype.toFloat32 = function() { return this };
ArrayBuffer.prototype.textDecode = function(encoding = 'utf-8') {
return new TextDecoder(encoding).decode(this);
};
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
Object.clone = function(o) {
// not using structuredClone because failes with some objects
// this method "cleans" out non clonables
return o ? JSON.parse(JSON.stringify(o)) : o;
};
Object.toArray = function(o) {
let ret = [];
for (let [key, value] of Object.entries(o)) {
ret.push({
key,
value
});
}
return ret;
};
Math.bound = function(val, min, max) {
return Math.max(min, Math.min(max, val));
};
Number.prototype.round = function(digits) {
if (digits === 0) return this.valueOf() | 0;
const pow = Math.pow(10, digits || 3);
return Math.round(this.valueOf() * pow) / pow;
};
});

174
src.old/add/class.js Normal file
View file

@ -0,0 +1,174 @@
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
// use: add.array
gapp.register("add.class", [], (root, exports) => {
self.ArrayWriter = class ArrayWriter {
constructor() {
this.pos = 0;
this.array = [];
}
seek(pos) {
let last = this.pos;
this.pos = pos;
return last;
}
skip(len) {
this.writeBytes(new Array(len).fill(0), len);
return this.pos;
}
writeU8(v) {
this.array[this.pos++] = v & 0xff;
}
writeU16(v) {
this.array[this.pos++] = (v >> 8) & 0xff;
this.array[this.pos++] = v & 0xff;
}
writeU32(v) {
this.array[this.pos++] = (v >> 24) & 0xff;
this.array[this.pos++] = (v >> 16) & 0xff;
this.array[this.pos++] = (v >> 8) & 0xff;
this.array[this.pos++] = v & 0xff;
}
writeBytes(bytes) {
this.array.appendAll(bytes);
this.pos += bytes.length;
}
toBuffer() {
return new Uint8ClampedArray(this.array).buffer;
}
};
self.DataWriter = class DataWriter {
constructor(view, pos) {
this.view = view;
this.pos = pos || 0;
}
skip(v) {
let p = this.pos;
this.pos += v;
return p;
}
writeU8(v) {
try {
this.view.setUint8(this.pos, v);
return this.skip(1);
} catch (err) {
console.log({pos:this.pos, err})
throw err;
}
}
writeU16(v,le) {
this.view.setUint16(this.pos, v, le);
return this.skip(2);
}
writeU32(v,le) {
this.view.setUint32(this.pos, v, le);
return this.skip(4);
}
writeI32(v,le) {
this.view.setInt32(this.pos, v, le);
return this.skip(4);
}
writeUB64(v,le) {
this.view.setBigUint64(this.pos, v, le);
return this.skip(8);
}
writeB64(v,le) {
this.view.setBigInt64(this.pos, v, le);
return this.skip(8);
}
writeF32(v,le) {
this.view.setFloat32(this.pos, v, le);
return this.skip(4);
}
writeF64(v,le) {
this.view.setFloat64(this.pos, v, le);
return this.skip(8);
}
}
self.DataReader = class DataReader {
constructor(view, pos) {
this.view = view;
this.pos = pos || 0;
}
skip(v) {
let p = this.pos;
this.pos += v;
return p;
}
readBytes(len) {
let buf = this.view.buffer.slice(this.pos, this.pos + len);
this.pos += len;
return buf;
}
readU8() {
return this.view.getUint8(this.pos++);
}
readU16(le) {
let v = this.view.getUint16(this.pos, le);
this.pos += 2;
return v;
}
readU32(le) {
let v = this.view.getUint32(this.pos, le);
this.pos += 4;
return v;
}
readI32(le) {
let v = this.view.getInt32(this.pos, le);
this.pos += 4;
return v;
}
readB64(le) {
let v = this.view.getBigInt64(this.pos, le);
this.pos += 8;
return v;
}
readUB64(le) {
let v = this.view.getBigUint64(this.pos, le);
this.pos += 8;
return v;
}
readF32(le) {
let v = this.view.getFloat32(this.pos, le);
this.pos += 4;
return v;
}
readF64(le) {
let v = this.view.getFloat64(this.pos, le);
this.pos += 8;
return v;
}
}
});

150
src.old/add/three.js Normal file
View file

@ -0,0 +1,150 @@
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
// dep: ext.three
gapp.register("add.three", [], (root, exports) => {
const {
THREE, SVGLoader, BufferGeometryUtils,
LineMaterial, Line2, LineGeometry, LineSegments2, LineSegmentsGeometry
} = ThreeBundle;
root.THREE = THREE;
THREE.BufferGeometryUtils = BufferGeometryUtils;
THREE.SVGLoader = SVGLoader.SVGLoader;
THREE.LineMaterial = LineMaterial;
THREE.Line2 = Line2;
THREE.LineGeometry = LineGeometry;
THREE.LineSegments2 = LineSegments2;
THREE.LineSegmentsGeometry = LineSegmentsGeometry;
let MP = THREE.Mesh.prototype,
XP = THREE.Box3.prototype,
BP = THREE.BufferGeometry.prototype;
THREE.computeFaceNormal = function(vA,vB,vC) {
const ab = new THREE.Vector3();
const cb = new THREE.Vector3();
cb.subVectors( vC, vB );
ab.subVectors( vA, vB );
cb.cross( ab );
cb.normalize();
return cb;
};
// https://www.npmjs.com/package/three-mesh-bvh
THREE.addBVH = function() {
const { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } = MeshBVHLib;
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
};
THREE.dispose = function(obj) {
if (!obj) {
return;
}
if (obj.geometry) {
obj.geometry.dispose();
}
if (obj.material && obj.material.map && obj.material.map.dispose) {
obj.material.map.dispose();
}
if (obj.children && obj.children.length) {
for (let c of obj.children) {
THREE.dispose(c);
}
}
};
// add .dim and .mid getters to Box3
Object.defineProperties(XP, {
dim: {
get: function() {
return this.max.clone().sub(this.min);
}
},
mid: {
get: function() {
return this.dim.clone().multiplyScalar(0.5).add(this.min);
}
}
});
MP.getBoundingBox = function(update) {
return this.geometry.getBoundingBox(update);
};
MP.mirrorX = function() {
return this.mirror(0);
};
MP.mirrorY = function() {
return this.mirror(1);
};
MP.mirrorZ = function() {
return this.mirror(2);
};
// fast mirror of vertices given axis array offset
// array elements are repeating [x,y,z,x,y,z,...]
MP.mirror = function(start) {
let i,
geo = this.geometry,
at = geo.attributes,
pa = at.position.array,
nm = at.normal.array;
for (i = start || 0 ; i < pa.length; i += 3) {
pa[i] = -pa[i];
nm[i] = -nm[i];
}
geo.computeVertexNormals();
return this;
};
// return cached or refreshed (when update = true) bounding box
BP.getBoundingBox = function(update) {
if (update || !this.boundingBox) {
this.boundingBox = null;
this.computeBoundingBox();
}
return this.boundingBox;
};
BP.moveMesh = function(x = 0, y = 0, z = 0) {
let gap = this.attributes.position,
pa = gap.array;
for (let i=0; i < pa.length; i += 3) {
pa[i ] += x;
pa[i + 1] += y;
pa[i + 2] += z;
}
gap.needsUpdate = true;
return this;
}
BP.shallowClone = function() {
let geo = new THREE.BufferGeometry();
for (let [k, v] of Object.entries(this.attributes)) {
geo.setAttribute(k, new THREE.BufferAttribute(v.array, v.itemSize));
}
return geo;
};
THREE.Object3D.prototype.newGroup = function() {
let group = new THREE.Group();
this.add(group);
return group;
};
THREE.Object3D.prototype.removeAll = function() {
this.children.slice().forEach(function (c) {
c.parent = undefined;
c.dispatchEvent( { type: 'removed' } );
});
this.children = [];
};
});