-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindexa.html
More file actions
81 lines (63 loc) · 2.1 KB
/
indexa.html
File metadata and controls
81 lines (63 loc) · 2.1 KB
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
<doctype html>
<head>
<title>Displaying an image with canvas</title>
</head>
<canvas width="319" height="160"></canvas>
<script >
// get a reference to the canvas
var canvas = document.querySelector("canvas");
// get a reference to the canvas's drawing surface
var drawingSurface = canvas.getContext("2d");
// create a monsterImage variable to hold the new image
var monsterImage = new Image();
// Add a listener to the image to call the render
// function when the image has loaded
monsterImage.addEventListener("load", loadHandler, false);
// Assign the image's src property to the image you want to load
monsterImage.src = "img/monsterStates.png";
function imageLoadHandler()
{
// Draw the monsterImage onto the drawingSurface
drawingSurface.drawImage(monsterImage, 0, 0);
}
// Before you can display anything in the canvas you have
// you have to access this surface with JavaScript
// Get a reference to the canvas HTML tag
var canvas = document.querySelector("canvas");
// Access the canvas's drawing surface
var drawingSurface = canvas.getContext("2d");
// drawing surface (blank piece of paper) is the same size as the canvas
// Create a new image object
var monsterImage = new Image(); // newImage is a built-in feature of JavaScript that creates a blank, invisible <img> tag directly on your program
// add a load event listener to the image. When it's finished loading, it will call the loadHandler
monsterImage.addEventListener("load", loadHandler, false);
monsterImage.src = "img/monsterStates.png";
// Call loadHandler when image is finished loading
function loadHandler()
{
drawingSurface.drawImage
(
monsterImage,
159, 0, 159, 159,
0, 0, 159, 159
);
}
// obj with 2 states
var monster =
{
//Define the monster's states
NORMAL: 0,
SCARED: 1,
//Set its initial state
state: 0
};
//Change the monster's state by pressing a key
window.addEventListener("keydown", keydownHandler, false);
function keydownHandler(event)
{
//Change the monster's state
monster.state = monster.SCARED;
//Display the new state
console.log("The monster's new state: " + monster.state);
}
</script>