simulationjsv2 0.1.6 → 0.1.7

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.
@@ -68,23 +68,35 @@ export declare class BezierCurve2d {
68
68
  interpolateSlope(t: number): readonly [Vector2, Vector2];
69
69
  interpolate(t: number): Vector2;
70
70
  getPoints(): Vector2[];
71
+ getLength(): number;
71
72
  }
72
73
  export declare class CubicBezierCurve2d extends BezierCurve2d {
73
- constructor(points: [Vector2, Vector2, Vector2, Vector2]);
74
+ private detail;
75
+ constructor(points: [Vector2, Vector2, Vector2, Vector2], detail?: number);
76
+ getDetail(): number | undefined;
74
77
  }
75
78
  export declare class SplinePoint2d {
76
79
  private start;
77
80
  private end;
78
- private controls;
79
- constructor(start: Vertex | null, end: Vertex, controls: [Vector2, Vector2]);
81
+ private control1;
82
+ private control2;
83
+ private rawControls;
84
+ private detail;
85
+ constructor(start: Vertex | null, end: Vertex, control1: Vector2 | null, control2: Vector2, rawControls: [Vector2, Vector2], detail?: number);
80
86
  getStart(): Vertex | null;
81
87
  getEnd(): Vertex;
82
- getVectorArray(prevEnd?: Vector2 | null): readonly [Vector2, Vector2, Vector2, Vector2];
88
+ getControls(): readonly [Vector2 | null, Vector2];
89
+ getRawControls(): [Vector2, Vector2];
90
+ getDetail(): number | undefined;
91
+ getVectorArray(prevEnd: Vector2 | null, prevControl: Vector2 | null): readonly [Vector2, Vector2, Vector2, Vector2];
83
92
  }
84
93
  export declare class Spline2d extends SimulationElement {
85
94
  private curves;
86
95
  private width;
87
96
  private detail;
97
+ private interpolateLimit;
98
+ private distance;
88
99
  constructor(pos: Vector2, points: SplinePoint2d[], width?: number, color?: Color, detail?: number);
100
+ setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
89
101
  getBuffer(camera: Camera, force: boolean): number[];
90
102
  }
package/dist/graphics.js CHANGED
@@ -472,20 +472,36 @@ export class BezierCurve2d {
472
472
  getPoints() {
473
473
  return this.points;
474
474
  }
475
+ getLength() {
476
+ const start = this.points[0];
477
+ const end = this.points[this.points.length - 1];
478
+ return Math.sqrt(Math.pow(end[0] - start[0], 2) + Math.pow(end[1] - start[1], 2));
479
+ }
475
480
  }
476
481
  export class CubicBezierCurve2d extends BezierCurve2d {
477
- constructor(points) {
482
+ detail;
483
+ constructor(points, detail) {
478
484
  super(points);
485
+ this.detail = detail;
486
+ }
487
+ getDetail() {
488
+ return this.detail;
479
489
  }
480
490
  }
481
491
  export class SplinePoint2d {
482
492
  start;
483
493
  end;
484
- controls;
485
- constructor(start, end, controls) {
494
+ control1;
495
+ control2;
496
+ rawControls;
497
+ detail;
498
+ constructor(start, end, control1, control2, rawControls, detail) {
486
499
  this.start = start;
487
500
  this.end = end;
488
- this.controls = controls;
501
+ this.control1 = control1;
502
+ this.control2 = control2;
503
+ this.rawControls = rawControls;
504
+ this.detail = detail;
489
505
  }
490
506
  getStart() {
491
507
  return this.start;
@@ -493,8 +509,17 @@ export class SplinePoint2d {
493
509
  getEnd() {
494
510
  return this.end;
495
511
  }
496
- getVectorArray(prevEnd) {
497
- const firstControl = cloneBuf(this.controls[0]);
512
+ getControls() {
513
+ return [this.control1, this.control2];
514
+ }
515
+ getRawControls() {
516
+ return this.rawControls;
517
+ }
518
+ getDetail() {
519
+ return this.detail;
520
+ }
521
+ getVectorArray(prevEnd, prevControl) {
522
+ const firstControl = cloneBuf(this.control1 || prevControl || vector2());
498
523
  if (prevEnd) {
499
524
  vec2.add(firstControl, prevEnd, firstControl);
500
525
  }
@@ -504,7 +529,7 @@ export class SplinePoint2d {
504
529
  return [
505
530
  this.start ? vector2FromVector3(this.start.getPos()) : prevEnd,
506
531
  firstControl,
507
- this.controls[1],
532
+ this.control2,
508
533
  vector2FromVector3(this.end.getPos())
509
534
  ];
510
535
  }
@@ -513,26 +538,61 @@ export class Spline2d extends SimulationElement {
513
538
  curves;
514
539
  width;
515
540
  detail;
541
+ interpolateLimit;
542
+ distance;
516
543
  constructor(pos, points, width = 2, color, detail = 40) {
517
544
  super(vector3FromVector2(pos), color);
518
545
  this.curves = [];
519
546
  this.width = width * devicePixelRatio;
520
547
  this.detail = detail;
548
+ this.interpolateLimit = 1;
549
+ this.distance = 0;
521
550
  for (let i = 0; i < points.length; i++) {
522
- const bezierPoints = points[i].getVectorArray(i > 0 ? vector2FromVector3(points[i - 1].getEnd().getPos()) : null);
523
- const curve = new CubicBezierCurve2d(bezierPoints);
551
+ let prevControl = null;
552
+ if (i > 0) {
553
+ prevControl = cloneBuf(points[i - 1].getRawControls()[1]);
554
+ vec2.negate(prevControl, prevControl);
555
+ console.log(prevControl);
556
+ }
557
+ const bezierPoints = points[i].getVectorArray(i > 0 ? vector2FromVector3(points[i - 1].getEnd().getPos()) : null, prevControl);
558
+ const curve = new CubicBezierCurve2d(bezierPoints, points[i].getDetail());
559
+ this.distance += curve.getLength();
524
560
  this.curves.push(curve);
525
561
  }
526
562
  }
563
+ setInterpolateLimit(limit, t = 0, f) {
564
+ const diff = limit - this.interpolateLimit;
565
+ return transitionValues((p) => {
566
+ this.interpolateLimit += diff * p;
567
+ this.vertexCache.updated();
568
+ }, () => {
569
+ this.interpolateLimit = limit;
570
+ this.vertexCache.updated();
571
+ }, t, f);
572
+ }
527
573
  getBuffer(camera, force) {
528
574
  if (this.vertexCache.shouldUpdate() || force) {
529
575
  const screenSize = camera.getScreenSize();
530
- const step = 1 / this.detail;
531
576
  let verticesTop = [];
532
577
  const verticesBottom = [];
533
- for (let i = 0; i < this.curves.length; i++) {
534
- for (let j = 0; j < this.detail + 1; j++) {
535
- const [point, slope] = this.curves[i].interpolateSlope(step * j);
578
+ let currentDistance = 0;
579
+ outer: for (let i = 0; i < this.curves.length; i++) {
580
+ const detail = this.curves[i].getDetail() || this.detail;
581
+ const step = 1 / detail;
582
+ const distanceRatio = currentDistance / this.distance;
583
+ if (distanceRatio > this.interpolateLimit)
584
+ break;
585
+ const curveLength = this.curves[i].getLength();
586
+ currentDistance += curveLength;
587
+ const sectionRatio = curveLength / this.distance;
588
+ for (let j = 0; j < detail + 1; j++) {
589
+ let currentInterpolation = step * j;
590
+ let atLimit = false;
591
+ if (step * j * sectionRatio + distanceRatio > this.interpolateLimit) {
592
+ atLimit = true;
593
+ currentInterpolation = (this.interpolateLimit - distanceRatio) / sectionRatio;
594
+ }
595
+ const [point, slope] = this.curves[i].interpolateSlope(currentInterpolation);
536
596
  const pos = this.getPos();
537
597
  point[0] += pos[0];
538
598
  point[1] += screenSize[1] - pos[1];
@@ -543,6 +603,9 @@ export class Spline2d extends SimulationElement {
543
603
  verticesTop.push(vertTop);
544
604
  const vertBottom = vertex(point[0] - normal[0], point[1] - normal[1]);
545
605
  verticesBottom.unshift(vertBottom);
606
+ if (atLimit) {
607
+ break outer;
608
+ }
546
609
  }
547
610
  }
548
611
  verticesTop = verticesTop.concat(verticesBottom);
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, splinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d } 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, splinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d } from './utils.js';
package/dist/utils.d.ts CHANGED
@@ -88,5 +88,6 @@ export declare function randomColor(a?: number): Color;
88
88
  export declare function vertex(x?: number, y?: number, z?: number, color?: Color, is3dPoint?: boolean, uv?: Vector2): Vertex;
89
89
  export declare function color(r?: number, g?: number, b?: number, a?: number): Color;
90
90
  export declare function colorf(val: number, a?: number): Color;
91
- export declare function splinePoint2d(end: Vertex, control1: Vector2, control2: Vector2): SplinePoint2d;
91
+ export declare function splinePoint2d(end: Vertex, control1: Vector2, control2: Vector2, detail?: number): SplinePoint2d;
92
+ export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, detail?: number): SplinePoint2d;
92
93
  export {};
package/dist/utils.js CHANGED
@@ -284,10 +284,18 @@ export function color(r, g, b, a) {
284
284
  export function colorf(val, a) {
285
285
  return color(val, val, val, a);
286
286
  }
287
- export function splinePoint2d(end, control1, control2) {
287
+ export function splinePoint2d(end, control1, control2, detail) {
288
288
  vec2.scale(control1, devicePixelRatio, control1);
289
289
  vec2.scale(control2, devicePixelRatio, control2);
290
290
  vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
291
+ const rawControls = [cloneBuf(control1), cloneBuf(control2)];
291
292
  vec2.add(end.getPos(), control2, control2);
292
- return new SplinePoint2d(null, end, [control1, control2]);
293
+ return new SplinePoint2d(null, end, control1, control2, rawControls, detail);
294
+ }
295
+ export function continuousSplinePoint2d(end, control, detail) {
296
+ vec2.scale(control, devicePixelRatio, control);
297
+ vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
298
+ const rawControls = [vector2(), cloneBuf(control)];
299
+ vec2.add(end.getPos(), control, control);
300
+ return new SplinePoint2d(null, end, null, control, rawControls, detail);
293
301
  }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Jackson Otto",
7
7
  "description": "A simple graphics library using WebGPU",
8
- "version": "0.1.6",
8
+ "version": "0.1.7",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",