-
Notifications
You must be signed in to change notification settings - Fork 1
/
shift-test.html
123 lines (111 loc) · 3.05 KB
/
shift-test.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
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shifting an image using HTML canvas</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background: grey;
margin: 1em;
}
h1 {
font-size: 1.2em;
margin: 0 0 .5em 0;
}
h2 {
font-size: 1.1em;
margin: 0 0 .5em 0;
}
canvas {
background: green;
margin: 2em 0;
}
button {
font-family: inherit;
font-size: 1em;
background: mediumspringgreen;
border: none;
}
input[type=number] {
width: 3em;
}
div {
display: flex;
gap: 10px;
max-width: 25em;
padding-bottom: 10px;
}
button { flex: 1; }
label { flex: 3; }
input { flex: 2; }
</style>
</head>
<body>
<h1>Shifting an image using HTML canvas</h1>
<h2>Test page</h2>
<div>
<button id="down">Down</button>
<label for="yoffset">Vertical offset:</label>
<input type="number" size="3" min="0" value="1" id="yoffset">
<button id="up">Up</button>
</div>
<div>
<button id="left">Left</button>
<label for="xoffset">Horizontal offset:</label>
<input type="number" size="3" min="0" value="1" id="xoffset">
<button id="right">Right</button>
</div>
<div>
<button onclick="canvasshift(c, -10,0)">-10,0</button>
<button onclick="canvasshift(c, 0,-23)">0,-23</button>
<button onclick="canvasshift(right, 10,0)">broken</button>
<button onclick="canvasshift(c, 0,10)">0,10</button>
<button onclick="canvasshift(c, -10,10)">-10,10</button>
</div>
<script src="canvasshift.js"></script>
<script>
let c = document.createElement('canvas');
let cx = c.getContext('2d');
document.body.appendChild(c);
let i = new Image();
i.src = 'puking-unicorn.jpg';
i.onload = function() {
c.height = i.naturalHeight;
c.width = i.naturalWidth;
cx.drawImage(i, 0, 0);
}
const getnumbervalues = _ => {
canvasshift(
c,
+document.querySelector('#xoffset').value,
+document.querySelector('#yoffset').value
)
};
const navigate = e => {
let direction = e.target.id;
let h = +document.querySelector('#xoffset').value;
let v = +document.querySelector('#yoffset').value;
if (direction) {
switch (direction) {
case 'down': canvasshift(c, 0, v); break;
case 'up': canvasshift(c, 0, -v); break;
case 'left': canvasshift(c, -h, 0); break;
case 'right': canvasshift(c, h, 0); break;
}
}
};
let $ = x => { return document.querySelector(x) };
$('#xoffset').addEventListener('change',getnumbervalues)
$('#yoffset').addEventListener('change',getnumbervalues)
$('#up').addEventListener('click',navigate)
$('#down').addEventListener('click',navigate)
$('#left').addEventListener('click',navigate)
$('#right').addEventListener('click',navigate)
// hardcore test
// c.width = c.height = 100;
// cx.fillRect(30,30,50,50);
</script>
</body>
</html>