blob: 6d9746f96a50c95db410e0357ced0c00df1f5d97 (
plain)
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
|
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
height: 100vh;
width: 100vw;
background-color: #161616;
}
body {
display: flex;
justify-content: center;
}
#target {
height: 20px;
width: 20px;
background-color: red;
position: relative;
}
#frame {
width: 75%;
height: 100vh;
border: 1px solid white;
}
</style>
</head>
<body>
<div id="frame">
</div>
<script>
const targetWidth = 20;
const targetHeight = 20;
let pageWidth = window.innerWidth;
let pageHeight = window.innerHeight;
const frameElement = document.getElementById("frame");
let frameHeight = frameElement.offsetHeight - targetHeight;
let frameWidth = frameElement.offsetWidth - targetWidth;
let target;
window.addEventListener("resize", (e) => {
this.pageWidth = window.innerWidth;
this.pageHeight = window.innerHeight;
this.frameHeight = frameElement.offsetHeight;
this.frameWidth = frameElement.offsetWidth;
console.log('page: ' + this.pageWidth + ' x ' + this.pageHeight);
console.log('frame: ' + this.frameWidth + ' x ' + this.frameHeight);
});
generateTarget();
// init logs:
console.log('page: ' + pageWidth + ' x ' + pageHeight);
console.log('frame: ' + frameWidth + ' x ' + frameHeight);
function generateTarget() {
const target = document.createElement("div");
target.id = "target";
target.style.top = getRandomInt(0, frameHeight) + 'px';
target.style.left = getRandomInt(0, frameWidth) + 'px';
console.log(target);
document.getElementById("frame").insertAdjacentElement("afterbegin", target);
}
function getRandomInt(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
}
</script>
</body>
</html>
|