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