A place to cache linked articles (think custom and personal wayback machine)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 5 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. title: Space.js
  2. url: http://www.slashie.org/articles/space.js/
  3. hash_url: 6545585160a15ef6eef8b7e9319a5cf3
  4. <p>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.</p>
  5. <p>I wanted the end user to feel a stronger connection to my content. I feel that the <em>medium</em> is often overlooked, sure - the message is the <em>core</em>, 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.</p>
  6. <p><img src="http://www.slashie.org/articles/space.js/img/camera1.jpg" alt="Both"/></p>
  7. <p>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.</p>
  8. <p><img src="http://www.slashie.org/articles/space.js/img/axisinformation.png" alt=""/></p>
  9. <p>For our messages to communicate across <strong>efficiently</strong>, <em>we need to create a powerful connection between the user and our medium</em>. 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 <em>space.js</em>. It’s basically parallax technologies used for narration. Think of it as a bicycle for content presentation.</p>
  10. <p><img src="http://www.slashie.org/articles/space.js/img/spacejs.png" alt=""/></p>
  11. <p>Now would be a great time to head over to <a href="http://www.slashie.org/space.js"><strong>the demos</strong></a> to try it out for yourself!</p>
  12. <p><a href="http://www.slashie.org/space.js"><img src="http://www.slashie.org/articles/space.js/img/demosthumb.jpg" alt=""/></a></p>
  13. <h2 id="using-the-library">Using the library</h2>
  14. <p>The library is <span class="caps">HTML</span>-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!</p>
  15. <p>The core of the library is to divide our <span class="caps">HTML</span> into frames, or <em>space-frames</em> as we call them her (to not conflict the common class name “frame”).</p>
  16. <h3 id="creating-a-frame">Creating a frame</h3>
  17. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span>&gt;</span>[contents]<span class="tag">&lt;/<span class="title">div</span>&gt;</span>
  18. </code></pre>
  19. <p>I would also <strong>strongly</strong> recommend using an inner-frame inside the space-frame, which provides some helpful <span class="caps">CSS</span> to make things centered both vertically and horizontally inside the frame.</p>
  20. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span>&gt;</span>
  21. <span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-inner-frame"</span>&gt;</span>
  22. [contents]
  23. <span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  24. <span class="tag">&lt;/<span class="title">div</span>&gt;</span>
  25. </code></pre>
  26. <h3 id="customize-duration">Customize Duration</h3>
  27. <p>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.</p>
  28. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-duration</span>=<span class="value">"1.4"</span>&gt;</span>...<span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  29. <span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-duration</span>=<span class="value">"0.6"</span>&gt;</span>...<span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  30. </code></pre>
  31. <h3 id="customize-transition">Customize transition</h3>
  32. <p>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.)</p>
  33. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-transition</span>=<span class="value">"rotate360"</span>&gt;</span>...<span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  34. </code></pre>
  35. <p>Multiple values are supported!</p>
  36. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-transition</span>=<span class="value">"rotate360 fadeOut slideInLeft"</span>&gt;</span>...<span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  37. </code></pre>
  38. <h3 id="custom-entry-and-exit">Custom entry and exit</h3>
  39. <p>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).</p>
  40. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-enter</span>=<span class="value">"fadeIn"</span> <span class="attribute">data-exit</span>=<span class="value">"fadeOut zoomOut"</span>&gt;</span>...<span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  41. </code></pre>
  42. <h2 id="what-a-complete-frame-could-look-like">What a complete frame could look like</h2>
  43. <pre><code class="lang-html"><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"space-frame"</span> <span class="attribute">data-enter</span>=<span class="value">"fadeIn"</span> <span class="attribute">data-exit</span>=<span class="value">"zoomOut fadeOut"</span> <span class="attribute">data-duration</span>=<span class="value">"1.3"</span>&gt;</span>
  44. <span class="tag">&lt;<span class="title">section</span> <span class="attribute">class</span>=<span class="value">"space-inner-frame"</span>&gt;</span>
  45. <span class="tag">&lt;<span class="title">div</span> <span class="attribute">style</span>=<span class="value">"background-image:url(img/splash.png); padding:150px 200px;"</span> <span class="attribute">class</span>=<span class="value">"bg"</span>&gt;</span>
  46. <span class="tag">&lt;<span class="title">section</span>&gt;</span>
  47. <span class="tag">&lt;<span class="title">p</span>&gt;</span>Demo 1<span class="tag">&lt;/<span class="title">p</span>&gt;</span>
  48. <span class="tag">&lt;<span class="title">h1</span>&gt;</span>The Gallery<span class="tag">&lt;/<span class="title">h1</span>&gt;</span>
  49. <span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  50. <span class="tag">&lt;/<span class="title">div</span>&gt;</span>
  51. <span class="tag">&lt;/<span class="title">section</span>&gt;</span>
  52. <span class="tag">&lt;/<span class="title">div</span>&gt;</span>
  53. </code></pre>
  54. <p><a href="https://github.com/gopatrik/space.js">Full <span class="caps">API</span> on GitHub</a></p>
  55. <h2 id="behind-the-scenes-">Behind the scenes.</h2>
  56. <p>The process of the library is pretty straight forward. When we have divided our <span class="caps">HTML</span> into frames, we assume each frame to initially cover the height and width of the window, which is equivalent to <code>data-duration="1"</code>. 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 <span class="caps">HTML</span>, let’s say 20. To simulate a proper scrolling mechanism the body-height is set to #frames <em> their height, in this case 800px </em> 20 frames = 16000px.</p>
  57. <p>We then hide all frames. Because of superior performance advantages when animating a <code>position: fixed;</code> 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.</p>
  58. <p>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 <em><span class="caps">CSS</span></em> according to when it <em>should be visible</em>, calculated from the value we get from scrollTop().</p>
  59. <p>We then create a listener for the for the scroll event on the document using the <code>ScrollController</code> 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 <em>method</em> 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 <strong>most importance</strong> that we use <code>requestAnimationFrame(...)</code> 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.</p>
  60. <p>The <code>animate()</code> method does <strong>two things</strong>.</p>
  61. <ol>
  62. <li>Calculate and set the <em>current frame</em> through a simple comparison of how far we have scrolled (<code>scrollTop()</code>) and which frame should be showing at that particular time.</li>
  63. <li>See how far we have scrolled into the <em>current frame</em>, and calculate all delta values of the transition based on that. E.g. if we fade something in; <code>'opacity':{from:0, to:1}</code> and have scrolled 36% into of that element, it would be given the opacity 0.36.</li>
  64. </ol>
  65. <p>The framework uses a <strong>linear easing</strong> algorithm for the transition, as per much experimentation, this is what feels most natural when the user <em>controls</em> the whole transition speed with his or her fingers. After all, why should the content move at different speeds when your motion remains constant?</p>
  66. <h3 id="transitions">Transitions</h3>
  67. <p>I love being able to construct as much as possible through the <span class="caps">HTML</span>. That’s why I made sure there are pre-defined transitions in the library, so that they can be accessed as keys from the <code>data-transition</code>, <code>data-enter</code> and <code>data-exit</code> attributes in the <span class="caps">HTML</span>. It seems the most efficient properties to transition on are <strong>opacity</strong>, <strong>scale</strong>, <strong>translate3d</strong> and <strong>rotation</strong>.</p>
  68. <p>Basically they look like this:</p>
  69. <pre><code class="lang-javascript">zoomOut:{
  70. <span class="string">'scale'</span>: {from: <span class="number">1</span>, to: <span class="number">0</span>}
  71. },
  72. slideInBottom: {
  73. <span class="string">'translate3d'</span>:{
  74. from:{y:<span class="number">700</span>},
  75. to: {y:<span class="number">0</span>}
  76. }
  77. },
  78. slideOutDown: {
  79. <span class="string">'translate3d'</span>:{
  80. from:{y:<span class="number">0</span>},
  81. to: {y:<span class="number">700</span>}
  82. }
  83. },
  84. rotate3dOut: {
  85. <span class="string">'rotate3d'</span>:{
  86. from:{x:<span class="number">0</span>, y:<span class="number">0</span>, z:<span class="number">0</span>, angle:<span class="number">0</span>},
  87. to: {x:<span class="number">1</span>, y:-<span class="number">1</span>,z:<span class="number">0</span>, angle:<span class="number">90</span>}
  88. }
  89. }
  90. </code></pre>
  91. <p>You can add your own transitions with the <code>`addTransitions</code> method. Make sure it is done after the library is loaded.</p>
  92. <pre><code class="lang-javascript">&lt;script src=<span class="string">"space.js"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="title">script</span>&gt;</span>
  93. <span class="tag">&lt;<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span>&gt;</span><span class="javascript">
  94. <span class="keyword">var</span> transitions = {
  95. rotate720: {
  96. <span class="string">'rotate'</span>:{from:<span class="number">0</span>, to:<span class="number">720</span>}
  97. },
  98. fadeOutHalf: {
  99. <span class="string">'opacity'</span>:{from:<span class="number">1</span>, to:<span class="number">0.5</span>}
  100. }
  101. };
  102. Space.addTransitions(transitions);
  103. </span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></span>
  104. </code></pre>
  105. <p>Check out the full <a href="https://github.com/gopatrik/space.js">source code</a> for more details!</p>
  106. <h2 id="demos">Demos</h2>
  107. <p><a href="http://www.slashie.org/space.js"><img src="http://www.slashie.org/articles/space.js/img/demosthumb.jpg" alt=""/></a></p>
  108. <h2 id="last-words">Last words</h2>
  109. <p>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.</p>
  110. <p>Hope you’re feeling inspired! Now tell us your story!</p>
  111. <p><strong>Thanks for reading!</strong> Let me know if you want me to elaborate on something!</p>