-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
51 lines (36 loc) · 1.46 KB
/
script.js
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
//the array with the path to the images
var pathToYourImages = ["pic1.png", "pic2.png", "pic3.png", "pic4.png"];
//a variable to hold the canvas element
var canvas = null;
//a variable to hold the canvas context
var contextForCanvas = null;
//a variable for the image element to be draw
var image = null;
//a variable to control which image to be displayed.
var currentImage = 0;
//when the wondow.onload happen, assing the value to the variables and then call ImageSliding.
window.onload = function() {
canvas = document.getElementById('theCanvas');
contextForCanvas = canvas.getContext('2d');
image = document.createElement("img");
ImageSliding();
};
//Performs the change on the image path and controls which will be showed
function ImageSliding() {
//sets the image's path
image.setAttribute('src', pathToYourImages[currentImage]);
//control which image is being used
currentImage = currentImage === (pathToYourImages.length - 1) ? 0 : ++currentImage;
//sets the transparency value
contextForCanvas.globalAlpha = 0.1;
//calls the timeout function
setTimeout(ShowImage, 200);
}
//clears the canvas and show the new image
function ShowImage() {
contextForCanvas.save();
contextForCanvas.clearRect(0, 0, canvas.width, canvas.height);
contextForCanvas.drawImage(image, 0, 0, 300, 150);
contextForCanvas.globalAlpha += 0.1;
contextForCanvas.globalAlpha > 0.9 ? setTimeout(ImageSliding, 3500) : setTimeout(ShowImage, 200);
}