canvu-react 0.4.73 → 0.4.74

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.
package/dist/react.cjs CHANGED
@@ -8561,6 +8561,11 @@ function shallowEqualStringArray(a, b) {
8561
8561
 
8562
8562
  // src/react/stroke-input.ts
8563
8563
  var MAX_INTERPOLATED_POINTS_PER_SAMPLE = 192;
8564
+ var MIN_CURVED_INTERPOLATION_DOT = -0.15;
8565
+ var MAX_STRAIGHT_INTERPOLATION_DOT = 0.985;
8566
+ var CURVED_INTERPOLATION_TENSION = 0.22;
8567
+ var MAX_CURVED_CONTROL_DISTANCE_RATIO = 0.5;
8568
+ var MIN_POINT_DISTANCE_SQUARED = 1e-12;
8564
8569
  function getPointerEventSamples(event) {
8565
8570
  if (typeof event.getCoalescedEvents !== "function") {
8566
8571
  return [event];
@@ -8575,6 +8580,62 @@ var resolveInterpolatedPressure = (lastPoint, nextPoint, ratio) => {
8575
8580
  return ratio === 1 ? nextPoint.pressure : void 0;
8576
8581
  };
8577
8582
  var resolveMaxStepWorld = (maxStepWorld) => Number.isFinite(maxStepWorld) && maxStepWorld > 0 ? maxStepWorld : Number.POSITIVE_INFINITY;
8583
+ var squaredDistance = (firstPoint, secondPoint) => {
8584
+ const deltaX = secondPoint.x - firstPoint.x;
8585
+ const deltaY = secondPoint.y - firstPoint.y;
8586
+ return deltaX * deltaX + deltaY * deltaY;
8587
+ };
8588
+ var resolvePreviousDistinctPoint = (points, lastPoint) => {
8589
+ for (let index = points.length - 2; index >= 0; index--) {
8590
+ const candidatePoint = points[index];
8591
+ if (candidatePoint && squaredDistance(candidatePoint, lastPoint) > MIN_POINT_DISTANCE_SQUARED) {
8592
+ return candidatePoint;
8593
+ }
8594
+ }
8595
+ return void 0;
8596
+ };
8597
+ var shouldUseCurvedInterpolation = (previousPoint, lastPoint, nextPoint) => {
8598
+ if (!previousPoint) return false;
8599
+ const incomingX = lastPoint.x - previousPoint.x;
8600
+ const incomingY = lastPoint.y - previousPoint.y;
8601
+ const outgoingX = nextPoint.x - lastPoint.x;
8602
+ const outgoingY = nextPoint.y - lastPoint.y;
8603
+ const incomingLengthSquared = incomingX * incomingX + incomingY * incomingY;
8604
+ const outgoingLengthSquared = outgoingX * outgoingX + outgoingY * outgoingY;
8605
+ if (incomingLengthSquared <= MIN_POINT_DISTANCE_SQUARED || outgoingLengthSquared <= MIN_POINT_DISTANCE_SQUARED) {
8606
+ return false;
8607
+ }
8608
+ const directionDot = (incomingX * outgoingX + incomingY * outgoingY) / Math.sqrt(incomingLengthSquared * outgoingLengthSquared);
8609
+ return directionDot >= MIN_CURVED_INTERPOLATION_DOT && directionDot <= MAX_STRAIGHT_INTERPOLATION_DOT;
8610
+ };
8611
+ var resolveCurvedControlPoint = (previousPoint, lastPoint, nextPoint, distance) => {
8612
+ const tangentX = nextPoint.x - previousPoint.x;
8613
+ const tangentY = nextPoint.y - previousPoint.y;
8614
+ const tangentLength = Math.sqrt(tangentX * tangentX + tangentY * tangentY);
8615
+ if (tangentLength <= MIN_POINT_DISTANCE_SQUARED) return void 0;
8616
+ const controlDistance = Math.min(
8617
+ distance * MAX_CURVED_CONTROL_DISTANCE_RATIO,
8618
+ tangentLength * CURVED_INTERPOLATION_TENSION
8619
+ );
8620
+ const scale = controlDistance / tangentLength;
8621
+ return {
8622
+ x: lastPoint.x + tangentX * scale,
8623
+ y: lastPoint.y + tangentY * scale
8624
+ };
8625
+ };
8626
+ var resolveInterpolatedPosition = (lastPoint, nextPoint, controlPoint, ratio) => {
8627
+ if (!controlPoint) {
8628
+ return {
8629
+ x: lastPoint.x + (nextPoint.x - lastPoint.x) * ratio,
8630
+ y: lastPoint.y + (nextPoint.y - lastPoint.y) * ratio
8631
+ };
8632
+ }
8633
+ const inverseRatio = 1 - ratio;
8634
+ return {
8635
+ x: inverseRatio * inverseRatio * lastPoint.x + 2 * inverseRatio * ratio * controlPoint.x + ratio * ratio * nextPoint.x,
8636
+ y: inverseRatio * inverseRatio * lastPoint.y + 2 * inverseRatio * ratio * controlPoint.y + ratio * ratio * nextPoint.y
8637
+ };
8638
+ };
8578
8639
  function appendInterpolatedStrokePoint(points, nextPoint, maxStepWorld) {
8579
8640
  if (points.length === 0) return [nextPoint];
8580
8641
  const lastPoint = points[points.length - 1];
@@ -8582,19 +8643,31 @@ function appendInterpolatedStrokePoint(points, nextPoint, maxStepWorld) {
8582
8643
  const deltaX = nextPoint.x - lastPoint.x;
8583
8644
  const deltaY = nextPoint.y - lastPoint.y;
8584
8645
  const distanceSquared = deltaX * deltaX + deltaY * deltaY;
8585
- if (distanceSquared < 1e-12) return points;
8646
+ if (distanceSquared < MIN_POINT_DISTANCE_SQUARED) return points;
8586
8647
  const distance = Math.sqrt(distanceSquared);
8587
8648
  const stepCount = Math.min(
8588
8649
  MAX_INTERPOLATED_POINTS_PER_SAMPLE,
8589
8650
  Math.max(1, Math.ceil(distance / resolveMaxStepWorld(maxStepWorld)))
8590
8651
  );
8591
8652
  if (stepCount === 1) return [...points, nextPoint];
8653
+ const previousPoint = resolvePreviousDistinctPoint(points, lastPoint);
8654
+ const controlPoint = shouldUseCurvedInterpolation(
8655
+ previousPoint,
8656
+ lastPoint,
8657
+ nextPoint
8658
+ ) ? resolveCurvedControlPoint(previousPoint, lastPoint, nextPoint, distance) : void 0;
8592
8659
  const interpolatedPoints = Array.from({ length: stepCount }, (_, index) => {
8593
8660
  const ratio = (index + 1) / stepCount;
8594
8661
  const pressure = resolveInterpolatedPressure(lastPoint, nextPoint, ratio);
8662
+ const position = resolveInterpolatedPosition(
8663
+ lastPoint,
8664
+ nextPoint,
8665
+ controlPoint,
8666
+ ratio
8667
+ );
8595
8668
  return {
8596
- x: lastPoint.x + deltaX * ratio,
8597
- y: lastPoint.y + deltaY * ratio,
8669
+ x: position.x,
8670
+ y: position.y,
8598
8671
  ...pressure != null ? { pressure } : {}
8599
8672
  };
8600
8673
  });