-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.html
136 lines (108 loc) · 4.69 KB
/
cards.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="cards.css">
</head>
<body>
<div>Client X <span id="clientX"></span></div>
<div>Client Y <span id="clientY"></span></div>
<div>Card posX <span id="offsetX"></span></div>
<div>Card posY <span id="offsetY"></span></div>
<div id="playfield" ondragover="return false;">
<div class="guide"></div>
</div>
<script>
cardList = [];
class Card {
constructor(title, image) {
this.title = title;
this.image = image;
this.flipped = false;
this.x = 0;
this.y = 0;
this.createCard();
}
createCard = () => {
this.DOMcard = document.createElement("div");
this.DOMcard.classList.add("card");
this.DOMcard.draggable = true;
var img = document.createElement('img');
img.src = this.image;
this.DOMcard.addEventListener("dragstart", e => {
this.mouseOffsetX = e.target.offsetLeft - e.clientX;
this.mouseOffsetY = e.target.offsetTop - e.clientY;
});
this.DOMcard.addEventListener("drag", e => {
var a = this.checkAlign(e);
if(a) {
let guide = document.querySelector("div.guide");
guide.style.top = a.y + "px";
document.querySelector("div.guide").style.visibility = "visible"
}else{
document.querySelector("div.guide").style.visibility = "hidden"
}
});
this.DOMcard.addEventListener("dragend", e => {
document.querySelector("#offsetX").innerHTML = e.target.offsetTop;
document.querySelector("#offsetY").innerHTML = e.target.offsetLeft;
var a = this.checkAlign(e);
console.log(a);
if(a != undefined){
this.setPosition(e.clientX + this.mouseOffsetX, a.y);
}else{
this.setPosition(e.clientX + this.mouseOffsetX, e.clientY + this.mouseOffsetY);
}
// this.setPosition(e.clientX + this.mouseOffsetX, e.clientY + this.mouseOffsetY);
document.querySelector("div.guide").style.visibility = "hidden"
});
this.DOMcard.addEventListener("dblclick", e => {
console.log("Hi");
this.flip();
})
let cardImage = document.createElement("img");
cardImage.draggable = false;
cardImage.src = this.image;
let cardBack = document.createElement("img");
cardBack.draggable = false;
cardBack.classList.add("back");
cardBack.src = "cards/cardback.jpg";
this.DOMcard.appendChild(cardBack);
this.DOMcard.appendChild(cardImage);
document.getElementById("playfield").appendChild(this.DOMcard);
}
flip = () => {
this.flipped = !this.flipped;
this.DOMcard.classList.toggle("flipped");
}
setPosition = (x, y) => {
this.x = x;
this.y = y;
this.DOMcard.style.left = x + "px";
this.DOMcard.style.top = y + "px";
}
/*
returns an object with the closest x,y;
*/
checkAlign = (event) => {
var myCardY = event.clientY + this.mouseOffsetY;
return cardList.find((el,i,a)=>{
return (myCardY <= el.y + 10 && myCardY >= el.y - 10 && this != el);
})
}
}
cardList.push(new Card("Jean Luc Picard", "cards/jeanlucpicard.jpg"));
cardList.push(new Card("William Riker", "cards/williamtriker.jpg"));
cardList.push(new Card("Major Rikal", "cards/majorrakal.jpg"));
document.addEventListener('DOMContentLoaded', e => {
document.addEventListener("mousemove", e => {
document.querySelector("#clientX").innerHTML = e.clientX;
document.querySelector("#clientY").innerHTML = e.clientY;
});
});
</script>
</body>
</html>