feat: treat Touch Events as authority during a live touch, and enlarge rocket flames
On a real phone, PointerEvent coordinates can be wrong while TouchEvent changedTouches stay correct, so a one-tile drag walked the whole column. While a touchstart is live, Touch Events lead and pointer move/up/cancel are ignored. Document-level non-passive touch listeners prevent scrolling, setPointerCapture is gone, cancel ends without launching, and layout freezes mid-drag. ?debug=drag shows an overlay. Rocket flames are 3x as large in width and length, extending downward under the block (t1110).
@@ -11,6 +11,7 @@ html, body {
11
11
color: #e8eaf2;
12
12
font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
13
13
touch-action: none;
14
overscroll-behavior: none;
14
15
-webkit-user-select: none;
15
16
user-select: none;
16
17
-webkit-tap-highlight-color: transparent;
@@ -539,3 +540,24 @@ a.button:is(:hover, :focus-visible) {
539
540
.planet-card .planet-nums { font-size: 0.68rem; }
540
541
.planet-card { padding: 10px 8px 8px; }
541
542
}
543
544
/* t1106: on-screen log for ?debug=drag */
545
#drag-debug {
546
position: fixed;
547
left: 6px;
548
right: 6px;
549
bottom: 6px;
550
z-index: 50;
551
pointer-events: none;
552
background: rgba(8, 10, 22, 0.82);
553
color: #c6f6c8;
554
font: 11px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
555
padding: 8px 10px;
556
border-radius: 6px;
557
border: 1px solid #3a6;
558
white-space: pre-wrap;
559
word-break: break-word;
560
max-height: 28vh;
561
overflow: hidden;
562
}
563
@@ -11,6 +11,7 @@ html, body {11
color: #e8eaf2;11
color: #e8eaf2;12
font-family: "Segoe UI", system-ui, -apple-system, sans-serif;12
font-family: "Segoe UI", system-ui, -apple-system, sans-serif;13
touch-action: none;13
touch-action: none;14
overscroll-behavior: none;14
-webkit-user-select: none;15
-webkit-user-select: none;15
user-select: none;16
user-select: none;16
-webkit-tap-highlight-color: transparent;17
-webkit-tap-highlight-color: transparent;@@ -539,3 +540,24 @@ a.button:is(:hover, :focus-visible) {539
.planet-card .planet-nums { font-size: 0.68rem; }540
.planet-card .planet-nums { font-size: 0.68rem; }540
.planet-card { padding: 10px 8px 8px; }541
.planet-card { padding: 10px 8px 8px; }541
}542
}543
544
/* t1106: on-screen log for ?debug=drag */545
#drag-debug {546
position: fixed;547
left: 6px;548
right: 6px;549
bottom: 6px;550
z-index: 50;551
pointer-events: none;552
background: rgba(8, 10, 22, 0.82);553
color: #c6f6c8;554
font: 11px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;555
padding: 8px 10px;556
border-radius: 6px;557
border: 1px solid #3a6;558
white-space: pre-wrap;559
word-break: break-word;560
max-height: 28vh;561
overflow: hidden;562
}563
@@ -58,6 +58,9 @@ var app = {
58
58
flames: Object.create(null),
59
59
cells: [],
60
60
pointerId: null,
61
pointerType: null,
62
activeTouchId: null,
63
debugDrag: false,
61
64
gameOverShown: false,
62
65
selectTween: null,
63
66
online: false,
@@ -70,6 +73,7 @@ var app = {
70
73
71
74
window.onload = function ()
72
75
{
76
app.debugDrag = isDragDebugEnabled();
73
77
var play = document.getElementById('btn-play');
74
78
play.addEventListener('click', onPlay);
75
79
play.addEventListener('pointerup', function (e)
@@ -222,10 +226,25 @@ function placeTile (obj, col, y)
222
226
obj.transform.size = { x: t, y: t };
223
227
}
224
228
229
function layoutIfNeeded ()
230
{
231
if (app.core && app.core.drag)
232
{ return; }
233
var vw = window.innerWidth || 320;
234
var vh = window.innerHeight || 480;
235
if (app._layoutVw === vw && app._layoutVh === vh && app.scene && app.tile)
236
{ return; }
237
layout();
238
}
239
225
240
function layout ()
226
241
{
242
if (app.core && app.core.drag)
243
{ return; }
227
244
var vw = window.innerWidth || 320;
228
245
var vh = window.innerHeight || 480;
246
app._layoutVw = vw;
247
app._layoutVh = vh;
229
248
var safeTop = 0;
230
249
try
231
250
{
@@ -309,9 +328,16 @@ function syncSprites ()
309
328
}
310
329
flame.renderer.visible = true;
311
330
var t = app.tile;
331
// 3x as large (length and width, t1110). Keep the top of the
332
// flame at the same overlap into the block so the extra length
333
// extends downward under the rocket.
334
var flameW = t * 0.85 * 3;
335
var flameH = t * 0.85 * 3;
336
var origCenter = app.fieldH - block.y * t + t * 0.15;
337
var origTop = origCenter - (t * 0.85) / 2;
312
338
flame.transform.position.x = block.col * t + t / 2;
313
flame.transform.position.y = app.fieldH - block.y * t + t * 0.15;
314
flame.transform.size = { x: t * 0.85, y: t * 0.85 };
339
flame.transform.position.y = origTop + flameH / 2;
340
flame.transform.size = { x: flameW, y: flameH };
315
341
}
316
342
else if (app.flames[block.id])
317
343
{ app.flames[block.id].renderer.visible = false; }
@@ -451,7 +477,11 @@ function tickView (dt)
451
477
if (app.mode !== 'playing' || !app.core)
452
478
{ return; }
453
479
454
layout();
480
// Never resize the canvas mid-drag: a URL-bar hide would move the
481
// field under the finger and jump the target row. Also skip work
482
// when the viewport has not actually changed.
483
if (!(app.core && app.core.drag))
484
{ layoutIfNeeded(); }
455
485
for (var i = 0; i < app.cells.length; i++)
456
486
{
457
487
var col = i % 10;
@@ -481,15 +511,58 @@ function tickView (dt)
481
511
482
512
/* ---------- pointer input (mouse + touch) --------------------------- */
483
513
/*
484
* Both mouse and touch use Pointer Events. After pointerdown we listen
485
* on window, so a drag that leaves the canvas (needed to launch a
486
* block at the top of a full column) still reaches moveDrag. We do
487
* not treat lostpointercapture as the end of a drag: on some touch
488
* browsers that fires mid-gesture and would cancel a launch.
514
* Mouse uses Pointer Events. Touch uses Touch Events as the authority:
515
* on a real phone, pointer clientX/Y can disagree with the visual
516
* finger (zoomed visual viewport, gesture takeover, capture cancel)
517
* while changedTouches stays correct. When a touch is active, pointer
518
* move/up/cancel are ignored so a poisoned pointer coordinate cannot
519
* walk the block to the top or bottom of the stack (t1106).
520
*
521
* After a down we also listen on the document, so a drag that leaves
522
* the canvas (needed to launch a block at the top of a full column)
523
* still reaches moveDrag. We do not call setPointerCapture: on mobile
524
* that is often cancelled when the browser takes the gesture, which
525
* would fire pointercancel mid-drag.
526
*
527
* A non-passive document touchstart/touchmove preventDefault during
528
* play stops scrolling, rubber-banding and the pointercancel that
529
* follows. pointercancel / touchcancel end the drag without launch.
530
*
531
* ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay.
489
532
*/
490
533
491
534
var boundCanvas = null;
492
535
var windowListening = false;
536
var touchListening = false;
537
var lastDragDebug = null;
538
539
function isDragDebugEnabled ()
540
{
541
try
542
{
543
var q = window.location && window.location.search;
544
if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q))
545
{ return true; }
546
if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1')
547
{ return true; }
548
}
549
catch (err) { /* ignore */ }
550
return false;
551
}
552
553
function eventClient (e)
554
{
555
var t = null;
556
if (e.changedTouches && e.changedTouches.length)
557
{ t = e.changedTouches[0]; }
558
else if (e.touches && e.touches.length)
559
{ t = e.touches[0]; }
560
if (t)
561
{ return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; }
562
if (e.clientX != null && e.clientY != null)
563
{ return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; }
564
return null;
565
}
493
566
494
567
function addWindowPointer ()
495
568
{
@@ -497,7 +570,7 @@ function addWindowPointer ()
497
570
{ return; }
498
571
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });
499
572
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });
500
window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false });
573
window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false });
501
574
windowListening = true;
502
575
}
503
576
@@ -507,28 +580,59 @@ function removeWindowPointer ()
507
580
{ return; }
508
581
window.removeEventListener('pointermove', onPointerMove, { capture: true });
509
582
window.removeEventListener('pointerup', onPointerUp, { capture: true });
510
window.removeEventListener('pointercancel', onPointerUp, { capture: true });
583
window.removeEventListener('pointercancel', onPointerCancel, { capture: true });
511
584
windowListening = false;
512
585
}
513
586
587
function addTouchListeners ()
588
{
589
if (touchListening)
590
{ return; }
591
var opts = { capture: true, passive: false };
592
document.addEventListener('touchstart', onTouchStart, opts);
593
document.addEventListener('touchmove', onTouchMove, opts);
594
document.addEventListener('touchend', onTouchEnd, opts);
595
document.addEventListener('touchcancel', onTouchCancel, opts);
596
touchListening = true;
597
}
598
599
function removeTouchListeners ()
600
{
601
if (!touchListening)
602
{ return; }
603
var opts = { capture: true };
604
document.removeEventListener('touchstart', onTouchStart, opts);
605
document.removeEventListener('touchmove', onTouchMove, opts);
606
document.removeEventListener('touchend', onTouchEnd, opts);
607
document.removeEventListener('touchcancel', onTouchCancel, opts);
608
touchListening = false;
609
}
610
514
611
function bindPointer (canvas)
515
612
{
516
613
unbindPointer();
517
614
boundCanvas = canvas;
518
615
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });
519
616
canvas.style.touchAction = 'none';
617
canvas.style.position = 'fixed';
520
618
canvas.style.cursor = 'pointer';
521
619
canvas.style.webkitUserSelect = 'none';
522
620
canvas.style.userSelect = 'none';
621
addTouchListeners();
622
if (app.debugDrag)
623
{ ensureDragDebugOverlay(); }
523
624
}
524
625
525
626
function unbindPointer ()
526
627
{
527
628
removeWindowPointer();
629
removeTouchListeners();
528
630
if (boundCanvas)
529
631
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }
530
632
boundCanvas = null;
531
633
app.pointerId = null;
634
app.pointerType = null;
635
app.activeTouchId = null;
532
636
}
533
637
534
638
function fieldFromEvent (e)
@@ -539,34 +643,94 @@ function fieldFromEvent (e)
539
643
var rect = canvas.getBoundingClientRect();
540
644
if (rect.width <= 0 || rect.height <= 0)
541
645
{ return null; }
646
var pt = eventClient(e);
647
if (!pt)
648
{ return null; }
542
649
// Map through the CSS box only. Using canvas.width / app.tile would
543
650
// scale the pointer by the backing-store ratio whenever that differs
544
651
// from the layout size (high-DPR devices), so a short finger move
545
652
// would look like a long swipe.
546
653
var width = Planet.FIELD_WIDTH;
547
654
var height = Planet.FIELD_HEIGHT;
548
var col = ((e.clientX - rect.left) / rect.width) * width;
549
var fieldY = (1 - (e.clientY - rect.top) / rect.height) * height;
550
return { col: col, y: fieldY };
655
var col = ((pt.x - rect.left) / rect.width) * width;
656
var fieldY = (1 - (pt.y - rect.top) / rect.height) * height;
657
return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect };
658
}
659
660
function playingForDrag ()
661
{
662
return app.core && app.core.state === 'playing';
663
}
664
665
function startDragFromEvent (e)
666
{
667
var pos = fieldFromEvent(e);
668
if (!pos)
669
{ return; }
670
if (app.core.drag)
671
{
672
app.core.moveDrag(pos.y);
673
logDragDebug(e, pos, 'move-already');
674
return;
675
}
676
if (app.core.beginDrag(pos.col, pos.y) && app.core.drag)
677
{ startPressTween(app.core.drag.blockId); }
678
logDragDebug(e, pos, 'down');
679
}
680
681
function moveDragFromEvent (e)
682
{
683
if (!app.core || !app.core.drag)
684
{ return; }
685
var pos = fieldFromEvent(e);
686
if (pos)
687
{ app.core.moveDrag(pos.y); }
688
logDragDebug(e, pos, 'move');
689
}
690
691
function finishDrag (opts)
692
{
693
opts = opts || {};
694
app.pointerId = null;
695
app.pointerType = null;
696
app.activeTouchId = null;
697
removeWindowPointer();
698
if (boundCanvas)
699
{
700
try
701
{
702
if (opts.pointerId != null && boundCanvas.releasePointerCapture)
703
{ boundCanvas.releasePointerCapture(opts.pointerId); }
704
}
705
catch (err) { /* ignore */ }
706
}
707
startReleaseTween();
708
if (app.core && app.core.drag)
709
{
710
if (opts.noLaunch)
711
{ app.core.drag.launchIntent = false; }
712
app.core.endDrag();
713
}
714
else if (app.core)
715
{ app.core.endDrag(); }
716
logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up');
551
717
}
552
718
553
719
function onPointerDown (e)
554
720
{
555
if (!app.core || app.core.state !== 'playing')
721
if (!playingForDrag())
556
722
{ return; }
557
if (app.pointerId != null)
723
if (app.pointerId != null && e.pointerId !== app.pointerId)
558
724
{ return; }
559
e.preventDefault();
725
if (e.preventDefault)
726
{ e.preventDefault(); }
560
727
app.pointerId = e.pointerId;
561
try
562
{ boundCanvas.setPointerCapture(e.pointerId); }
563
catch (err) { /* ignore */ }
728
app.pointerType = e.pointerType || 'mouse';
564
729
addWindowPointer();
565
var pos = fieldFromEvent(e);
566
// Instant grab, no long-press or hold delay: the block is taken on
567
// pointerdown and the click tween starts in the same moment.
568
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)
569
{ startPressTween(app.core.drag.blockId); }
730
// If a touch is already in charge, do not reset the grab from pointerdown.
731
if (app.activeTouchId != null)
732
{ return; }
733
startDragFromEvent(e);
570
734
}
571
735
572
736
function onPointerMove (e)
@@ -575,10 +739,20 @@ function onPointerMove (e)
575
739
{ return; }
576
740
if (!app.core || !app.core.drag)
577
741
{ return; }
578
e.preventDefault();
579
var pos = fieldFromEvent(e);
580
if (pos)
581
{ app.core.moveDrag(pos.y); }
742
// Touch coordinates are the authority; ignore pointer moves for a
743
// live touch so a wrong clientY cannot walk the whole column.
744
// Only skip when touchstart has actually arrived — some environments
745
// deliver touch-pointer events without a TouchEvent stream.
746
if (app.activeTouchId != null)
747
{
748
if (e.preventDefault)
749
{ e.preventDefault(); }
750
logDragDebug(e, fieldFromEvent(e), 'pointer-ignored');
751
return;
752
}
753
if (e.preventDefault)
754
{ e.preventDefault(); }
755
moveDragFromEvent(e);
582
756
}
583
757
584
758
function onPointerUp (e)
@@ -587,21 +761,155 @@ function onPointerUp (e)
587
761
{ return; }
588
762
if (e && e.pointerId != null && e.pointerId !== app.pointerId)
589
763
{ return; }
590
if (e)
764
if (app.activeTouchId != null)
765
{
766
// touchend will finish the drag.
767
if (e && e.preventDefault)
768
{ e.preventDefault(); }
769
logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored');
770
return;
771
}
772
if (e && e.preventDefault)
591
773
{ e.preventDefault(); }
592
app.pointerId = null;
593
removeWindowPointer();
594
if (boundCanvas)
774
finishDrag({ pointerId: e && e.pointerId });
775
}
776
777
function onPointerCancel (e)
778
{
779
if (app.pointerId == null)
780
{ return; }
781
if (e && e.pointerId != null && e.pointerId !== app.pointerId)
782
{ return; }
783
if (app.activeTouchId != null)
595
784
{
596
try
597
{ boundCanvas.releasePointerCapture(e && e.pointerId); }
598
catch (err) { /* ignore */ }
785
if (e && e.preventDefault)
786
{ e.preventDefault(); }
787
logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored');
788
return;
599
789
}
600
startReleaseTween();
601
if (app.core)
602
{ app.core.endDrag(); }
790
if (e && e.preventDefault)
791
{ e.preventDefault(); }
792
// A cancelled gesture is not a launch pull.
793
finishDrag({ pointerId: e && e.pointerId, noLaunch: true });
794
}
795
796
function touchById (list, id)
797
{
798
if (!list)
799
{ return null; }
800
for (var i = 0; i < list.length; i++)
801
{
802
if (list[i].identifier === id)
803
{ return list[i]; }
804
}
805
return null;
603
806
}
604
807
808
function onTouchStart (e)
809
{
810
if (!playingForDrag())
811
{ return; }
812
if (app.activeTouchId != null)
813
{
814
if (e.preventDefault)
815
{ e.preventDefault(); }
816
return;
817
}
818
var t = e.changedTouches && e.changedTouches[0];
819
if (!t)
820
{ return; }
821
if (e.preventDefault)
822
{ e.preventDefault(); }
823
app.activeTouchId = t.identifier;
824
addWindowPointer();
825
startDragFromEvent(e);
826
}
827
828
function onTouchMove (e)
829
{
830
if (app.activeTouchId == null)
831
{ return; }
832
if (!touchById(e.changedTouches, app.activeTouchId)
833
&& !touchById(e.touches, app.activeTouchId))
834
{ return; }
835
if (e.preventDefault)
836
{ e.preventDefault(); }
837
moveDragFromEvent(e);
838
}
839
840
function onTouchEnd (e)
841
{
842
if (app.activeTouchId == null)
843
{ return; }
844
if (!touchById(e.changedTouches, app.activeTouchId))
845
{ return; }
846
if (e.preventDefault)
847
{ e.preventDefault(); }
848
finishDrag({});
849
}
850
851
function onTouchCancel (e)
852
{
853
if (app.activeTouchId == null)
854
{ return; }
855
if (!touchById(e.changedTouches, app.activeTouchId))
856
{ return; }
857
if (e.preventDefault)
858
{ e.preventDefault(); }
859
finishDrag({ noLaunch: true });
860
}
861
862
function ensureDragDebugOverlay ()
863
{
864
if (document.getElementById('drag-debug'))
865
{ return; }
866
var el = document.createElement('div');
867
el.id = 'drag-debug';
868
el.setAttribute('aria-live', 'polite');
869
document.body.appendChild(el);
870
}
871
872
function logDragDebug (e, pos, phase)
873
{
874
if (!app.debugDrag)
875
{ return; }
876
ensureDragDebugOverlay();
877
var el = document.getElementById('drag-debug');
878
if (!el)
879
{ return; }
880
var block = null;
881
if (app.core && app.core.drag)
882
{ block = app.core.blockById(app.core.drag.blockId); }
883
var pt = e ? eventClient(e) : null;
884
var evType = e ? e.type : phase;
885
var info = {
886
phase: phase,
887
ev: evType,
888
src: pos && pos.src,
889
fieldY: pos && Math.round(pos.y * 100) / 100,
890
col: pos && Math.round(pos.col * 100) / 100,
891
cy: pt && Math.round(pt.y * 10) / 10,
892
blockY: block && Math.round(block.y * 100) / 100,
893
target: block && Math.floor((pos && pos.y) + 1e-12),
894
launch: app.core && app.core.drag && app.core.drag.launchIntent,
895
touchId: app.activeTouchId,
896
pid: app.pointerId,
897
ptype: app.pointerType
898
};
899
lastDragDebug = info;
900
el.textContent = 'drag ' + phase
901
+ ' ev=' + evType
902
+ ' src=' + (info.src || '-')
903
+ ' y=' + (info.fieldY != null ? info.fieldY : '-')
904
+ ' → ' + (info.target != null ? info.target : '-')
905
+ ' block=' + (info.blockY != null ? info.blockY : '-')
906
+ (info.launch ? ' LAUNCH' : '')
907
+ ' tId=' + info.touchId
908
+ ' pId=' + info.pid;
909
el.hidden = false;
910
}
911
912
605
913
function hideEl (id)
606
914
{
607
915
document.getElementById(id).hidden = true;
@@ -58,6 +58,9 @@ var app = {58
flames: Object.create(null),58
flames: Object.create(null),59
cells: [],59
cells: [],60
pointerId: null,60
pointerId: null,61
pointerType: null,62
activeTouchId: null,63
debugDrag: false,61
gameOverShown: false,64
gameOverShown: false,62
selectTween: null,65
selectTween: null,63
online: false,66
online: false,@@ -70,6 +73,7 @@ var app = {70
73
71
window.onload = function ()74
window.onload = function ()72
{75
{76
app.debugDrag = isDragDebugEnabled();73
var play = document.getElementById('btn-play');77
var play = document.getElementById('btn-play');74
play.addEventListener('click', onPlay);78
play.addEventListener('click', onPlay);75
play.addEventListener('pointerup', function (e)79
play.addEventListener('pointerup', function (e)@@ -222,10 +226,25 @@ function placeTile (obj, col, y)222
obj.transform.size = { x: t, y: t };226
obj.transform.size = { x: t, y: t };223
}227
}224
228
229
function layoutIfNeeded ()230
{231
if (app.core && app.core.drag)232
{ return; }233
var vw = window.innerWidth || 320;234
var vh = window.innerHeight || 480;235
if (app._layoutVw === vw && app._layoutVh === vh && app.scene && app.tile)236
{ return; }237
layout();238
}239
225
function layout ()240
function layout ()226
{241
{242
if (app.core && app.core.drag)243
{ return; }227
var vw = window.innerWidth || 320;244
var vw = window.innerWidth || 320;228
var vh = window.innerHeight || 480;245
var vh = window.innerHeight || 480;246
app._layoutVw = vw;247
app._layoutVh = vh;229
var safeTop = 0;248
var safeTop = 0;230
try249
try231
{250
{@@ -309,9 +328,16 @@ function syncSprites ()309
}328
}310
flame.renderer.visible = true;329
flame.renderer.visible = true;311
var t = app.tile;330
var t = app.tile;331
// 3x as large (length and width, t1110). Keep the top of the332
// flame at the same overlap into the block so the extra length333
// extends downward under the rocket.334
var flameW = t * 0.85 * 3;335
var flameH = t * 0.85 * 3;336
var origCenter = app.fieldH - block.y * t + t * 0.15;337
var origTop = origCenter - (t * 0.85) / 2;312
flame.transform.position.x = block.col * t + t / 2;338
flame.transform.position.x = block.col * t + t / 2;313
flame.transform.position.y = app.fieldH - block.y * t + t * 0.15;339
flame.transform.position.y = origTop + flameH / 2;314
flame.transform.size = { x: t * 0.85, y: t * 0.85 };340
flame.transform.size = { x: flameW, y: flameH };315
}341
}316
else if (app.flames[block.id])342
else if (app.flames[block.id])317
{ app.flames[block.id].renderer.visible = false; }343
{ app.flames[block.id].renderer.visible = false; }@@ -451,7 +477,11 @@ function tickView (dt)451
if (app.mode !== 'playing' || !app.core)477
if (app.mode !== 'playing' || !app.core)452
{ return; }478
{ return; }453
479
454
layout();480
// Never resize the canvas mid-drag: a URL-bar hide would move the481
// field under the finger and jump the target row. Also skip work482
// when the viewport has not actually changed.483
if (!(app.core && app.core.drag))484
{ layoutIfNeeded(); }455
for (var i = 0; i < app.cells.length; i++)485
for (var i = 0; i < app.cells.length; i++)456
{486
{457
var col = i % 10;487
var col = i % 10;@@ -481,15 +511,58 @@ function tickView (dt)481
511
482
/* ---------- pointer input (mouse + touch) --------------------------- */512
/* ---------- pointer input (mouse + touch) --------------------------- */483
/*513
/*484
* Both mouse and touch use Pointer Events. After pointerdown we listen514
* Mouse uses Pointer Events. Touch uses Touch Events as the authority:485
* on window, so a drag that leaves the canvas (needed to launch a515
* on a real phone, pointer clientX/Y can disagree with the visual486
* block at the top of a full column) still reaches moveDrag. We do516
* finger (zoomed visual viewport, gesture takeover, capture cancel)487
* not treat lostpointercapture as the end of a drag: on some touch517
* while changedTouches stays correct. When a touch is active, pointer488
* browsers that fires mid-gesture and would cancel a launch.518
* move/up/cancel are ignored so a poisoned pointer coordinate cannot519
* walk the block to the top or bottom of the stack (t1106).520
*521
* After a down we also listen on the document, so a drag that leaves522
* the canvas (needed to launch a block at the top of a full column)523
* still reaches moveDrag. We do not call setPointerCapture: on mobile524
* that is often cancelled when the browser takes the gesture, which525
* would fire pointercancel mid-drag.526
*527
* A non-passive document touchstart/touchmove preventDefault during528
* play stops scrolling, rubber-banding and the pointercancel that529
* follows. pointercancel / touchcancel end the drag without launch.530
*531
* ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay.489
*/532
*/490
533
491
var boundCanvas = null;534
var boundCanvas = null;492
var windowListening = false;535
var windowListening = false;536
var touchListening = false;537
var lastDragDebug = null;538
539
function isDragDebugEnabled ()540
{541
try542
{543
var q = window.location && window.location.search;544
if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q))545
{ return true; }546
if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1')547
{ return true; }548
}549
catch (err) { /* ignore */ }550
return false;551
}552
553
function eventClient (e)554
{555
var t = null;556
if (e.changedTouches && e.changedTouches.length)557
{ t = e.changedTouches[0]; }558
else if (e.touches && e.touches.length)559
{ t = e.touches[0]; }560
if (t)561
{ return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; }562
if (e.clientX != null && e.clientY != null)563
{ return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; }564
return null;565
}493
566
494
function addWindowPointer ()567
function addWindowPointer ()495
{568
{@@ -497,7 +570,7 @@ function addWindowPointer ()497
{ return; }570
{ return; }498
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });571
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });499
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });572
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });500
window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false });573
window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false });501
windowListening = true;574
windowListening = true;502
}575
}503
576
@@ -507,28 +580,59 @@ function removeWindowPointer ()507
{ return; }580
{ return; }508
window.removeEventListener('pointermove', onPointerMove, { capture: true });581
window.removeEventListener('pointermove', onPointerMove, { capture: true });509
window.removeEventListener('pointerup', onPointerUp, { capture: true });582
window.removeEventListener('pointerup', onPointerUp, { capture: true });510
window.removeEventListener('pointercancel', onPointerUp, { capture: true });583
window.removeEventListener('pointercancel', onPointerCancel, { capture: true });511
windowListening = false;584
windowListening = false;512
}585
}513
586
587
function addTouchListeners ()588
{589
if (touchListening)590
{ return; }591
var opts = { capture: true, passive: false };592
document.addEventListener('touchstart', onTouchStart, opts);593
document.addEventListener('touchmove', onTouchMove, opts);594
document.addEventListener('touchend', onTouchEnd, opts);595
document.addEventListener('touchcancel', onTouchCancel, opts);596
touchListening = true;597
}598
599
function removeTouchListeners ()600
{601
if (!touchListening)602
{ return; }603
var opts = { capture: true };604
document.removeEventListener('touchstart', onTouchStart, opts);605
document.removeEventListener('touchmove', onTouchMove, opts);606
document.removeEventListener('touchend', onTouchEnd, opts);607
document.removeEventListener('touchcancel', onTouchCancel, opts);608
touchListening = false;609
}610
514
function bindPointer (canvas)611
function bindPointer (canvas)515
{612
{516
unbindPointer();613
unbindPointer();517
boundCanvas = canvas;614
boundCanvas = canvas;518
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });615
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });519
canvas.style.touchAction = 'none';616
canvas.style.touchAction = 'none';617
canvas.style.position = 'fixed';520
canvas.style.cursor = 'pointer';618
canvas.style.cursor = 'pointer';521
canvas.style.webkitUserSelect = 'none';619
canvas.style.webkitUserSelect = 'none';522
canvas.style.userSelect = 'none';620
canvas.style.userSelect = 'none';621
addTouchListeners();622
if (app.debugDrag)623
{ ensureDragDebugOverlay(); }523
}624
}524
625
525
function unbindPointer ()626
function unbindPointer ()526
{627
{527
removeWindowPointer();628
removeWindowPointer();629
removeTouchListeners();528
if (boundCanvas)630
if (boundCanvas)529
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }631
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }530
boundCanvas = null;632
boundCanvas = null;531
app.pointerId = null;633
app.pointerId = null;634
app.pointerType = null;635
app.activeTouchId = null;532
}636
}533
637
534
function fieldFromEvent (e)638
function fieldFromEvent (e)@@ -539,34 +643,94 @@ function fieldFromEvent (e)539
var rect = canvas.getBoundingClientRect();643
var rect = canvas.getBoundingClientRect();540
if (rect.width <= 0 || rect.height <= 0)644
if (rect.width <= 0 || rect.height <= 0)541
{ return null; }645
{ return null; }646
var pt = eventClient(e);647
if (!pt)648
{ return null; }542
// Map through the CSS box only. Using canvas.width / app.tile would649
// Map through the CSS box only. Using canvas.width / app.tile would543
// scale the pointer by the backing-store ratio whenever that differs650
// scale the pointer by the backing-store ratio whenever that differs544
// from the layout size (high-DPR devices), so a short finger move651
// from the layout size (high-DPR devices), so a short finger move545
// would look like a long swipe.652
// would look like a long swipe.546
var width = Planet.FIELD_WIDTH;653
var width = Planet.FIELD_WIDTH;547
var height = Planet.FIELD_HEIGHT;654
var height = Planet.FIELD_HEIGHT;548
var col = ((e.clientX - rect.left) / rect.width) * width;655
var col = ((pt.x - rect.left) / rect.width) * width;549
var fieldY = (1 - (e.clientY - rect.top) / rect.height) * height;656
var fieldY = (1 - (pt.y - rect.top) / rect.height) * height;550
return { col: col, y: fieldY };657
return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect };658
}659
660
function playingForDrag ()661
{662
return app.core && app.core.state === 'playing';663
}664
665
function startDragFromEvent (e)666
{667
var pos = fieldFromEvent(e);668
if (!pos)669
{ return; }670
if (app.core.drag)671
{672
app.core.moveDrag(pos.y);673
logDragDebug(e, pos, 'move-already');674
return;675
}676
if (app.core.beginDrag(pos.col, pos.y) && app.core.drag)677
{ startPressTween(app.core.drag.blockId); }678
logDragDebug(e, pos, 'down');679
}680
681
function moveDragFromEvent (e)682
{683
if (!app.core || !app.core.drag)684
{ return; }685
var pos = fieldFromEvent(e);686
if (pos)687
{ app.core.moveDrag(pos.y); }688
logDragDebug(e, pos, 'move');689
}690
691
function finishDrag (opts)692
{693
opts = opts || {};694
app.pointerId = null;695
app.pointerType = null;696
app.activeTouchId = null;697
removeWindowPointer();698
if (boundCanvas)699
{700
try701
{702
if (opts.pointerId != null && boundCanvas.releasePointerCapture)703
{ boundCanvas.releasePointerCapture(opts.pointerId); }704
}705
catch (err) { /* ignore */ }706
}707
startReleaseTween();708
if (app.core && app.core.drag)709
{710
if (opts.noLaunch)711
{ app.core.drag.launchIntent = false; }712
app.core.endDrag();713
}714
else if (app.core)715
{ app.core.endDrag(); }716
logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up');551
}717
}552
718
553
function onPointerDown (e)719
function onPointerDown (e)554
{720
{555
if (!app.core || app.core.state !== 'playing')721
if (!playingForDrag())556
{ return; }722
{ return; }557
if (app.pointerId != null)723
if (app.pointerId != null && e.pointerId !== app.pointerId)558
{ return; }724
{ return; }559
e.preventDefault();725
if (e.preventDefault)726
{ e.preventDefault(); }560
app.pointerId = e.pointerId;727
app.pointerId = e.pointerId;561
try728
app.pointerType = e.pointerType || 'mouse';562
{ boundCanvas.setPointerCapture(e.pointerId); }563
catch (err) { /* ignore */ }564
addWindowPointer();729
addWindowPointer();565
var pos = fieldFromEvent(e);730
// If a touch is already in charge, do not reset the grab from pointerdown.566
// Instant grab, no long-press or hold delay: the block is taken on731
if (app.activeTouchId != null)567
// pointerdown and the click tween starts in the same moment.732
{ return; }568
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)733
startDragFromEvent(e);569
{ startPressTween(app.core.drag.blockId); }570
}734
}571
735
572
function onPointerMove (e)736
function onPointerMove (e)@@ -575,10 +739,20 @@ function onPointerMove (e)575
{ return; }739
{ return; }576
if (!app.core || !app.core.drag)740
if (!app.core || !app.core.drag)577
{ return; }741
{ return; }578
e.preventDefault();742
// Touch coordinates are the authority; ignore pointer moves for a579
var pos = fieldFromEvent(e);743
// live touch so a wrong clientY cannot walk the whole column.580
if (pos)744
// Only skip when touchstart has actually arrived — some environments581
{ app.core.moveDrag(pos.y); }745
// deliver touch-pointer events without a TouchEvent stream.746
if (app.activeTouchId != null)747
{748
if (e.preventDefault)749
{ e.preventDefault(); }750
logDragDebug(e, fieldFromEvent(e), 'pointer-ignored');751
return;752
}753
if (e.preventDefault)754
{ e.preventDefault(); }755
moveDragFromEvent(e);582
}756
}583
757
584
function onPointerUp (e)758
function onPointerUp (e)@@ -587,21 +761,155 @@ function onPointerUp (e)587
{ return; }761
{ return; }588
if (e && e.pointerId != null && e.pointerId !== app.pointerId)762
if (e && e.pointerId != null && e.pointerId !== app.pointerId)589
{ return; }763
{ return; }590
if (e)764
if (app.activeTouchId != null)765
{766
// touchend will finish the drag.767
if (e && e.preventDefault)768
{ e.preventDefault(); }769
logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored');770
return;771
}772
if (e && e.preventDefault)591
{ e.preventDefault(); }773
{ e.preventDefault(); }592
app.pointerId = null;774
finishDrag({ pointerId: e && e.pointerId });593
removeWindowPointer();775
}594
if (boundCanvas)776
777
function onPointerCancel (e)778
{779
if (app.pointerId == null)780
{ return; }781
if (e && e.pointerId != null && e.pointerId !== app.pointerId)782
{ return; }783
if (app.activeTouchId != null)595
{784
{596
try785
if (e && e.preventDefault)597
{ boundCanvas.releasePointerCapture(e && e.pointerId); }786
{ e.preventDefault(); }598
catch (err) { /* ignore */ }787
logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored');788
return;599
}789
}600
startReleaseTween();790
if (e && e.preventDefault)601
if (app.core)791
{ e.preventDefault(); }602
{ app.core.endDrag(); }792
// A cancelled gesture is not a launch pull.793
finishDrag({ pointerId: e && e.pointerId, noLaunch: true });794
}795
796
function touchById (list, id)797
{798
if (!list)799
{ return null; }800
for (var i = 0; i < list.length; i++)801
{802
if (list[i].identifier === id)803
{ return list[i]; }804
}805
return null;603
}806
}604
807
808
function onTouchStart (e)809
{810
if (!playingForDrag())811
{ return; }812
if (app.activeTouchId != null)813
{814
if (e.preventDefault)815
{ e.preventDefault(); }816
return;817
}818
var t = e.changedTouches && e.changedTouches[0];819
if (!t)820
{ return; }821
if (e.preventDefault)822
{ e.preventDefault(); }823
app.activeTouchId = t.identifier;824
addWindowPointer();825
startDragFromEvent(e);826
}827
828
function onTouchMove (e)829
{830
if (app.activeTouchId == null)831
{ return; }832
if (!touchById(e.changedTouches, app.activeTouchId)833
&& !touchById(e.touches, app.activeTouchId))834
{ return; }835
if (e.preventDefault)836
{ e.preventDefault(); }837
moveDragFromEvent(e);838
}839
840
function onTouchEnd (e)841
{842
if (app.activeTouchId == null)843
{ return; }844
if (!touchById(e.changedTouches, app.activeTouchId))845
{ return; }846
if (e.preventDefault)847
{ e.preventDefault(); }848
finishDrag({});849
}850
851
function onTouchCancel (e)852
{853
if (app.activeTouchId == null)854
{ return; }855
if (!touchById(e.changedTouches, app.activeTouchId))856
{ return; }857
if (e.preventDefault)858
{ e.preventDefault(); }859
finishDrag({ noLaunch: true });860
}861
862
function ensureDragDebugOverlay ()863
{864
if (document.getElementById('drag-debug'))865
{ return; }866
var el = document.createElement('div');867
el.id = 'drag-debug';868
el.setAttribute('aria-live', 'polite');869
document.body.appendChild(el);870
}871
872
function logDragDebug (e, pos, phase)873
{874
if (!app.debugDrag)875
{ return; }876
ensureDragDebugOverlay();877
var el = document.getElementById('drag-debug');878
if (!el)879
{ return; }880
var block = null;881
if (app.core && app.core.drag)882
{ block = app.core.blockById(app.core.drag.blockId); }883
var pt = e ? eventClient(e) : null;884
var evType = e ? e.type : phase;885
var info = {886
phase: phase,887
ev: evType,888
src: pos && pos.src,889
fieldY: pos && Math.round(pos.y * 100) / 100,890
col: pos && Math.round(pos.col * 100) / 100,891
cy: pt && Math.round(pt.y * 10) / 10,892
blockY: block && Math.round(block.y * 100) / 100,893
target: block && Math.floor((pos && pos.y) + 1e-12),894
launch: app.core && app.core.drag && app.core.drag.launchIntent,895
touchId: app.activeTouchId,896
pid: app.pointerId,897
ptype: app.pointerType898
};899
lastDragDebug = info;900
el.textContent = 'drag ' + phase901
+ ' ev=' + evType902
+ ' src=' + (info.src || '-')903
+ ' y=' + (info.fieldY != null ? info.fieldY : '-')904
+ ' → ' + (info.target != null ? info.target : '-')905
+ ' block=' + (info.blockY != null ? info.blockY : '-')906
+ (info.launch ? ' LAUNCH' : '')907
+ ' tId=' + info.touchId908
+ ' pId=' + info.pid;909
el.hidden = false;910
}911
912
605
function hideEl (id)913
function hideEl (id)606
{914
{607
document.getElementById(id).hidden = true;915
document.getElementById(id).hidden = true;