Javascript Slideshow Code

Tags

, , , , , , ,

I prefer writing all my web-design code from scratch. So I use only text editors to write HTML directly, and raw JavaScript (no jQuery etc). While working on a javascript slideshow for Artarium, I came across a lot of problems in transitioning the images. My javascript was changing the src for the image, but I needed to also resize it to the dimensions of the new image before displaying it. Finally, after a lot of time, effort and fruitless migrations to online tutorials and troubleshooters, I came up with the exact configuration that works. On a transition the image disappears, resizes, then reappears. In between this, if the next image takes too long to load, you could display some loading animation, as I did. Just put an animated gif there permanently with a lower z-index than the photo. This will cause it to show between transitions. For the transitions itself you could use a CSS3 transition of opacity as I did and not have to worry about further code for transition effects.

Here is the JavaScript code with some explanatory comments. I have added a preloading function and keyboard navigation. I have not explained everything in detail as I have assumed the user will have standard web-designing experience, in which case this should very well suffice.

JavaScript:

function keyNavigate(e) //to enable slideshow navigation using right and left arrow keys
{
    if(e.which==39)
         pre_next();
    else if (e.which==37)
        pre_previous();
}

var i=0,imax=n; //put the # of slideshow images as n here

function imagearray() //prepares things on page load and starts preloading images
{
    preloader=new Image()
    var j=0;
    captions = new Array();
    captions = ['caption1', 'caption2', ... 'caption n'];
    document.photo.src="directory/photo1.jpg"; //assumes photos are in 'directory'
    document.getElementById('navigation-count').innerHTML="0/"+(imax); //sets slide number
    for(j=1; j    {
        filename="directory/photo"+j+".jpg"; //assumes photos are 'photo1.jpg', 'photo2.jpg' etc
        preloader.src=filename;
    }
}

var imgHeight;
var imgWidth;
var newImg;

function resize() //to resize as image changes
{
    imgHeight = this.height;
    imgWidth = this.width;
    if (imgWidth/imgHeight < 2.25) //any desired criterion
    {
        document.photo.style.height='355px'; //or whatever else
    }
    else
    {
        document.photo.style.width="95%";
    }
    document.photo.style.opacity=1; //photo appears only after it has been resized
    document.getElementById('caption').innerHTML=captions[i-1]; //caption changes
    document.getElementById('caption').style.opacity=1; //caption appears
    document.getElementById('navigation-count').innerHTML=(i)+"/"+(imax); //slide count changes
    return true;
}

function pre_next() //to ensure resizing occurs after picture disappears
{
    document.photo.style.opacity=0;
    document.getElementById('caption').style.opacity=0;
    setTimeout("next()",500);
}

function next()
{
    if (i==imax)
    {i=0;}
    i++;
    newImg=new Image();
    newImg.src="directory/photo"+i+".jpg";
    document.photo.src=newImg.src;
    newImg.onload = resize; //resize function is called
}

function pre_previous()
{
    document.photo.style.opacity=0;
    document.getElementById('caption').style.opacity=0;
    setTimeout("previous()",500);
}

function previous()
{
    if (i==1)
    {i=imax+1;}
    i--;
    newImg=new Image();
    newImg.src="directory/photo"+i+".jpg";
    document.photo.src=newImg.src;
    newImg.onload = resize;
}

This JavaScript alone does not suffice. Here’s some things you need to do with the HTML for this to work:

  1. The image element which changes in the slideshow should have a name=”photo” attribute (used on line 17 etc. of the JavaScript).
  2. Add <body onload = “imagearray(), next()”  onkeydown=”keyNavigate(event)”>  to the HTML body. The next() is required to display the first photo and initialize the caption and slide count.
  3. The slide transition occurs by pre_next() and pre_previous() functions. So any event that you want will trigger a transition should call these functions (as used in the keyboard navigation part), not next() or previous().
  4. The ‘caption’ div in the HTML holds the caption, while the ‘navigation-count’ div holds the slide number. Place them as you require.

You can find this code at work in any gallery at Artarium, unless I change it in the future. If you hit a block using this code, there’s nothing a couple of Google searches won’t clear for you. If there’s a problem that persists even after you have done your research thoroughly, leave a comment (be specific) with your e-mail and I promise to try to help.

Germany Through My Lens

Tags

, , , , , ,

At the DAAD1 Young Ambassador2 annual meet in Delhi, India, we had a special session for presenting our photos and videos from around Germany. The night before, I decided I’d do something slightly better than just opening up the photos and tapping the right arrow key as the room stares at the projector screen out of nicety. So I stayed up all night making a video out of photos and clips, and threw in some of my favourite uncommon music. As dawn was breaking, I went to the front desk of our hotel and asked if the swimming pool was open yet. It was plain from my looks that I hadn’t slept. The guy must have thought I was crazy. He said it opens from eight. So I went back and hit the bed. And got up an hour later for our program that day.

Anyway, everyone liked the video. At the end, it was replayed as a way of finishing the meet, so that was great. Ms. Christiane Schlottmann, head of DAAD India, said the music was cool. I had uploaded it to YouTube and today DAAD India uploaded it to their newly opened YouTube channel. So here it is from their channel, and I hope you like it!

Photoshop Tutorial: Add texture to flat image

Tags

, , ,

This will be a short Photoshop tutorial that will explain how you can make an image richer by adding a texture to it.

We start with this image that can be found on the internet:

HD Wallpapers of Violin - High Resolution Backgrounds of Guitar 3000 x 2000

Then we straighten and flip it in Photoshop using Image > Image Rotation options:

photoshop-1

There is a more or less flat backdrop to the guitar. We want to introduce a texture here. You can download textures from the internet, like this crumpled paper texture that I chose:

crumpled_paper_7_insight_designs

Insert it into the document as a layer, like so:

photoshop-2

Resize it so that it fills the entire area, in this case:

photoshop-3

Now comes the magic. Set the blend mode of this layer to overlay. You can experiment with some of the other modes too to get your desired result:

photoshop-4

This is the result:

photoshop-5

Not quite finished yet. The overlayed texture has fallen on the entire image, including the guitar. Although in this case it is not very conspicuous, I still prefer the smoother, softer original look of the guitar. Also, in other cases it might not do to have the texture sprawl over the entire image, but only lie over a specific area of it. For this we must remove the texture from the unneeded region. This we shall do via a layer mask.

Apply a layer mask to the texture layer by Layer > Layer Mask > Reveal All:

photoshop-6

Now choose a suitable brush:

photoshop-7

Paint with black over the mask of the texture layer. This will start removing the texture from the regions you paint over. Paint with white to restore the texture in that area. Toy around with the brush size, hardness and opacity; change brushes etc. until you are satisfied with the result:

photoshop-8

As you can see, some softness has been restored to the guitar and the surrounding background. Now you may, if you want, merge down the two layers by selecting the top layer and pressing Ctrl+E or choosing Layer > Merge Down and use it as a single layer or save as an image.

This essentially concludes the tutorial on adding a texture. However, I also wanted to increase the extent of this image upwards, filling the expanded region with only the textured background. For this we must first make a layer from the locked background (the original image) by double-clicking it. Then we change the canvas size by Image > Canvas Size:

photoshop-9

If we do not make a layer from the background before expanding the canvas, the new area will be white instead of transparent.

Then I duplicate our layer into a new one and shift that up. I flip it vertically and line it up with the top edge of the original layer below:

photoshop-10

We need to make the top guitar disappear. We do this using the Clone Stamp tool. Choosing a patch of the textured background beside the top guitar as the source, you can paint over the guitar. Again, fiddle with the size, hardness and opacity of the brush to smoothen out the changed region with its surroundings.

photoshop-11

Same goes for the region where the two edges are meeting, because the mirroring is apparent there:

photoshop-12

Here is the final image:

photoshop-tutorial

Share the knowledge and enjoy.

Unwritten Thoughts

Tags

, , ,

Last night I finished some work on a website I’ve been designing and watched a video lecture before tucking into bed. It was pretty late by then, around half past three, and I was very sleepy. But I think I got a small idea for a blog post (after a long time) and wanted to write it down before I either forgot or lost the enthusiasm the next day. So instead of turning off the laptop, I opened up the blog publishing program. I remember that well, although I was terribly sleepy and there was a film of clouded haze before my eyes, and I was forgetting whether I was asleep or awake.

I wrote down the title, I remember that. What happened next was pretty creepy, but I remember that too.

I collected my half-asleep mind with considerable difficulty in order to type down the formative idea for the post. Then, just as I was about to type it, just as my fingers hovered close above the keyboard, I heard this distinctive patter of the keys. Moderately fast typing, like my speed. I blinked and shook my head, but the haze did not clear. I could still see though that the text area was blank. I looked down and my fingers were still poised above the keys.

The soft sounds of the typing continued. I completely forgot what I was about to write.

I have been going to bed overworked, tired, and very late, a lot lately. So I stopped and tried to figure out whether all this was really happening, or I had smoothly passed from reality to a dream without significant changes in my surroundings. I was thinking that when I realized there was no sound. No typing noises. I sat up, rubbed my eyes and took stock of my surroundings. I was in my dark room under the sheets with a laptop on my lap, staring at a blank blog post. I remembered why I had it open. I remembered what I wanted to write down. So I thought of typing it down before I fell completely asleep and forgot the thought the next day.

Again, just as I was about to touch the keys, there started this soft patter over the keyboard. It wasn’t coming out of the speakers or anything, I was sure. It was coming from the keyboard, as if I was typing. But I wasn’t. I just sort of gave up then and lay back and let it be, much as one does after some shots of alcohol. I kept listening and did nothing. The typing continued. It was fast, and did not pause for long gaps, as if the mind was made. It continued, interjected by the slightly louder tap of the spacebar here and there. It was as if someone had wanted to write down a lot of things on their mind but had for long been unable to, and now took the opportunity of my attempt at a post to pour out their heart and mind. Only that it refused to show up on the screen. But it was as if I knew that someone, with that distinctive typing speed, that peculiar distribution of speeding up in some words and slowing down, pausing, thinking for a moment in others, that so familiar punching of the spacebar…

It was as if that someone was me, from some place else I had long neglected.

I kept listening to the typing in a general sleepy trance, trying a bit to guess from the source of the sounds which keys were being pressed, when there arose from inside me a faint feeling that I wasn’t asleep or imagining this.

I listened to it for a long time, gradually slipping into a deeper haze until I remember no more.

I woke up very late this morning. Afternoon, rather. The laptop was on my lap. It had turned itself off. I remembered last night. Before I did anything else, I booted the laptop and opened up the program.

Something had been written and saved last night. I opened it up and almost fell off my bed.

It was huge. I read through the whole thing for an hour. Then I sat silent for another hour afterwards, not believing, not being able to figure it out.

I don’t remember writing any of this. I don’t remember ever having thought of writing any of this. A person half-asleep, disoriented and hallucinating cannot write like this. But they were my thoughts, my own thoughts. My own wispy odds and ends that had flashed past the mind now and then and been carelessly shelved away for later reflection which I never had time for. Thoughts I had had on a bus, on the road, while looking at the face of a sad young girl in the metro, thoughts while listening to a song, thoughts about the nature of darkness and the lives of animals. Inconsequential thoughts, unfruitful thoughts, the smallest of thoughts. Picked and arranged and nurtured now into a long monologue before my eyes. Everything I had wanted to write about. Everything that is proper to write about, and everything improper.

I have not read it a second time since. I cannot put it in my blog. It cannot be read by any person except me. And the one thing I can swear on, is it couldn’t have been written by anyone except me.

I don’t know what to do with this. I don’t know if I am happy, or upset, or scared. I can’t decide. I don’t know how this happened. I need to find out. But I don’t know how. Who else can I ask? I have my doubts whether anyone else can help me.

I just know one thing. I’m going early to bed tonight.