A place to cache linked articles (think custom and personal wayback machine)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

title: Space.js url: http://www.slashie.org/articles/space.js/ hash_url: 6545585160

Creating content on the Internet is not just about the contents. It’s about reaching out with a message, telling a story, trying to translate an emotion into bits and communicating them to the user, efficiently.

I wanted the end user to feel a stronger connection to my content. I feel that the medium is often overlooked, sure - the message is the core, but what’s wrong with silver platters? I love mediums that help narrate the experience. The way an old mechanical Polaroid cameras printed picture feels in your hands, or perhaps the note produced from the purposeful humming sounds of a typewriter. Imagine holding an old hardcover book, it doesn’t present you information, it tells you its story, it tells you it’s secrets. Medium is important.

Both

We generally use web pages as our canvases and paint content on the x– and y-axises. This is our tangible abstraction of reality. Here we are going to explore what happens when we introduce z-axis to content presentation. Instead of stripping down reality to a plane, we are going to simplify it, keeping our three physical dimensions but only their minimal essence.

For our messages to communicate across efficiently, we need to create a powerful connection between the user and our medium. Today we are going to explore a new way of presenting stories on the web. And for this I’ve created an open-source and free to use JavaScript library i call space.js. It’s basically parallax technologies used for narration. Think of it as a bicycle for content presentation.

Now would be a great time to head over to the demos to try it out for yourself!

Using the library

The library is HTML-driven, which means that you don’t need to write a single line of JavaScript to use it on your site and still have a lot of flexibility!

The core of the library is to divide our HTML into frames, or space-frames as we call them her (to not conflict the common class name “frame”).

Creating a frame

<div class="space-frame">[contents]</div>

I would also strongly recommend using an inner-frame inside the space-frame, which provides some helpful CSS to make things centered both vertically and horizontally inside the frame.

<div class="space-frame">
    <section class="space-inner-frame">
        [contents]
    </section>
</div>

Customize Duration

If we want we can provide a custom duration for our frames with the data-duration attribute, which multiplies the default duration of the transition.

<section class="space-frame" data-duration="1.4">...</section>
<section class="space-frame" data-duration="0.6">...</section>

Customize transition

Space.js has a default default transition - which is to enter by fading in and exit by scaling up and fading out. We can also provide a custom transition override to the library from predefined transitions. (We can also create our own transitions from scratch, but we’ll get to that later.)

<section class="space-frame" data-transition="rotate360">...</section>

Multiple values are supported!

<section class="space-frame" data-transition="rotate360 fadeOut slideInLeft">...</section>

Custom entry and exit

If we really want to get into detail, we can provide how we wish the frame to enter (first half of the frame duration) and exit (second half).

<section class="space-frame" data-enter="fadeIn" data-exit="fadeOut zoomOut">...</section>

What a complete frame could look like

<div class="space-frame" data-enter="fadeIn" data-exit="zoomOut fadeOut" data-duration="1.3">
    <section class="space-inner-frame">
        <div style="background-image:url(img/splash.png); padding:150px 200px;" class="bg">
            <section>
                <p>Demo 1</p>
                <h1>The Gallery</h1>
            </section>
        </div>
    </section>
</div>

Full API on GitHub

Behind the scenes.

The process of the library is pretty straight forward. When we have divided our HTML into frames, we assume each frame to initially cover the height and width of the window, which is equivalent to data-duration="1". Each frame is set to have the same scroll-duration as it would have if we would’ve normally scrolled past it. So we measure how high the view port of the users browser window is let’s say 800px, then we count the number of frames there are in the HTML, let’s say 20. To simulate a proper scrolling mechanism the body-height is set to #frames their height, in this case 800px 20 frames = 16000px.

We then hide all frames. Because of superior performance advantages when animating a position: fixed; object, we need to keep them fixed, which means we have to hide all frames that should not be showing at a particular height in the document.

The function scrollTop() tells us how far we are from the top of the page. We can use it to check what frame should be active and at what stage that frames transition is in. Instead of scrolling past an element we change its CSS according to when it should be visible, calculated from the value we get from scrollTop().

We then create a listener for the for the scroll event on the document using the ScrollController provided in the library. Which accepts a method as an argument, and when we receive the scroll event it keeps running that method. In this case the method is our animate(), which animates our frames. The ScrollController will then run the animation method at an interval of something around 60 fps while we are scrolling to not overload client. It’s also of most importance that we use requestAnimationFrame(...) to make sure our renderings are synced up with the clients browser, otherwise we might lose a lot of performance, which is of essence here.

The animate() method does two things.

  1. Calculate and set the current frame through a simple comparison of how far we have scrolled (scrollTop()) and which frame should be showing at that particular time.
  2. See how far we have scrolled into the current frame, and calculate all delta values of the transition based on that. E.g. if we fade something in; 'opacity':{from:0, to:1} and have scrolled 36% into of that element, it would be given the opacity 0.36.

The framework uses a linear easing algorithm for the transition, as per much experimentation, this is what feels most natural when the user controls the whole transition speed with his or her fingers. After all, why should the content move at different speeds when your motion remains constant?

Transitions

I love being able to construct as much as possible through the HTML. That’s why I made sure there are pre-defined transitions in the library, so that they can be accessed as keys from the data-transition, data-enter and data-exit attributes in the HTML. It seems the most efficient properties to transition on are opacity, scale, translate3d and rotation.

Basically they look like this:

zoomOut:{
    'scale': {from: 1, to: 0}
},
slideInBottom: {
    'translate3d':{
        from:{y:700},
        to: {y:0}
    }
},
slideOutDown: {
    'translate3d':{
        from:{y:0},
        to: {y:700}
    }
},
rotate3dOut: {
    'rotate3d':{
        from:{x:0, y:0, z:0, angle:0},
        to: {x:1, y:-1,z:0, angle:90}
    }
}

You can add your own transitions with the `addTransitions method. Make sure it is done after the library is loaded.

<script src="space.js"></script>

<script type="text/javascript">
    var transitions = {
        rotate720: {
            'rotate':{from:0, to:720}
        },
        fadeOutHalf: {
            'opacity':{from:1, to:0.5}
        }
    };
    Space.addTransitions(transitions);
</script>

Check out the full source code for more details!

Demos

Last words

Note that this is not intended to be used everywhere. Every type of content has a preferred medium. I mainly wanted to inspire you to not feel conformed to the two-dimensional world of the Internet.

Hope you’re feeling inspired! Now tell us your story!

Thanks for reading! Let me know if you want me to elaborate on something!