motion 14.10.1 → 14.11.0
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/motion.dev.js +547 -2
- package/dist/motion.js +1 -1
- package/package.json +3 -3
package/dist/motion.dev.js
CHANGED
|
@@ -9921,6 +9921,7 @@
|
|
|
9921
9921
|
"whileHover",
|
|
9922
9922
|
"whileTap",
|
|
9923
9923
|
"whileDrag",
|
|
9924
|
+
"whilePinch",
|
|
9924
9925
|
"exit",
|
|
9925
9926
|
];
|
|
9926
9927
|
const variantProps = /* @__PURE__ */ ["initial"].concat(variantPriorityOrder);
|
|
@@ -11720,6 +11721,10 @@
|
|
|
11720
11721
|
this.openDragLock = null;
|
|
11721
11722
|
this.isDragging = false;
|
|
11722
11723
|
this.currentDirection = null;
|
|
11724
|
+
/**
|
|
11725
|
+
* Where the drag started, which pointer offsets are added to. Public
|
|
11726
|
+
* so pinch() can restart a drag from where it leaves the element.
|
|
11727
|
+
*/
|
|
11723
11728
|
this.originPoint = { x: 0, y: 0 };
|
|
11724
11729
|
/**
|
|
11725
11730
|
* The permitted boundaries of travel, in pixels.
|
|
@@ -12490,6 +12495,540 @@
|
|
|
12490
12495
|
return cancel;
|
|
12491
12496
|
}
|
|
12492
12497
|
|
|
12498
|
+
/**
|
|
12499
|
+
* A trackpad pinch arrives as a stream of `wheel` events with no end
|
|
12500
|
+
* event, so it ends once none have arrived for this long.
|
|
12501
|
+
*/
|
|
12502
|
+
const wheelTimeout = 150;
|
|
12503
|
+
/**
|
|
12504
|
+
* Convert an `x`/`y` set in other units (e.g. `"-50%"` to centre an
|
|
12505
|
+
* element) to pixels, so the gesture can add to it.
|
|
12506
|
+
*/
|
|
12507
|
+
const toPixels = (value, size) => {
|
|
12508
|
+
const latest = value.get();
|
|
12509
|
+
if (typeof latest === "string") {
|
|
12510
|
+
value.set(parseFloat(latest) * (latest.endsWith("%") ? size / 100 : 1));
|
|
12511
|
+
}
|
|
12512
|
+
};
|
|
12513
|
+
const limitsOf = (option) => typeof option === "object" ? option : {};
|
|
12514
|
+
/**
|
|
12515
|
+
* Resolve `elastic` to a resistance for each side, as for `dragElastic`.
|
|
12516
|
+
*/
|
|
12517
|
+
const resolveElastic = (elastic = defaultElastic) => {
|
|
12518
|
+
const side = (value) => value === true ? defaultElastic : +(value || 0);
|
|
12519
|
+
return typeof elastic === "object"
|
|
12520
|
+
? { min: side(elastic.min), max: side(elastic.max) }
|
|
12521
|
+
: { min: side(elastic), max: side(elastic) };
|
|
12522
|
+
};
|
|
12523
|
+
const clampTo = (value, { min = -Infinity, max = Infinity }) => clamp(min, max, value);
|
|
12524
|
+
/**
|
|
12525
|
+
* The raw gesture value that elastic shows as `value`. A pinch that starts
|
|
12526
|
+
* while a value is still past its limit (like during a spring back) carries
|
|
12527
|
+
* on from it, rather than jumping.
|
|
12528
|
+
*/
|
|
12529
|
+
const unconstrain = (value, option, resistance) => {
|
|
12530
|
+
const limit = clampTo(value, limitsOf(option));
|
|
12531
|
+
const elastic = resistance[value < limit ? "min" : "max"];
|
|
12532
|
+
return elastic ? limit + (value - limit) / elastic : value;
|
|
12533
|
+
};
|
|
12534
|
+
/**
|
|
12535
|
+
* Listen for pinch gestures on `element`: two touches (or pens), a trackpad
|
|
12536
|
+
* pinch (`wheel` with `ctrlKey`, as Chrome, Edge and Firefox report it),
|
|
12537
|
+
* and Safari's `gesture*` events.
|
|
12538
|
+
*
|
|
12539
|
+
* `getValues` is called each time a gesture starts, in the frame's read
|
|
12540
|
+
* step, for the scale/rotate/x/y values to write to. It runs alongside the
|
|
12541
|
+
* element measurement so neither forces a layout.
|
|
12542
|
+
*
|
|
12543
|
+
* `getOptions` is read whenever it's needed, so options can change while
|
|
12544
|
+
* listening. Return `false` to disable the gesture, which also ends one in
|
|
12545
|
+
* progress.
|
|
12546
|
+
*
|
|
12547
|
+
* `onPinchStart` is called once as each gesture starts, with the gesture's
|
|
12548
|
+
* info as a third argument. It can return the gesture's handlers, as for
|
|
12549
|
+
* `pinch()`.
|
|
12550
|
+
*/
|
|
12551
|
+
function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart) {
|
|
12552
|
+
const pointers = new Map();
|
|
12553
|
+
/**
|
|
12554
|
+
* The gesture as last reported: focal point, log of a distance and an
|
|
12555
|
+
* angle. Only changes in distance and angle matter, so trackpad and
|
|
12556
|
+
* Safari input write a virtual distance (the scale) here. Logs keep
|
|
12557
|
+
* a long trackpad pinch from overflowing.
|
|
12558
|
+
*/
|
|
12559
|
+
const current = { x: 0, y: 0, l: 0, a: 0 };
|
|
12560
|
+
const start = { ...current };
|
|
12561
|
+
const prev = { ...current };
|
|
12562
|
+
let source;
|
|
12563
|
+
let lastEvent;
|
|
12564
|
+
let values;
|
|
12565
|
+
let originX = 0;
|
|
12566
|
+
let originY = 0;
|
|
12567
|
+
let lastX = 0;
|
|
12568
|
+
let lastY = 0;
|
|
12569
|
+
let hasMoved = false;
|
|
12570
|
+
let wheelTime = 0;
|
|
12571
|
+
let handlers = {};
|
|
12572
|
+
let unlock;
|
|
12573
|
+
/**
|
|
12574
|
+
* The gesture's scale and rotation before limits and elastic apply.
|
|
12575
|
+
*/
|
|
12576
|
+
let rawScale = 1;
|
|
12577
|
+
let rawRotate = 0;
|
|
12578
|
+
let stopSnap = () => { };
|
|
12579
|
+
/**
|
|
12580
|
+
* The latest enabled options, so a gesture disabled mid-way can still
|
|
12581
|
+
* report its end.
|
|
12582
|
+
*/
|
|
12583
|
+
let options = {};
|
|
12584
|
+
const getOptions = () => (options = getEnabledOptions() || options);
|
|
12585
|
+
const info = () => ({
|
|
12586
|
+
point: { x: current.x, y: current.y },
|
|
12587
|
+
offset: { x: current.x - start.x, y: current.y - start.y },
|
|
12588
|
+
scale: Math.exp(current.l - start.l),
|
|
12589
|
+
rotate: current.a - start.a,
|
|
12590
|
+
});
|
|
12591
|
+
/**
|
|
12592
|
+
* Read the element's box and values, and find its layout origin: its
|
|
12593
|
+
* centre without `x`/`y`. Scale and rotate are applied around the
|
|
12594
|
+
* centre (the default transform origin), so they don't move it.
|
|
12595
|
+
*/
|
|
12596
|
+
const measure = () => {
|
|
12597
|
+
const { left, top, width, height } = element.getBoundingClientRect();
|
|
12598
|
+
const { scale, rotate, pan, elastic } = getOptions();
|
|
12599
|
+
const [s, r, x, y] = (values = getValues());
|
|
12600
|
+
const resistance = resolveElastic(elastic);
|
|
12601
|
+
/**
|
|
12602
|
+
* Stop animations on values this gesture writes, and leave the
|
|
12603
|
+
* rest running.
|
|
12604
|
+
*/
|
|
12605
|
+
scale !== false && s.stop();
|
|
12606
|
+
rotate !== false && r.stop();
|
|
12607
|
+
rawScale = unconstrain(s.get(), scale, resistance);
|
|
12608
|
+
rawRotate = unconstrain(r.get(), rotate, resistance);
|
|
12609
|
+
if (pan !== false) {
|
|
12610
|
+
x.stop();
|
|
12611
|
+
y.stop();
|
|
12612
|
+
toPixels(x, element.offsetWidth ?? width);
|
|
12613
|
+
toPixels(y, element.offsetHeight ?? height);
|
|
12614
|
+
}
|
|
12615
|
+
lastX = x.get();
|
|
12616
|
+
lastY = y.get();
|
|
12617
|
+
originX = left + width / 2 - lastX;
|
|
12618
|
+
originY = top + height / 2 - lastY;
|
|
12619
|
+
};
|
|
12620
|
+
const dragControls = () => elementDragControls.get(visualElementStore.get(element));
|
|
12621
|
+
/**
|
|
12622
|
+
* On release, spring scale and rotation back inside their limits (or
|
|
12623
|
+
* to a `snap` angle), keeping the focal point in place, or everything
|
|
12624
|
+
* back to its origin with `snapToOrigin`.
|
|
12625
|
+
*/
|
|
12626
|
+
const snap = () => {
|
|
12627
|
+
const { scale: scaleOption, rotate: rotateOption, pan, snapToOrigin, transition, } = options;
|
|
12628
|
+
const [scale, rotate, x, y] = values;
|
|
12629
|
+
const s0 = scale.get();
|
|
12630
|
+
const r0 = rotate.get();
|
|
12631
|
+
const x0 = x.get();
|
|
12632
|
+
const y0 = y.get();
|
|
12633
|
+
const { snap: step } = limitsOf(rotateOption);
|
|
12634
|
+
let s1 = clampTo(s0, limitsOf(scaleOption));
|
|
12635
|
+
let r1 = clampTo(step ? Math.round(r0 / step) * step : r0, limitsOf(rotateOption));
|
|
12636
|
+
if (snapToOrigin) {
|
|
12637
|
+
s1 = 1;
|
|
12638
|
+
// The nearest upright angle, so it turns the shortest way
|
|
12639
|
+
r1 = Math.round(r0 / 360) * 360;
|
|
12640
|
+
}
|
|
12641
|
+
/**
|
|
12642
|
+
* Keep the focal point in place, unless the element doesn't move
|
|
12643
|
+
* with it, or a drag carries on and owns x/y.
|
|
12644
|
+
*/
|
|
12645
|
+
const moves = pan !== false && !dragControls()?.isDragging;
|
|
12646
|
+
const vx = originX + x0 - current.x;
|
|
12647
|
+
const vy = originY + y0 - current.y;
|
|
12648
|
+
if (!s0 ||
|
|
12649
|
+
(s1 === s0 && r1 === r0 && !(snapToOrigin && moves && (x0 || y0)))) {
|
|
12650
|
+
return;
|
|
12651
|
+
}
|
|
12652
|
+
const generator = spring({
|
|
12653
|
+
keyframes: [0, 1],
|
|
12654
|
+
stiffness: 200,
|
|
12655
|
+
damping: 40,
|
|
12656
|
+
...transition,
|
|
12657
|
+
});
|
|
12658
|
+
const startTime = time.now();
|
|
12659
|
+
/**
|
|
12660
|
+
* Drive the values from one spring, from 0 to 1, so they stay in
|
|
12661
|
+
* step and the focal point stays in place.
|
|
12662
|
+
*/
|
|
12663
|
+
const tick = () => {
|
|
12664
|
+
if (dragControls()?.isDragging)
|
|
12665
|
+
return stopSnap();
|
|
12666
|
+
const { value: p, done } = generator.next(time.now() - startTime);
|
|
12667
|
+
if (done)
|
|
12668
|
+
stopSnap();
|
|
12669
|
+
const k = mixNumber$1(s0, s1, p);
|
|
12670
|
+
const r = mixNumber$1(r0, r1, p);
|
|
12671
|
+
const angle = ((r - r0) * Math.PI) / 180;
|
|
12672
|
+
const cos = (Math.cos(angle) * k) / s0;
|
|
12673
|
+
const sin = (Math.sin(angle) * k) / s0;
|
|
12674
|
+
scaleOption !== false && scale.set(k);
|
|
12675
|
+
rotateOption !== false && rotate.set(r);
|
|
12676
|
+
if (moves) {
|
|
12677
|
+
x.set(snapToOrigin
|
|
12678
|
+
? mixNumber$1(x0, 0, p)
|
|
12679
|
+
: x0 + cos * vx - sin * vy - vx);
|
|
12680
|
+
y.set(snapToOrigin
|
|
12681
|
+
? mixNumber$1(y0, 0, p)
|
|
12682
|
+
: y0 + sin * vx + cos * vy - vy);
|
|
12683
|
+
}
|
|
12684
|
+
};
|
|
12685
|
+
stopSnap = () => cancelFrame(tick);
|
|
12686
|
+
frame.update(tick, true);
|
|
12687
|
+
};
|
|
12688
|
+
/**
|
|
12689
|
+
* Apply the change in the gesture since the last frame as an
|
|
12690
|
+
* incremental scale and rotation around the previous focal point,
|
|
12691
|
+
* plus the focal point's movement.
|
|
12692
|
+
*/
|
|
12693
|
+
const apply = () => {
|
|
12694
|
+
if (!values || !hasMoved)
|
|
12695
|
+
return;
|
|
12696
|
+
hasMoved = false;
|
|
12697
|
+
const { rotate: rotateOption, scale: scaleOption, pan, elastic, } = getOptions();
|
|
12698
|
+
const [scale, rotate, x, y] = values;
|
|
12699
|
+
const resistance = resolveElastic(elastic);
|
|
12700
|
+
const latest = scale.get();
|
|
12701
|
+
let theta = 0;
|
|
12702
|
+
let k = 1;
|
|
12703
|
+
/**
|
|
12704
|
+
* Apply limits and elastic to the gesture's raw scale and rotation.
|
|
12705
|
+
* At a hard limit, the raw value stops too, so moving back responds
|
|
12706
|
+
* straight away.
|
|
12707
|
+
*/
|
|
12708
|
+
const constrain = (raw, option) => {
|
|
12709
|
+
const limits = limitsOf(option);
|
|
12710
|
+
const limit = clampTo(raw, limits);
|
|
12711
|
+
return resistance[raw < limit ? "min" : "max"]
|
|
12712
|
+
? [applyConstraints(raw, limits, resistance), raw]
|
|
12713
|
+
: [limit, limit];
|
|
12714
|
+
};
|
|
12715
|
+
if (scaleOption !== false && latest) {
|
|
12716
|
+
const [next, raw] = constrain(clamp(1e-9, 1e9, rawScale * Math.exp(current.l - prev.l)), scaleOption);
|
|
12717
|
+
rawScale = raw;
|
|
12718
|
+
k = next / latest;
|
|
12719
|
+
scale.set(next);
|
|
12720
|
+
}
|
|
12721
|
+
if (rotateOption !== false) {
|
|
12722
|
+
const [next, raw] = constrain(rawRotate + current.a - prev.a, rotateOption);
|
|
12723
|
+
rawRotate = raw;
|
|
12724
|
+
theta = next - rotate.get();
|
|
12725
|
+
if (theta)
|
|
12726
|
+
rotate.set(next);
|
|
12727
|
+
}
|
|
12728
|
+
if (pan !== false) {
|
|
12729
|
+
/**
|
|
12730
|
+
* Pinch owns x/y while it runs, so stop any animation started
|
|
12731
|
+
* on them since (like a drag's snap back) taking over after.
|
|
12732
|
+
*/
|
|
12733
|
+
x.stop();
|
|
12734
|
+
y.stop();
|
|
12735
|
+
/**
|
|
12736
|
+
* If drag() or the drag prop is dragging this element with one
|
|
12737
|
+
* of the touches, it has set x/y this frame to where it started
|
|
12738
|
+
* plus the touch's offset. Carry on from our own values instead,
|
|
12739
|
+
* and restart the drag from them and the touch's latest point.
|
|
12740
|
+
* That way it continues from wherever the pinch leaves the
|
|
12741
|
+
* element, and its release velocity only counts movement after
|
|
12742
|
+
* the pinch, so a zoom doesn't end in a fling.
|
|
12743
|
+
*/
|
|
12744
|
+
const drag = dragControls();
|
|
12745
|
+
const isDragging = drag?.isDragging;
|
|
12746
|
+
if (!isDragging) {
|
|
12747
|
+
lastX = x.get();
|
|
12748
|
+
lastY = y.get();
|
|
12749
|
+
}
|
|
12750
|
+
const angle = (theta * Math.PI) / 180;
|
|
12751
|
+
const cos = Math.cos(angle) * k;
|
|
12752
|
+
const sin = Math.sin(angle) * k;
|
|
12753
|
+
const dx = prev.x - originX - lastX;
|
|
12754
|
+
const dy = prev.y - originY - lastY;
|
|
12755
|
+
/**
|
|
12756
|
+
* Move the centre so the previous focal point, scaled and
|
|
12757
|
+
* rotated around the centre, lands on the current focal point.
|
|
12758
|
+
*/
|
|
12759
|
+
lastX += dx + current.x - prev.x - (cos * dx - sin * dy);
|
|
12760
|
+
lastY += dy + current.y - prev.y - (sin * dx + cos * dy);
|
|
12761
|
+
if (isDragging) {
|
|
12762
|
+
const history = drag.panSession.history;
|
|
12763
|
+
history.splice(0, history.length - 1);
|
|
12764
|
+
/**
|
|
12765
|
+
* Date the point long ago, so drag only measures release
|
|
12766
|
+
* velocity from moves after the pinch. A move that drag
|
|
12767
|
+
* processes as the touch lifts is part of the pinch.
|
|
12768
|
+
*/
|
|
12769
|
+
history[0].timestamp = -Infinity;
|
|
12770
|
+
drag.originPoint.x = lastX;
|
|
12771
|
+
drag.originPoint.y = lastY;
|
|
12772
|
+
}
|
|
12773
|
+
x.set(lastX);
|
|
12774
|
+
y.set(lastY);
|
|
12775
|
+
}
|
|
12776
|
+
Object.assign(prev, current);
|
|
12777
|
+
handlers.onPinch?.(lastEvent, info());
|
|
12778
|
+
};
|
|
12779
|
+
/**
|
|
12780
|
+
* Runs after the update step, where drag sets x/y, so it has the final
|
|
12781
|
+
* say on them each frame.
|
|
12782
|
+
*/
|
|
12783
|
+
const update = () => {
|
|
12784
|
+
if (!getEnabledOptions())
|
|
12785
|
+
return end(lastEvent);
|
|
12786
|
+
apply();
|
|
12787
|
+
if (source === "wheel" && time.now() - wheelTime > wheelTimeout) {
|
|
12788
|
+
end(lastEvent);
|
|
12789
|
+
}
|
|
12790
|
+
};
|
|
12791
|
+
const begin = (event, from) => {
|
|
12792
|
+
if (!getEnabledOptions())
|
|
12793
|
+
return;
|
|
12794
|
+
stopSnap();
|
|
12795
|
+
source = from;
|
|
12796
|
+
lastEvent = event;
|
|
12797
|
+
hasMoved = false;
|
|
12798
|
+
values = undefined;
|
|
12799
|
+
handlers = {};
|
|
12800
|
+
Object.assign(start, current);
|
|
12801
|
+
Object.assign(prev, current);
|
|
12802
|
+
frame.read(measure);
|
|
12803
|
+
frame.preRender(update, true);
|
|
12804
|
+
const result = onPinchStart(element, event, info());
|
|
12805
|
+
handlers =
|
|
12806
|
+
typeof result === "function" ? { onComplete: result } : result || {};
|
|
12807
|
+
};
|
|
12808
|
+
const end = (event) => {
|
|
12809
|
+
if (!source)
|
|
12810
|
+
return;
|
|
12811
|
+
cancelFrame(measure);
|
|
12812
|
+
cancelFrame(update);
|
|
12813
|
+
if (getEnabledOptions()) {
|
|
12814
|
+
apply();
|
|
12815
|
+
values && snap();
|
|
12816
|
+
}
|
|
12817
|
+
source = undefined;
|
|
12818
|
+
handlers.onComplete?.(event, info());
|
|
12819
|
+
};
|
|
12820
|
+
const moved = (event) => {
|
|
12821
|
+
lastEvent = event;
|
|
12822
|
+
hasMoved = true;
|
|
12823
|
+
};
|
|
12824
|
+
const readPointers = () => {
|
|
12825
|
+
const [a, b] = pointers.values();
|
|
12826
|
+
const angle = (Math.atan2(b.y - a.y, b.x - a.x) * 180) / Math.PI;
|
|
12827
|
+
const delta = angle - current.a;
|
|
12828
|
+
current.x = (a.x + b.x) / 2;
|
|
12829
|
+
current.y = (a.y + b.y) / 2;
|
|
12830
|
+
current.l = Math.log(Math.hypot(b.x - a.x, b.y - a.y) || 1);
|
|
12831
|
+
/**
|
|
12832
|
+
* Take the shortest way round, so rotation keeps counting past
|
|
12833
|
+
* 180deg rather than flipping to -180deg.
|
|
12834
|
+
*/
|
|
12835
|
+
current.a += delta - 360 * Math.round(delta / 360);
|
|
12836
|
+
};
|
|
12837
|
+
const setPointer = (event) => pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
12838
|
+
const onPointerMove = (event) => {
|
|
12839
|
+
if (!pointers.has(event.pointerId))
|
|
12840
|
+
return;
|
|
12841
|
+
setPointer(event);
|
|
12842
|
+
if (source === "pointer") {
|
|
12843
|
+
readPointers();
|
|
12844
|
+
moved(event);
|
|
12845
|
+
}
|
|
12846
|
+
};
|
|
12847
|
+
const onPointerUp = (event) => {
|
|
12848
|
+
if (!pointers.delete(event.pointerId))
|
|
12849
|
+
return;
|
|
12850
|
+
if (source === "pointer")
|
|
12851
|
+
end(event);
|
|
12852
|
+
if (!pointers.size) {
|
|
12853
|
+
toggleWindowListeners();
|
|
12854
|
+
release();
|
|
12855
|
+
}
|
|
12856
|
+
};
|
|
12857
|
+
/**
|
|
12858
|
+
* Once a touch is down on the element, listen on the window too, so
|
|
12859
|
+
* the second touch can land beside it (for instance on a small image).
|
|
12860
|
+
*/
|
|
12861
|
+
const onPointerDown = (event) => {
|
|
12862
|
+
if (source ||
|
|
12863
|
+
pointers.size > 1 ||
|
|
12864
|
+
pointers.has(event.pointerId) ||
|
|
12865
|
+
event.pointerType === "mouse") {
|
|
12866
|
+
return;
|
|
12867
|
+
}
|
|
12868
|
+
if (!pointers.size)
|
|
12869
|
+
toggleWindowListeners(true);
|
|
12870
|
+
setPointer(event);
|
|
12871
|
+
if (pointers.size === 2) {
|
|
12872
|
+
readPointers();
|
|
12873
|
+
begin(event, "pointer");
|
|
12874
|
+
/**
|
|
12875
|
+
* Unless this element drags, hold the drag lock until every
|
|
12876
|
+
* touch lifts. A press on it then ends as a cancel, rather than
|
|
12877
|
+
* a tap, and a draggable parent doesn't drag.
|
|
12878
|
+
*/
|
|
12879
|
+
if (source &&
|
|
12880
|
+
!unlock &&
|
|
12881
|
+
!elementDragControls.get(visualElementStore.get(element))) {
|
|
12882
|
+
unlock = setDragLock(true);
|
|
12883
|
+
}
|
|
12884
|
+
}
|
|
12885
|
+
};
|
|
12886
|
+
/**
|
|
12887
|
+
* Release the drag lock a frame later, so a press ending on this same
|
|
12888
|
+
* pointerup still sees it.
|
|
12889
|
+
*/
|
|
12890
|
+
const release = () => {
|
|
12891
|
+
const done = unlock;
|
|
12892
|
+
unlock = undefined;
|
|
12893
|
+
done && frame.postRender(done);
|
|
12894
|
+
};
|
|
12895
|
+
const toggleWindowListeners = (add) => {
|
|
12896
|
+
const method = add ? "addEventListener" : "removeEventListener";
|
|
12897
|
+
window[method]("pointerdown", onPointerDown);
|
|
12898
|
+
window[method]("pointermove", onPointerMove);
|
|
12899
|
+
window[method]("pointerup", onPointerUp);
|
|
12900
|
+
window[method]("pointercancel", onPointerUp);
|
|
12901
|
+
};
|
|
12902
|
+
const onWheel = (event) => {
|
|
12903
|
+
if (!event.ctrlKey || (source && source !== "wheel"))
|
|
12904
|
+
return;
|
|
12905
|
+
current.x = event.clientX;
|
|
12906
|
+
current.y = event.clientY;
|
|
12907
|
+
if (!source) {
|
|
12908
|
+
current.l = 0;
|
|
12909
|
+
begin(event, "wheel");
|
|
12910
|
+
if (!source)
|
|
12911
|
+
return;
|
|
12912
|
+
}
|
|
12913
|
+
event.preventDefault();
|
|
12914
|
+
/**
|
|
12915
|
+
* Lines (Firefox's mouse wheel) to pixels, and cap each event so a
|
|
12916
|
+
* mouse wheel notch with ctrl held (about 100px) zooms a step.
|
|
12917
|
+
*/
|
|
12918
|
+
current.l -=
|
|
12919
|
+
clamp(-50, 50, event.deltaY * (event.deltaMode ? 16 : 1)) / 100;
|
|
12920
|
+
wheelTime = time.now();
|
|
12921
|
+
moved(event);
|
|
12922
|
+
};
|
|
12923
|
+
/**
|
|
12924
|
+
* Safari reports trackpad pinches as `gesture*` events. iOS Safari
|
|
12925
|
+
* fires them for touches too, alongside the pointer events we track.
|
|
12926
|
+
*/
|
|
12927
|
+
const onGesture = (event) => {
|
|
12928
|
+
if (!pointers.size && (!source || source === "gesture")) {
|
|
12929
|
+
const { scale, rotation, clientX, clientY } = event;
|
|
12930
|
+
current.x = clientX;
|
|
12931
|
+
current.y = clientY;
|
|
12932
|
+
current.l = Math.log(scale);
|
|
12933
|
+
current.a = rotation;
|
|
12934
|
+
if (source) {
|
|
12935
|
+
moved(event);
|
|
12936
|
+
if (event.type === "gestureend")
|
|
12937
|
+
end(event);
|
|
12938
|
+
}
|
|
12939
|
+
else if (event.type === "gesturestart") {
|
|
12940
|
+
begin(event, "gesture");
|
|
12941
|
+
}
|
|
12942
|
+
}
|
|
12943
|
+
if (source)
|
|
12944
|
+
event.preventDefault();
|
|
12945
|
+
};
|
|
12946
|
+
const listeners = {
|
|
12947
|
+
pointerdown: onPointerDown,
|
|
12948
|
+
wheel: onWheel,
|
|
12949
|
+
gesturestart: onGesture,
|
|
12950
|
+
gesturechange: onGesture,
|
|
12951
|
+
gestureend: onGesture,
|
|
12952
|
+
};
|
|
12953
|
+
for (const name in listeners) {
|
|
12954
|
+
element.addEventListener(name, listeners[name], { passive: false });
|
|
12955
|
+
}
|
|
12956
|
+
return () => {
|
|
12957
|
+
for (const name in listeners) {
|
|
12958
|
+
element.removeEventListener(name, listeners[name]);
|
|
12959
|
+
}
|
|
12960
|
+
toggleWindowListeners();
|
|
12961
|
+
end(lastEvent);
|
|
12962
|
+
stopSnap();
|
|
12963
|
+
pointers.clear();
|
|
12964
|
+
release();
|
|
12965
|
+
};
|
|
12966
|
+
}
|
|
12967
|
+
const valueKeys = ["scale", "rotate", "x", "y"];
|
|
12968
|
+
/**
|
|
12969
|
+
* The element's scale/rotate/x/y motion values: those of its
|
|
12970
|
+
* VisualElement if it has one (it's a <motion.*> component, or drag() or
|
|
12971
|
+
* animateLayout() own it), otherwise those animate() binds to it, created
|
|
12972
|
+
* from its current style if this is the first time.
|
|
12973
|
+
*/
|
|
12974
|
+
const getElementValues = (element) => {
|
|
12975
|
+
const visualElement = visualElementStore.get(element);
|
|
12976
|
+
const effect = isSVGElement(element) ? svgSubjectEffect : styleSubjectEffect;
|
|
12977
|
+
return valueKeys.map((key) => {
|
|
12978
|
+
if (visualElement) {
|
|
12979
|
+
return visualElement.getValue(key, key === "scale" ? 1 : 0);
|
|
12980
|
+
}
|
|
12981
|
+
let value = effect.get(element, key);
|
|
12982
|
+
if (!value) {
|
|
12983
|
+
value = motionValue(readStyleValue(element, key));
|
|
12984
|
+
effect(element, { [key]: value });
|
|
12985
|
+
}
|
|
12986
|
+
return value;
|
|
12987
|
+
});
|
|
12988
|
+
};
|
|
12989
|
+
function pinch(elementOrSelector, startOrOptions, options = {}) {
|
|
12990
|
+
let onPinchStart = () => { };
|
|
12991
|
+
if (typeof startOrOptions === "function") {
|
|
12992
|
+
onPinchStart = startOrOptions;
|
|
12993
|
+
}
|
|
12994
|
+
else if (startOrOptions) {
|
|
12995
|
+
options = startOrOptions;
|
|
12996
|
+
}
|
|
12997
|
+
const scope = activeScope.current;
|
|
12998
|
+
/**
|
|
12999
|
+
* Run the start callback and the handlers it returns in the active
|
|
13000
|
+
* scope, so work they start is collected. A returned function is
|
|
13001
|
+
* bound by `scope.bind` itself.
|
|
13002
|
+
*/
|
|
13003
|
+
if (scope) {
|
|
13004
|
+
const start = scope.bind(onPinchStart);
|
|
13005
|
+
onPinchStart = (element, event) => {
|
|
13006
|
+
const result = start(element, event);
|
|
13007
|
+
if (typeof result !== "object")
|
|
13008
|
+
return result;
|
|
13009
|
+
const handlers = {};
|
|
13010
|
+
for (const key in result) {
|
|
13011
|
+
const name = key;
|
|
13012
|
+
const handler = result[name];
|
|
13013
|
+
handlers[name] = handler && scope.bind(handler);
|
|
13014
|
+
}
|
|
13015
|
+
return handlers;
|
|
13016
|
+
};
|
|
13017
|
+
}
|
|
13018
|
+
const cleanups = resolveElements(elementOrSelector).map((element) => {
|
|
13019
|
+
scope?.track(element);
|
|
13020
|
+
const { style } = element;
|
|
13021
|
+
const touchAction = style.touchAction;
|
|
13022
|
+
style.touchAction = "none";
|
|
13023
|
+
const remove = addPinchListeners(element, () => getElementValues(element), () => options, onPinchStart);
|
|
13024
|
+
return () => {
|
|
13025
|
+
remove();
|
|
13026
|
+
style.touchAction = touchAction;
|
|
13027
|
+
};
|
|
13028
|
+
});
|
|
13029
|
+
return addToScope(() => cleanups.forEach((cleanup) => cleanup()));
|
|
13030
|
+
}
|
|
13031
|
+
|
|
12493
13032
|
/**
|
|
12494
13033
|
* Recursively traverse up the tree to check whether the provided child node
|
|
12495
13034
|
* is the parent or a descendant of it.
|
|
@@ -12705,14 +13244,18 @@
|
|
|
12705
13244
|
*/
|
|
12706
13245
|
const endEventOptions = { ...eventOptions, capture: true };
|
|
12707
13246
|
const onPointerEnd = (endEvent, success) => {
|
|
13247
|
+
/**
|
|
13248
|
+
* Another touch lifting (for instance after a pinch) doesn't
|
|
13249
|
+
* end the press.
|
|
13250
|
+
*/
|
|
13251
|
+
if (!isPrimaryPointer(endEvent))
|
|
13252
|
+
return;
|
|
12708
13253
|
window.removeEventListener("pointerup", onPointerUp, endEventOptions);
|
|
12709
13254
|
window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
|
|
12710
13255
|
window.removeEventListener("pointermove", onPointerMove, endEventOptions);
|
|
12711
13256
|
target.removeEventListener("contextmenu", preventDefault);
|
|
12712
13257
|
cancelFrame(tick);
|
|
12713
13258
|
isPressing.delete(target);
|
|
12714
|
-
if (!isPrimaryPointer(endEvent))
|
|
12715
|
-
return;
|
|
12716
13259
|
/**
|
|
12717
13260
|
* A press that ends while a drag is active is a cancel.
|
|
12718
13261
|
*/
|
|
@@ -17217,6 +17760,7 @@
|
|
|
17217
17760
|
exports.addBuilderToScope = addBuilderToScope;
|
|
17218
17761
|
exports.addDomEvent = addDomEvent;
|
|
17219
17762
|
exports.addEffect = addEffect;
|
|
17763
|
+
exports.addPinchListeners = addPinchListeners;
|
|
17220
17764
|
exports.addPointerEvent = addPointerEvent;
|
|
17221
17765
|
exports.addPointerInfo = addPointerInfo;
|
|
17222
17766
|
exports.addSVGValue = addSVGValue;
|
|
@@ -17442,6 +17986,7 @@
|
|
|
17442
17986
|
exports.parseCSSVariable = parseCSSVariable;
|
|
17443
17987
|
exports.parseValueFromTransform = parseValueFromTransform;
|
|
17444
17988
|
exports.percent = percent;
|
|
17989
|
+
exports.pinch = pinch;
|
|
17445
17990
|
exports.pipe = pipe;
|
|
17446
17991
|
exports.pixelsToPercent = pixelsToPercent;
|
|
17447
17992
|
exports.positionalKeys = positionalKeys;
|