-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas-slideshow.js
71 lines (62 loc) · 1.73 KB
/
canvas-slideshow.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// slideshow.js
//
// Slideshow of pictures
//
// C. David Sherrill, March 2018
//
window.onerror = function (msg, url, lineNo, colunmNo, error) {
var mainImage = document.getElementById("mainimage");
var ermsg = msg;
mainImage.innerHTML = ermsg;
return false;
}
window.onload = updateMainImage;
// var Slides = ["pics/pic1.jpg", "pics/pic2.jpg"]
// alert("Got this: " + Slides[0] + " " + Slides[1]);
var whichSlide;
// get the last slide number from localStorage
var lastSlide = localStorage.getItem('slideshow_lastslide');
if (lastSlide == null) {
whichSlide = Math.floor(Math.random()*Slides.length)-1;
}
else {
whichSlide = parseInt(lastSlide);
}
function updateMainImage() {
whichSlide++;
if (whichSlide >= Slides.length) {
whichSlide = 0;
}
var canvas = document.getElementById("mainimage");
var context = canvas.getContext("2d");
var theImage = new Image();
theImage.src = Slides[whichSlide];
theImage.onload = function() {
context.fillStyle = "black";
context.fillRect(0,0,canvas.width,canvas.height);
var nw = theImage.naturalWidth;
var nh = theImage.naturalHeight;
var cw = canvas.width;
var ch = canvas.height;
var sw = nw / cw;
var sh = nh / ch;
if (nh <= ch && nw <= cw) {
context.drawImage(theImage,(cw-nw)/2,(ch-nh)/2,nw,nh);
}
else { // need to scale something to fit
if (sw >= sh) {
fw = canvas.width;
fh = Math.floor(nh / sw);
context.drawImage(theImage,0,(ch-fh)/2,fw,fh);
}
else {
fh = canvas.height;
fw = Math.floor(nw / sh);
context.drawImage(theImage,(cw-fw)/2,0,fw,fh);
}
}
};
setTimeout(updateMainImage, 10000);
localStorage.setItem("slideshow_lastslide", whichSlide);
}