-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanevas.html
69 lines (58 loc) · 1.8 KB
/
canevas.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
<!DOCTYPE html>
<html>
<body>
<canvas id="svs" width="700" height="400"></canvas>
<div>Use Keyboard to move rectangle</div>
<br />
<button>↑</button> Move Up
<br />
<button>↓</button> Move Down
<br />
<button>←</button> Move Left
<br />
<button>→</button> Move Right
<script>
var stage = document.getElementById('svs'), // Get the canvas element by Id
ctx = stage.getContext('2d'), // Canvas 2d rendering context
x = 10, //intial horizontal position of drawn rectangle
y = 10, //intial vertical position of drawn rectangle
wid = 40, //width of the drawn rectangle
hei = 40; //height of the drawn rectangle
//Draw Rectangle function
function drawRect(x,y,wid,hei) {
ctx.fillStyle = 'red'; // Fill color of rectangle drawn
ctx.fillRect(x, y, wid, hei); //This will draw a rectangle of 20x20
}
drawRect(x,y,wid,hei); //Drawing rectangle on initial load
//move rectangle inside the canvas using arrow keys
window.onkeydown = function(event) {
var keyPr = event.keyCode; //Key code of key pressed
if(keyPr === 39 && x<=645){
x = x+20; //right arrow add 20 from current
}
else if(keyPr === 37 && x>10){
x = x-20; //left arrow subtract 20 from current
}
else if(keyPr === 38 && y>10) {
y = y-20; //top arrow subtract 20 from current
}
else if(keyPr === 40 && y<=340){
y = y+20; //bottom arrow add 20 from current
}
/*clearing anything drawn on canvas
*comment this below do draw path */
ctx.clearRect(0,0, 800, 800);
//Drawing rectangle at new position
drawRect(x,y,wid,hei);
};
</script>
<style>
#svs {
border: 1px solid #333;
position: relative;
top: 100px;
left:330px;
}
</style>
</body>
</html>