annotate src/dad.js @ 3:6d5e4b38b4fb

start drag
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 21 Apr 2023 00:58:31 -0600
parents
children 0130ae25ef94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 'use strict';
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 let dad = {};
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // override this if needed
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 dad.whatToDrag = function(draggable) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 return draggable;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 };
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 let original = null;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 let dragging = null;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 let touchX, touchY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 function onMouseMove(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //console.log(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 event.preventDefault();
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 let rect = dragging.getBoundingClientRect();
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 dragging.style.left = `${rect.x+event.movementX}px`;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 let y = rect.y + event.movementY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( y < 0 ) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 window.scrollBy( 0, y );
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 } else if( y + rect.height > window.innerHeight ) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 window.scrollBy( 0, y + rect.height - window.innerHeight );
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 dragging.style.top = `${y}px`;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 function onTouchMove(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 let touches = event.touches;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if( touches.length !== 1 )
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 let touch = touches[0];
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 let x = touch.clientX;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 let y = touch.clientY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 event.movementX = x - touchX;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 event.movementY = y - touchY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 touchX = x;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 touchY = y;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 onMouseMove(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 function onMouseUp(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 //console.log(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 original.removeAttribute('dad-original');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 document.body.removeChild(dragging);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 document.removeEventListener('mousemove',onMouseMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 document.removeEventListener('mouseup',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 document.removeEventListener('mousemove',onMouseMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 document.removeEventListener('touchend',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 original.scrollIntoViewIfNeeded(false);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 original = null;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 dragging = null;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function start(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 //console.log(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 original = dad.whatToDrag(event.target);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 dragging = original.cloneNode(true);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 original.setAttribute('dad-original','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 dragging.setAttribute('dad-dragging','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 let rect = original.getBoundingClientRect();
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 dragging.style.left = `${rect.x}px`;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 dragging.style.top = `${rect.y}px`;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 document.body.appendChild(dragging);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 function onMouseDown(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 start(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 document.addEventListener('mousemove',onMouseMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 document.addEventListener('mouseup',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 function onTouchStart(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 start(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 let touches = event.touches;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 if( touches.length !== 1 )
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 let touch = touches[0];
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 touchX = touch.clientX;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 touchY = touch.clientY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 document.addEventListener('touchmove',onTouchMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 document.addEventListener('touchend',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 dad.setDraggable = function(el) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 el.setAttribute('dad-drag','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 el.addEventListener('mousedown',onMouseDown);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 el.addEventListener('touchstart',onTouchStart);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 };
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }