simulationjsv2 0.2.1 → 0.2.3

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.
@@ -137,9 +137,13 @@ export declare class Spline2d extends SimulationElement {
137
137
  private curves;
138
138
  private thickness;
139
139
  private detail;
140
+ private interpolateStart;
140
141
  private interpolateLimit;
141
142
  private distance;
142
143
  constructor(pos: Vertex, points: SplinePoint2d[], thickness?: number, detail?: number);
144
+ setInterpolateStart(start: number, t?: number, f?: LerpFunc): Promise<void>;
143
145
  setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
146
+ interpolateSlope(t: number): readonly [Vector2, Vector2];
147
+ interpolate(t: number): Vector2;
144
148
  getBuffer(camera: Camera, force: boolean): number[];
145
149
  }
package/dist/graphics.js CHANGED
@@ -742,6 +742,7 @@ export class BezierCurve2d {
742
742
  this.points = points;
743
743
  }
744
744
  interpolateSlope(t) {
745
+ t = Math.max(0, Math.min(1, t));
745
746
  let vectors = this.points;
746
747
  let slopeVector = vector2(1);
747
748
  while (vectors.length > 2) {
@@ -828,16 +829,16 @@ export class SplinePoint2d {
828
829
  }
829
830
  getColors(prevColor) {
830
831
  const colors = [null, null];
831
- if (this.start && this.start.getColor()) {
832
+ if (prevColor) {
833
+ colors[0] = prevColor;
834
+ }
835
+ else if (this.start && this.start.getColor()) {
832
836
  colors[0] = this.start.getColor();
833
837
  }
834
838
  if (this.end.getColor()) {
835
839
  colors[1] = this.end.getColor();
836
840
  }
837
- if (prevColor) {
838
- colors.unshift(prevColor);
839
- }
840
- if (colors.at(-1) === null) {
841
+ if (colors.length > 2 && colors.at(-1) === null) {
841
842
  colors.pop();
842
843
  }
843
844
  return colors;
@@ -862,6 +863,7 @@ export class Spline2d extends SimulationElement {
862
863
  curves;
863
864
  thickness;
864
865
  detail;
866
+ interpolateStart;
865
867
  interpolateLimit;
866
868
  distance;
867
869
  constructor(pos, points, thickness = devicePixelRatio, detail = 40) {
@@ -869,6 +871,7 @@ export class Spline2d extends SimulationElement {
869
871
  this.curves = [];
870
872
  this.thickness = thickness * devicePixelRatio;
871
873
  this.detail = detail;
874
+ this.interpolateStart = 0;
872
875
  this.interpolateLimit = 1;
873
876
  this.distance = 0;
874
877
  for (let i = 0; i < points.length; i++) {
@@ -888,6 +891,16 @@ export class Spline2d extends SimulationElement {
888
891
  this.curves.push(curve);
889
892
  }
890
893
  }
894
+ setInterpolateStart(start, t = 0, f) {
895
+ const diff = start - this.interpolateStart;
896
+ return transitionValues((p) => {
897
+ this.interpolateStart += diff * p;
898
+ this.vertexCache.updated();
899
+ }, () => {
900
+ this.interpolateStart = start;
901
+ this.vertexCache.updated();
902
+ }, t, f);
903
+ }
891
904
  setInterpolateLimit(limit, t = 0, f) {
892
905
  const diff = limit - this.interpolateLimit;
893
906
  return transitionValues((p) => {
@@ -898,12 +911,25 @@ export class Spline2d extends SimulationElement {
898
911
  this.vertexCache.updated();
899
912
  }, t, f);
900
913
  }
914
+ interpolateSlope(t) {
915
+ const curveInterval = 1 / this.curves.length;
916
+ let index = Math.floor(t / curveInterval);
917
+ if (index === this.curves.length)
918
+ index--;
919
+ const diff = (t - curveInterval * index) * 2;
920
+ return this.curves[index].interpolateSlope(diff);
921
+ }
922
+ interpolate(t) {
923
+ const [vec] = this.interpolateSlope(t);
924
+ return vec;
925
+ }
901
926
  getBuffer(camera, force) {
902
927
  if (this.vertexCache.shouldUpdate() || force) {
903
928
  const screenSize = camera.getScreenSize();
904
929
  let verticesTop = [];
905
930
  const verticesBottom = [];
906
931
  let currentDistance = 0;
932
+ let interpolationStarted = false;
907
933
  outer: for (let i = 0; i < this.curves.length; i++) {
908
934
  const detail = this.curves[i].getDetail() || this.detail;
909
935
  const step = 1 / detail;
@@ -920,6 +946,14 @@ export class Spline2d extends SimulationElement {
920
946
  atLimit = true;
921
947
  currentInterpolation = (this.interpolateLimit - distanceRatio) / sectionRatio;
922
948
  }
949
+ if (distanceRatio + currentInterpolation / this.curves.length < this.interpolateStart) {
950
+ continue;
951
+ }
952
+ if (!interpolationStarted) {
953
+ interpolationStarted = true;
954
+ currentInterpolation = (this.interpolateStart - distanceRatio) / sectionRatio;
955
+ j--;
956
+ }
923
957
  const [point, slope] = this.curves[i].interpolateSlope(currentInterpolation);
924
958
  const pos = this.getPos();
925
959
  point[0] += pos[0];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './simulation.js';
2
2
  export * from './graphics.js';
3
3
  export * from './types.js';
4
- export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, exponentialStep, splinePoint2d, continuousSplinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './simulation.js';
2
2
  export * from './graphics.js';
3
3
  export * from './types.js';
4
- export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, exponentialStep, splinePoint2d, continuousSplinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
package/dist/utils.d.ts CHANGED
@@ -74,7 +74,9 @@ export declare function transitionValues(callback1: (deltaT: number, t: number)
74
74
  export declare function lerp(a: number, b: number, t: number): number;
75
75
  export declare function smoothStep(t: number): number;
76
76
  export declare function linearStep(t: number): number;
77
- export declare function exponentialStep(t: number): number;
77
+ export declare function easeInOutExpo(t: number): number;
78
+ export declare function easeInOutQuart(t: number): number;
79
+ export declare function easeInOutQuad(t: number): number;
78
80
  export declare function vertexBuffer3d(x: number, y: number, z: number, color: Color, uv?: Vector2): number[];
79
81
  export declare function vertexBuffer2d(x: number, y: number, color: Color, uv?: Vector2): number[];
80
82
  export declare function vec3ToPixelRatio(vec: Vector3): void;
package/dist/utils.js CHANGED
@@ -248,7 +248,7 @@ export function smoothStep(t) {
248
248
  export function linearStep(t) {
249
249
  return t;
250
250
  }
251
- export function exponentialStep(t) {
251
+ export function easeInOutExpo(t) {
252
252
  return t === 0
253
253
  ? 0
254
254
  : t === 1
@@ -257,6 +257,12 @@ export function exponentialStep(t) {
257
257
  ? Math.pow(2, 20 * t - 10) / 2
258
258
  : (2 - Math.pow(2, -20 * t + 10)) / 2;
259
259
  }
260
+ export function easeInOutQuart(t) {
261
+ return t < 0.5 ? 8 * t * t * t * t : 1 - Math.pow(-2 * t + 2, 4) / 2;
262
+ }
263
+ export function easeInOutQuad(t) {
264
+ return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
265
+ }
260
266
  export function vertexBuffer3d(x, y, z, color, uv = vector2()) {
261
267
  return [x, y, z, 1, ...color.toBuffer(), ...uv, 1];
262
268
  }
@@ -318,21 +324,23 @@ export function continuousSplinePoint2d(end, control, detail) {
318
324
  return new SplinePoint2d(null, end, null, control, rawControls, detail);
319
325
  }
320
326
  export function interpolateColors(colors, t) {
327
+ t = Math.min(1, Math.max(0, t));
321
328
  if (colors.length === 0)
322
329
  return color();
323
330
  if (colors.length === 1)
324
331
  return colors[0];
325
332
  const colorInterval = 1 / colors.length;
326
333
  let index = Math.floor(t / colorInterval);
327
- if (index === colors.length)
328
- index--;
334
+ if (index >= colors.length)
335
+ index = colors.length - 1;
329
336
  const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
330
337
  const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
331
338
  const diff = to.diff(from);
332
- diff.r *= t / (colorInterval * colors.length);
333
- diff.g *= t / (colorInterval * colors.length);
334
- diff.b *= t / (colorInterval * colors.length);
335
- diff.a *= t / (colorInterval * colors.length);
339
+ const scale = t / (colorInterval * colors.length);
340
+ diff.r *= scale;
341
+ diff.g *= scale;
342
+ diff.b *= scale;
343
+ diff.a *= scale;
336
344
  const res = from.clone();
337
345
  res.r += diff.r;
338
346
  res.g += diff.g;
package/package.json CHANGED
@@ -5,13 +5,18 @@
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Jackson Otto",
7
7
  "description": "A simple graphics library using WebGPU",
8
- "version": "0.2.1",
8
+ "version": "0.2.3",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts"
13
13
  }
14
14
  },
15
+ "scripts": {
16
+ "dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
17
+ "test": "vite --port 3000",
18
+ "build": "npx tsc"
19
+ },
15
20
  "devDependencies": {
16
21
  "nodemon": "^3.0.1",
17
22
  "typescript": "^5.1.6",
@@ -20,10 +25,5 @@
20
25
  "dependencies": {
21
26
  "@webgpu/types": "^0.1.34",
22
27
  "wgpu-matrix": "^2.5.1"
23
- },
24
- "scripts": {
25
- "dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
26
- "test": "vite --port 3000",
27
- "build": "npx tsc"
28
28
  }
29
- }
29
+ }