Click here to Skip to main content
Click here to Skip to main content

GoSlideshow HTML5 Canvas

By , 17 May 2012
 

Introduction

GoSlideshow is a simple awesome image slider, created for each web browser that supports HTML5, even in smartphones.

The Code

Images:

var imagePaths = ["images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg"];    
Declaration:
var showCanvas = null;
var showCanvasCtx = null;
var img = document.createElement("img");
var currentImage = 0;
var revealTimer;

window.onload = function () {
                showCanvas = document.getElementById('GoSlideshow');
                showCanvasCtx = showCanvas.getContext('2d');
                
                img.setAttribute('width','600');
                img.setAttribute('height','400');
                switchImage();
                
                setInterval(switchImage,3000);
            } 

Functions:

            function switchImage() {
                img.setAttribute('src',imagePaths[currentImage++]);
                if (currentImage >= imagePaths.length)
                    currentImage = 0;
                
                showCanvasCtx.globalAlpha = 0.1;
                revealTimer = setInterval(revealImage,100);
            }
            
            function revealImage() {
                showCanvasCtx.save();
                showCanvasCtx.drawImage(img,0,0,600,400);
                showCanvasCtx.globalAlpha += 0.1;
                if (showCanvasCtx.globalAlpha >= 1.0)
                    clearInterval(revealTimer);
                showCanvasCtx.restore();
            } 

Points of Interest

Html5 Canvas Experiments

History

Version 1.6

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)

About the Author

Agon Avdimetaj
CEO Graphsix Studio
Albania Albania
Member
CEO / Web & App Developer @ Graphsix Studio.
Studying Computer Engineering @ Faculty of Electrical and Computer Engineering, UP.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberEd Nutting26 Aug '12 - 14:31 
This needs so much more explanation of how it works and how to use it properly for it to be actually helpful. A screenshot or video of a demo would also be good to see.
 
Ed
GeneralMy vote of 1memberKentEVela10 Jul '12 - 8:52 
Not really an article.
GeneralMy vote of 1mvpJani Giannoudis13 Jun '12 - 23:22 
Not an article. At best a tip/trick
GeneralMy vote of 1memberPranay Rana13 Jun '12 - 0:24 
Please explain in detail rather than just showing code...
Question[My vote of 1] This is not an article [modified]mvpSacha Barber11 Jun '12 - 6:16 
This talks very little about Html Canvas, and shows nothing that could not be done with 2 images a bit of jQuery and a simple loop, and a Fade/Show. Job done.
 
I find it very weird that this is "The BEST" web article this month. This is no where near a finished "Article"
Sacha Barber
  • Microsoft Visual C# MVP 2008-2012
  • Codeproject MVP 2008-2012
Open Source Projects
Cinch SL/WPF MVVM

Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net


modified 12 Jun '12 - 8:27.

GeneralMy vote of 1memberryanthemadone11 Jun '12 - 5:35 
This isn't an article, not only that there's no way it deserves to be beating the actual articles in this category.
 
This shouldn't have got through the editorial process.
 
Where are the editors?
GeneralMy vote of 5memberfrazzle-me10 Jun '12 - 8:07 
Nice get's my 5
GeneralMy vote of 5membercatcoptor9 Jun '12 - 2:13 
nice and smooth coding
BugBugs + FixmemberVitaly Tomilov22 May '12 - 9:43 
As I wrote earlier, the code here doesn't really work, it results in overlapping timers, memory leaks and eventually crashes javascript.
 
Below is one version that I changed quite a bit to make it at least work as expected, without losing the fading effect.
 
Note that I also deleted such thing as showCanvasCtx.restore(), because it would reset the transparency level. If this omission isn't critical, then this can be the solution.
 
P.S. It is generally a bad idea using multiple timers with setInterval while timers depend on each other. Instead, one should use setTimeout in such cases, which is exactly what I did in this code, beside other changes.
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>GoSlideshow HTML5 Canvas</title>
		<script type="text/javascript">
			var imagePaths = [
				"images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg"
			];
			var showCanvas = null;
			var showCanvasCtx = null;
			var img = document.createElement("img");
			var currentImage = 0;
 
			window.onload = function () {
				showCanvas = document.getElementById('GoSlideshow');
				showCanvasCtx = showCanvas.getContext('2d');
				
				img.setAttribute('width','600');
				img.setAttribute('height','400');
				switchImage();			
			}
			
			function switchImage() {
				img.setAttribute('src',imagePaths[currentImage++]);
				if (currentImage >= imagePaths.length)
					currentImage = 0;
				
				showCanvasCtx.globalAlpha = 0.1;
				setTimeout(revealImage, 100);
			}
			
			function revealImage() {
				showCanvasCtx.save();
				showCanvasCtx.drawImage(img,0,0,600,400);
				showCanvasCtx.globalAlpha += 0.1;
				if (showCanvasCtx.globalAlpha > 0.9)
					setTimeout(switchImage,3000);
				else
					setTimeout(revealImage, 100);
			}
		</script>
	</head>
	<body>
		<canvas id='GoSlideshow' width='600' height='400'>
			Canvas Not Supported
		</canvas>
	</body>
</html>
 

GeneralMy vote of 5membereddy morrison22 May '12 - 2:15 
like it
GeneralMy vote of 5memberTRIInc21 May '12 - 10:53 
Simple and straight forward.
GeneralRe: My vote of 5memberVitaly Tomilov22 May '12 - 8:34 
really, except it doesn't work as expected, and leaks memory (see my earlier post) Wink | ;)
GeneralMy vote of 5memberAkram El Assas21 May '12 - 1:36 
Nice animation.
GeneralMy vote of 4memberVitaly Tomilov20 May '12 - 12:13 
Although it offers clear and concise idea and implementation, the problems are:
 
1. This is hardly an article;
2. There is an obvious bug in implementation. The fading effect does not last. Try any browser (I used FF-12 and Chrome-19). Open the page and leave it running for about 1.5 minute - you will see the transition effect is now gone, the images change instantaneously.
GeneralRe: My vote of 4memberMember 767909622 May '12 - 5:09 
That's right! Do you have any idea how to fix it?
GeneralquiteRe: My vote of 4 [modified]memberVitaly Tomilov22 May '12 - 8:30 
I'll give you a hint: Setting value into showCanvasCtx.globalAlpha doesn't quite work the way that you expect. It does not increment value as you think, because there is a time effect to it whenever it changes, i.e. increment for such variable takes time for adjusting the canvas, and you are not considering it, treating it as a regular variable, which it is not Wink | ;)
 
As another side effect, the following line is never executed:

clearInterval(revealTimer);

 
Because of this, you end up with unused timers and serious memory leaks Wink | ;)
 
And then again, because previous timers aren't released, you end up with overlapping timers, building on each other, tearing your JavaScript engine apart in the end, and, after a while, crashing it.

modified 22 May '12 - 14:38.

GeneralRe: My vote of 4memberVitaly Tomilov22 May '12 - 9:44 
P.S. I have posted a solution there at the top, to make it more visible Wink | ;)
GeneralRe: My vote of 4memberMember 767909623 May '12 - 0:43 
Thank you very much!!
GeneralRe: My vote of 4memberVitaly Tomilov23 May '12 - 11:21 
There is a 'Rate' option on each message (in the right bottom of each message), if you want to thank or boo somebody Wink | ;)
QuestionMy vote For Thismemberonurag1917 May '12 - 21:11 
Thumbs Up | :thumbsup:
GeneralMy vote of 5memberonurag1917 May '12 - 21:11 
nice
GeneralMy vote of 5memberruddyl17 May '12 - 16:10 
Nice,simple & easy!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 17 May 2012
Article Copyright 2012 by Agon Avdimetaj
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid