An idea to help spark some ideas for your projects using random number generation and a fading mask.
In this project I have a little fun with some random number generation paired up with some revealing mask effect that gives a glimpse of what is underneath.
Let’s take a closer look at how this was done.
The Elements
This project is made up of 14 items.
- The ‘Reveal Next’ button
- Twelve white boxes
- The image under the boxes
There is a single variable that I named nextBox to track which box will fade to reveal a portion of the hidden image.
The Code
All of the code is on the button and it is pretty repetitive. Since there are 12 boxes – we have similar blocks of code for each box.
Let’s break it down in small chunks.
function getNextBox () {
nextBox=Math.floor(Math.random() * 12) + 1;
cp.disable(“getNextBtn”);
}
This first block of code defines a function that I have called getNextBox. It does two things:
- Assigns a random number from 1 thru 12 to our nextBox variable.
- Disables the button you just pressed
I chose to disable the button so that you could not cheat by clicking the button super fast to get a better reveal of the image underneath.
getNextBox ();
The next small line simply calls the function we defined.
if (window.nextBox==1) {
reveal1();
}
Then we have 12 if statements – one for each of the 12 boxes. Basically – if the random generator assigns a 1 – we call the reveal1() function. If it assigns a 12 we call the reveal12() function.
Then we have the 12 functions themselves that get called based on the random number assignment.
function reveal1() {
setTimeout(function() {
$(“#box1c”).fadeOut(500);
$(“#box1c”).fadeIn(500);
}, 50);setTimeout(function() {
cp.enable(“getNextBtn”);
}, 1000);
}
So what we are doing is fading out the numbered box and fading it back in over the course of one second. Then you will notice that we immediately enable the button again so that we can call the next box reveal.
Some Fun Ideas
So think about what you could do with this sort of idea.
- Maybe some sort of guess the image game with the fewest number of reveals. You could easily add another variable to track the number times you hit that button.
- Maybe use this in reverse with a shown image and small circles that fade in and out to reveal a clue on an image like some of those hidden picture games. You could use that to help someone find all the safety hazards in the picture and track how many times they use a hint.
- Maybe use two pictures with missing things where the learner has to spot the differences and you could have the different elements fade in and out as clues.
Share some ideas you have in the comments and ask any questions that you might have.
…
Thanks once again Greg !…
It was a real struggle to make it work as I expected… Javascript is definitely not my friend !!!…
But… As I was about to cry for help, I finally manage to make it work !…
Always the same problem with ” _ ” when we copy some codes !!!…
And the Javascript panel in Captivate doesn’t help… at all !!!…
(Need to go to my Notepad++)
Is there a way to discover different boxes each time, and avoid the same one to discover many times ???…
I will post my projects soon !…
😉
…
Refactor idea. Will this work?
function reveal(bn) {
var whatBox = “#box” + bn.toString();
setTimeout(function() {
$(whatBox).fadeOut(500);
$(whatBox).fadeIn(500);
}, 50);
setTimeout(function() {
cp.enable(“getNextBtn”);
}, 1000);
}
reveal(window.nextBox);
Looks like we’re basically variable-izing the reveal functions. (I may have just invented a word) LOL.
Good idea. I will need to attempt that. I sometimes struggle between using short code or keeping actions separate but here is a case where it would be nice to shorten it up.
At first glance, the whatBox variable would need to also have that c appended so we get box1c, box2c, etc.
You have a lot of fun these days….
Random numbers in any situation needs the use of JS . I have created several examples. Here is one link, where I use random numbers with Timeline and Effects:
You must be logged in to post a comment.