-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathindex.html
66 lines (51 loc) · 1.66 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>screenX/screenTop and ScreenY/ScreenLeft example</title>
<style>
canvas {
display: block;
margin: 50px auto;
}
</style>
</head>
<body>
<p></p>
<canvas>
<p>Your browser doesn't support canvas. Boo hoo!</p>
</canvas>
<script>
const canvasElem = document.querySelector('canvas');
let width = canvasElem.width = 640;
let height = canvasElem.height = 480
let ctx = canvasElem.getContext('2d');
function degToRad(degrees) {
return degrees * Math.PI / 180;
};
const pElem = document.querySelector('p');
let initialTop, initialLeft;
if(!window.screenLeft) {
window.screenLeft = window.screenX;
window.screenTop = window.screenY;
}
initialLeft = window.screenLeft + canvasElem.offsetLeft;
initialTop = window.screenTop + canvasElem.offsetTop;
function positionElem() {
let newLeft = window.screenLeft + canvasElem.offsetLeft;
let newTop = window.screenTop + canvasElem.offsetTop;
let leftUpdate = initialLeft - newLeft;
let topUpdate = initialTop - newTop;
ctx.fillStyle = 'rgb(0, 0, 0)';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = 'rgb(0, 0, 255)';
ctx.beginPath();
ctx.arc(leftUpdate + (width/2), topUpdate + (height/2) + 35, 50, degToRad(0), degToRad(360), false);
ctx.fill();
pElem.textContent = 'Window.screenLeft: ' + window.screenLeft + ', Window.screenTop: ' + window.screenTop;
window.requestAnimationFrame(positionElem);
}
window.requestAnimationFrame(positionElem);
</script>
</body>
</html>