-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.html
121 lines (97 loc) · 2.97 KB
/
sandbox.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
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />
<title>Canvas Sandbox</title>
<style>
body {
color: white;
background-color: black;
text-align: center;
font-size: 16px;
}
#canvas {
display: block;
width: 320px;
height: 200px;
background-color: blueviolet;
margin: 0 auto;
}
@media screen and (min-width: 800px) {
body {
font-size: 32px;
}
#canvas {
width: 640px;
height: 400px;
}
}
@media screen and (min-width: 1120px) {
body {
font-size: 48px;
}
#canvas {
width: 960px;
height: 600px;
}
}
@media screen and (min-width: 1440px) {
body {
font-size: 64px;
}
#canvas {
width: 1280px;
height: 800px;
}
}
</style>
</head>
<body>
Canvas Sandbox
</body>
<script type="text/javascript">
let tempCanvasImage = new Image();
tempCanvasImage.src = 'bitmaps/village00.png';
const canvas = document.createElement('canvas');
const canvas2dContext = canvas.getContext('2d');
canvas.id = 'canvas';
canvas.width = 320;
canvas.height = 200;
document.body.appendChild(canvas);
document.body.onkeyup = function (e) {
if (e.key == '1') {
canvas2dContext.drawImage(tempCanvasImage, 0, 0, 320, 200, 0, 0, 320, 200);
}
if (e.key == '2') {
canvas2dContext.drawImage(tempCanvasImage, 0, 0, 160, 200, 0, 0, 320, 200);
}
if (e.key == '3') {
canvas2dContext.drawImage(tempCanvasImage, 0, 0, 160, 100, 0, 0, 320, 200);
}
if (e.key == '4') {
canvas2dContext.drawImage(tempCanvasImage, 0, 0, 320, 100, 0, 0, 320, 200);
}
if (e.key == '5') {
canvas2dContext.drawImage(tempCanvasImage, 0, 0, 320, 200, 0, 0, 160, 100);
}
}
canvas.onmousemove = function (e) {
console.log('squeep');
}
</script>
<script type="text/javascript">
function getMousePosition(canvas, event) {
let rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
console.log("Coordinate x: " + x,
"Coordinate y: " + y);
}
let canvasElem = document.querySelector("canvas");
canvasElem.addEventListener("mousedown", function (e) {
getMousePosition(canvasElem, e);
});
</script>
</html>
<!-- End of Document -->