Summary Files Commits Branches @commit @diff @stats
path: root / index.html
blob: 2d43ea555ac26f16a05c43d15bfe506d121d73da (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
    <head>
        <title>Minecraft Crafting</title>
    </head>
    <body>
        <article>
            <table id="crafting-bench">
                <tr>
                    <td><img id="1" class="slot" src="/" draggable="false"></td>
                    <td><img id="2" class="slot" src="/" draggable="false"></td>
                    <td><img id="3" class="slot" src="/" draggable="false"></td>
                </tr>
                <tr>
                    <td><img id="4" class="slot" src="/" draggable="false"></td>
                    <td><img id="5" class="slot" src="/" draggable="false"></td>
                    <td><img id="6" class="slot" src="/" draggable="false"></td>
                </tr>
                <tr>
                    <td><img id="7" class="slot" src="/" draggable="false"></td>
                    <td><img id="8" class="slot" src="/" draggable="false"></td>
                    <td><img id="9" class="slot" src="/" draggable="false"></td>
                </tr>
            </table>
            <table>
                <tr id="inventory">
                </tr>
            </table>
            <img id="selected" src="/" />
        </article>
<style>
body {
  background-color: black;
}
article {
  background-color: #c6c6c6;
  max-width: 40em;
  margin: 0 auto;
  padding: 1em;
  border-left:   0.3em solid white;
  border-top:    0.3em solid white;
  border-right:  0.3em solid #555;
  border-bottom: 0.3em solid #555;
  border-radius: 3px;
}

img, #crafting-bench td {
  width: 3em;
  height: 3em;
  border: none;
  vertical-align: center;
}
img[src$="/"] {
  opacity: 0;
}
#crafting-bench {
  background-color: #c6c6c6;
  border-spacing: 1px;
}
#crafting-bench td {
  border-left: 2px solid #373737;
  border-top: 2px solid #373737;
  border-right: 2px solid white;
  border-bottom: 2px solid white;
  background-color: #8b8b8b;
  padding: 0.15em;
}
#crafting-bench td:hover {
  background-color: #c6c6c6;
}
#crafting-bench, #crafting-bench tr, #crafting-bench td {
  user-select: none;
}

#inventory .item {
  cursor: pointer;
}

#selected {
  position: absolute;
  pointer-events: none;
}
</style>
        <script>
            const slots = document.getElementsByClassName('slot')
            var firstChange = ""
            function onSlotPress(e) {
                if (firstChange === "") {
                    if (e.target.src !== selected.src)
                        firstChange = "place"
                    else
                        firstChange = "remove"
                }

                if (firstChange === "place") {
                    if (e.target.src !== selected.src) {
                        e.target.src = selected.src
                        onSlotSrcChanged()
                    }
                }
                else if (firstChange === "remove") {
                    e.target.src = "/"
                    onSlotSrcChanged()
                }
            }
            function onSlotEnter(e) {
                if ((e.buttons & 1) === 1) {
                    onSlotPress(e)
                }
            }
            for (slot of slots) {
                slot.addEventListener('mouseenter', onSlotEnter)
                slot.addEventListener('mousedown', onSlotPress)
            }
            function onMouseRelease(e) {
                firstChange = ""
            }
            document.addEventListener('mouseup', onMouseRelease)

            const items = document.getElementsByClassName('item')
            function onItemPress(e) {
                if (e.target.id !== "")
                    return;
                console.log(e.target.src)
                if (e.target.src === undefined || e.target.src === "/" || e.target.src.endsWith("html")) {
                    selected.src = "/"
                }
                else {
                    selected.src = e.target.src
                    selectedFollow(e)
                }
            }
            document.addEventListener('mousedown', onItemPress)

            function selectedFollow(e) {
                selected.style.top  = (e.clientY - selected.clientHeight / 2) + 'px'
                selected.style.left = (e.clientX - selected.clientWidth / 2) + 'px'
            }
            document.addEventListener('mousemove', selectedFollow)

            var itemMap = {
                "plank": "https://minecraft.wiki/images/Oak_Planks.png",
            };
            for (item in itemMap) {
                var itemImg = document.createElement("img")
                itemImg.classList.add("item")
                itemImg.draggable = false
                itemImg.src = itemMap[item]
                var itemTD = document.createElement("td")
                itemTD.appendChild(itemImg)
                inventory.appendChild(itemTD);
            }
            for (key in itemMap) {
                itemMap[itemMap[key]] = key
            }

            function getCraft() {
                var result = ""
                for (slot of slots) {
                    if (slot.src in itemMap) {
                        result += itemMap[slot.src]
                    }
                    result += ","
                }
                return result
            }
            function onSlotSrcChanged() {
                console.log(getCraft())
            }
        </script>
    </body>
</html>