Skip to content

Commit 9b7524c

Browse files
committed
UI and Camera improvements
Started working on UI, added debug info Standardised camera zoom values
1 parent 8df7c5d commit 9b7524c

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
<link rel="shortcut icon" href="assets/textures/placeholder.png" type="image/x-icon">
99
</head>
1010
<body>
11-
<div id="debugInfo"></div>
12-
13-
<div id="canvas">
11+
<div id="settings" class="ui">
12+
settings box woo
1413
</div>
1514

15+
<div id="debugInfo" class="ui"></div>
16+
17+
<div id="canvas"></div>
18+
1619
<script src="script.js"></script>
1720
</body>
1821
</html>

script.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function sprite(options) {
4949
sprite.texture.src = "assets/textures/"+ url;
5050
} else {
5151
sprite.texture = null
52-
sprite.size = {x:0, y:0}
52+
if (sprite.size == null) {sprite.size = {x:0, y:0}}
5353
}
5454
}
5555

@@ -97,7 +97,6 @@ function sprite(options) {
9797
this.render = function() {
9898
// Sets the center of the element to where it's top left corner was
9999
// Then sets its center to the center of its parent
100-
this.body.style.imageRendering = "pixelated"
101100
this.body.style.position = "absolute"
102101
this.body.style.left = (this.pos.x - this.size.x/2) + nopx(this.parent.body.style.width)/2 +"px"
103102
this.body.style.top = (-this.pos.y - this.size.y/2) + nopx(this.parent.body.style.height)/2 +"px"
@@ -120,11 +119,11 @@ function sprite(options) {
120119
this.body.style.background = "url("+ this.texture.src +")"
121120
}
122121

123-
124122
this.body.style.transform += "translate("+ this.offset.x +"px, "+ this.offset.y +"px) "
125123
this.body.style.transformOrigin = (-this.offset.x+this.body.style.width/2) +"px "+ (-this.offset.y+this.body.style.height/2) +"px"
124+
this.body.style.imageRendering = "pixelated"
126125

127-
126+
//this.body.style.filter = "drop-shadow(5px 5px 5px #222)"
128127

129128
this.body.style.border = this.debug ? "1px solid yellow" : "none"
130129
}
@@ -139,7 +138,6 @@ window.onload = function() {
139138
x: 1000,
140139
y: 1000
141140
},
142-
//scale: 1,
143141
offset: {
144142
x: 0,
145143
y: 0
@@ -148,7 +146,7 @@ window.onload = function() {
148146
x: 0,
149147
y: 0,
150148
z: 0,
151-
p: 500
149+
p: 0
152150
},
153151
texture: "background.png"
154152
})
@@ -171,32 +169,38 @@ window.onload = function() {
171169
canvas.style.width = window.innerWidth +"px"
172170
canvas.style.height = window.innerHeight +"px"
173171

174-
let camZoom = 0
175-
world.setScale(0.5+(camZoom+1)*camZoom)
172+
let camZoom = 1
173+
world.setScale(Math.pow(camZoom+1, 2))
176174
canvas.onwheel = function(e) {
177175
camZoom += e.deltaY/-1000
178-
camZoom = clamp(camZoom, 0, 3)
176+
camZoom = clamp(camZoom, -0.29, 3)
179177
camZoom = Math.floor(camZoom*100)/100 // Removes any imprecision errors
180-
world.setScale(0.5+(camZoom+1)*camZoom)
181-
console.log(world.scale)
178+
world.setScale(Math.pow(camZoom+1, 2))
182179
}
183180

181+
const debugInfo = this.document.getElementById("debugInfo")
182+
184183
const camPos = {x:0, y:0}
185184
function render() {
186-
const camSpeed = heldKeys["ShiftLeft"] ? 24 : 12
187-
if (heldKeys["KeyW"]) {camPos.y -= camSpeed / world.scale}
188-
if (heldKeys["KeyS"]) {camPos.y += camSpeed / world.scale}
185+
let camSpeed = 12
186+
if (heldKeys["ShiftLeft"]) {camSpeed *= 2}
187+
if (heldKeys["KeyW"]) {camPos.y += camSpeed / world.scale}
188+
if (heldKeys["KeyS"]) {camPos.y -= camSpeed / world.scale}
189189
if (heldKeys["KeyA"]) {camPos.x -= camSpeed / world.scale}
190190
if (heldKeys["KeyD"]) {camPos.x += camSpeed / world.scale}
191191
camPos.x = clamp(camPos.x, world.size.x/-2, world.size.x/2)
192192
camPos.y = clamp(camPos.y, world.size.y/-2, world.size.y/2)
193193
world.setOffset({
194-
x: world.offset.x + (-world.offset.x-camPos.x)/10,
195-
y: world.offset.y + (-world.offset.y-camPos.y)/10
194+
x: world.offset.x + (-world.offset.x-camPos.x)/5, // range 0-20 +1
195+
y: world.offset.y + (-world.offset.y+camPos.y)/5
196196
})
197197
canvas.style.width = window.innerWidth +"px"
198198
canvas.style.height = window.innerHeight +"px"
199199
world.render()
200+
201+
debugInfo.innerText = ""
202+
debugInfo.innerText += "Cam X Y: "+ camPos.x.toFixed(2) +" "+ camPos.y.toFixed(2) +"\n"
203+
debugInfo.innerText += "Cam zoom: "+ camZoom +" ("+ world.scale.toFixed(2) +")\n"
200204
}
201205

202206
setInterval(render, 10)

style.css

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@ body {
55
font-family: monospace;
66
color: white;
77
}
8+
.ui {
9+
position: absolute;
10+
z-index: 1;
11+
backdrop-filter: blur(5px);
12+
background-color: rgba(70, 130, 180, 0.5);
13+
border-radius: 5px;
14+
}
15+
#settings {
16+
top: 10px;
17+
left: 10px;
18+
width: 100px;
19+
height: 100px;
20+
}
21+
#debugInfo {
22+
top: 50%;
23+
background-color: unset;
24+
}
825
#canvas {
926
position: absolute;
1027
top: 0;
1128
left: 0;
12-
}
13-
#crosshair {
14-
width: 2px;
15-
height: 2px;
16-
background-color: white;
29+
z-index: 0;
1730
}

0 commit comments

Comments
 (0)