summaryrefslogtreecommitdiff
path: root/scripts/cards
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2022-08-18 02:17:23 -0400
committerKyle Gunger <kgunger12@gmail.com>2022-08-18 02:17:23 -0400
commit46aae06bd699b3ca396f7f5c750e1fb9fc9ff4bc (patch)
tree92a7c7179a568d0095c2fff3c14d20437c1acd8b /scripts/cards
parent4efae60183feb616c3dec7698a6a2fe4351b71c7 (diff)
Add transform to drags
Diffstat (limited to 'scripts/cards')
-rw-r--r--scripts/cards/card.js7
-rw-r--r--scripts/cards/deck.js24
-rw-r--r--scripts/cards/drag.js58
3 files changed, 59 insertions, 30 deletions
diff --git a/scripts/cards/card.js b/scripts/cards/card.js
index 53e9861..352f2b3 100644
--- a/scripts/cards/card.js
+++ b/scripts/cards/card.js
@@ -7,7 +7,7 @@ const CardPos = ["top", "topl", "topr", "mid", "midt", "midb", "bot", "botl", "b
// Every card should have a deck.
// Use deck.appendCard, deck.prependCard, or deck.addCardAt to make a card visible
class Card {
- constructor (id, data)
+ constructor (id, deck, data)
{
this.e = document.createElement("card");
this.generateElements(data);
@@ -16,6 +16,7 @@ class Card {
this.e.card = this;
this.id = id;
+ this.deck = deck;
}
// Generate a card with basic text only
@@ -125,6 +126,8 @@ class Card {
{
this.e.style.setProperty("--left", "0px");
this.e.style.setProperty("--top", "0px");
+ this.e.style.setProperty("--irot", "0");
+ this.e.style.setProperty("--iscale", "1");
}
resetPosInstant()
@@ -136,6 +139,6 @@ class Card {
}
getDeck() {
- return this.e.parentElement.deck;
+ return this.deck;
}
}
diff --git a/scripts/cards/deck.js b/scripts/cards/deck.js
index b1c7fd2..3ed94ff 100644
--- a/scripts/cards/deck.js
+++ b/scripts/cards/deck.js
@@ -64,6 +64,15 @@ class Deck {
updatePos()
{
+ let cb = this.e.getBoundingClientRect();
+
+ if(cb.width >= window.innerWidth)
+ this.e.className = "maxw";
+ else if (cb.height >= window.innerHeight)
+ this.e.className = "maxh";
+ else
+ this.e.className = "";
+
let len = this.cards.length - 1;
for(let i in this.cards)
this.cards[i].setPos(len-i);
@@ -77,6 +86,7 @@ class Deck {
}
this.cards.push(card);
this.e.appendChild(card.e);
+ card.deck = this.getID();
this.updatePos();
return true;
}
@@ -89,14 +99,14 @@ class Deck {
this.cards.unshift(card);
this.e.prepend(card.e);
card.setPos(this.cards.length - 1);
+ card.deck = this.getID();
this.updateCount();
return true;
}
addCardAt(card, index)
{
- if(index < 0 || index > this.cards.length)
- return
+ index = Math.min(Math.max(index, 0), this.cards.length);
if(index == 0) {
this.prependCard(card);
@@ -106,6 +116,7 @@ class Deck {
let temp = this.cards.slice(0, index);
temp[temp.length - 1].e.after(card.e);
this.cards.unshift(card);
+ card.deck = this.getID();
for(let i = temp.length - 1; i >= 0; i--)
this.cards.unshift(temp[i]);
@@ -135,7 +146,6 @@ class Deck {
if(index < 0 || index >= this.cards.length)
return
- this.e.removeChild(this.cards[index].e);
let c = this.cards.splice(index, 1)[0];
this.updatePos();
@@ -174,6 +184,14 @@ class Deck {
return (x > rect.left && x < rect.right && y > rect.top && y < rect.bottom)
}
+ dist(x, y)
+ {
+ let rect = this.e.getBoundingClientRect();
+ let mx = rect.left + (rect.right - rect.left) / 2;
+ let my = rect.top + (rect.bottom - rect.top) / 2;
+ return Math.sqrt((mx - x)**2 + (my - y)**2);
+ }
+
checkCard (el)
{
for(let c of this.cards)
diff --git a/scripts/cards/drag.js b/scripts/cards/drag.js
index ec82ade..0805556 100644
--- a/scripts/cards/drag.js
+++ b/scripts/cards/drag.js
@@ -5,7 +5,7 @@ class MultiDrag extends EventTarget {
drag = [];
mouse = [null, null];
- constructor(ret = false) {
+ constructor(root, ret = false) {
super();
window.addEventListener("mousemove", this.update.bind(this));
@@ -15,26 +15,32 @@ class MultiDrag extends EventTarget {
//window.addEventListener("touchend", this.stopDraggingAll.bind(this));
//window.addEventListener("touchcancel", this.stopDraggingAll.bind(this));
+ this.root = root;
this.ret = ret;
+ this.transform = ()=>{return [0.0, 1.0];};
}
- addDragEl(el, ox, oy, px, py, pt) {
+ addDragEl(el, px, py) {
if(this.del)
return;
- el.style.transitionDuration = "0s";
+ el.className = "instant";
let push = {
e: el,
- osx: ox,
- osy: oy,
- ptd: pt
+ ep: el.parentElement,
+ ex: el.getBoundingClientRect().left,
+ ey: el.getBoundingClientRect().top,
+ px: px,
+ py: py
};
- if(this.ret) {
- push.prx = px;
- push.pry = py;
- }
+ let t = this.transform(px, py);
+ this.root.appendChild(el);
+ el.style.setProperty("--left", push.ex + "px");
+ el.style.setProperty("--top", push.ey + "px");
+ el.style.setProperty("--irot", t[0]);
+ el.style.setProperty("--iscale", t[1]);
this.drag.push(push);
@@ -60,14 +66,7 @@ class MultiDrag extends EventTarget {
let cap = new Event("dragstart");
- cap.drag = this.addDragEl(
- e.target,
- e.pageX - parseInt(e.target.style.getPropertyValue("--left")),
- e.pageY - parseInt(e.target.style.getPropertyValue("--top")),
- e.target.style.left,
- e.target.style.top,
- e.target.style.transitionDuration
- );
+ cap.drag = this.addDragEl(e.target, e.pageX, e.pageY);
this.dispatchEvent(cap);
}
@@ -85,11 +84,12 @@ class MultiDrag extends EventTarget {
cap.x = this.mouse[0];
cap.y = this.mouse[1];
- this.drag[i].e.style.transitionDuration = this.drag[i].ptd;
+ this.drag[i].e.className = "";
if(this.ret) {
- this.drag[i].e.style.setProperty("--left", this.drag[i].prx + "px");
- this.drag[i].e.style.setProperty("--top", this.drag[i].pry + "px");
+ this.drag[i].e.style.setProperty("--left", "0px");
+ this.drag[i].e.style.setProperty("--top", "0px");
+ this.drag[i].ep.appendChild(this.drag[i].e);
}
cap.drag = this.drag.splice(i, 1);
@@ -123,8 +123,9 @@ class MultiDrag extends EventTarget {
this.drag[0].e.style.transitionDuration = this.drag[0].ptd;
if(this.ret) {
- this.drag[0].e.style.setProperty("--left", this.drag[0].prx + "px");
- this.drag[0].e.style.setProperty("--top", this.drag[0].pry + "px");
+ this.drag[0].e.style.setProperty("--left", "0px");
+ this.drag[0].e.style.setProperty("--top", "0px");
+ this.drag[0].ep.appendChild(this.drag[0].e);
}
cap.drag.push(this.drag.shift());
@@ -136,12 +137,19 @@ class MultiDrag extends EventTarget {
}
update(e) {
+ if(this.drag.length == 0)
+ return;
+
this.mouse[0] = e.pageX;
this.mouse[1] = e.pageY;
+ let t = this.transform(this.mouse[0], this.mouse[1]);
for (let i = 0; i < this.drag.length && !this.del; i++) {
- this.drag[i].e.style.setProperty("--left", e.pageX - this.drag[i].osx + "px");
- this.drag[i].e.style.setProperty("--top", e.pageY - this.drag[i].osy + "px");
+ let d = this.drag[i];
+ d.e.style.setProperty("--left", (e.pageX - d.px) + d.ex + "px");
+ d.e.style.setProperty("--top", (e.pageY - d.py) + d.ey + "px");
+ d.e.style.setProperty("--irot", t[0]);
+ d.e.style.setProperty("--iscale", t[1]);
}
}