HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<html> <head> <script type="text/javascript" src="cvi_reflex_lib.js"></script> <script type="text/javascript" src="coverflow.js"></script> <script type="text/javascript" src="lib/prototype.js"></script> <script type="text/javascript" src="src/scriptaculous.js"></script> <title>Coverflow</title> <script> var coverflow; function load(){ coverflow = new Coverflow("tmp"); coverflow.add("http://gallery.mac.com/devon.govett/100032/50970014.jpg?derivative=medium&source=web.jpg&type=medium&ver=12124007550006","Sunset", "In Oregon"); coverflow.add("http://gallery.mac.com/devon.govett/100032/50960013.jpg?derivative=medium&source=web.jpg&type=medium&ver=12124010000005","Snowy", "In Oregon"); coverflow.add("http://gallery.mac.com/devon.govett/100032/50970016_2.jpg?derivative=medium&source=web.jpg&type=medium&ver=12124012900006","Another Sunset", "In Oregon"); coverflow.add("http://www.yesterland.com/images-sidetrips/flower2007_floating.jpg","Flower", "Somewhere"); coverflow.render(); } </script> </head> <body onload="load()" bgcolor="black"> <div id="tmp"> </div> </body> </html>
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
function Coverflow(parent){ var values = new Array(); this.covers = new Array(); this.titles = new Array(); this.descriptions = new Array(); // Space between covers this.cover_spacing = 40; // Space from top for center cover this.top_space = 40; this.centerItem = 0; this.oldCenter = this.centerItem; // Space for items, left and right of center, from top this.leftright_top_space = 30; // Space between center and 3d covers this.center_space=150; this.hookEvent = function(element, eventName, callback) { if(typeof(element) == "string") element = document.getElementById(element); if(element == null) return; if(element.addEventListener) { element.addEventListener(eventName, callback, false); } else if(element.attachEvent) element.attachEvent("on" + eventName, callback); } var m = this; this.MouseWheel = function(event) { var delta = 0; if (!event) event = window.event; if (event.wheelDelta) { delta = event.wheelDelta / 120; } else if (event.detail) { delta = -event.detail / 3; } if (delta){ if(delta > 0){ if(m.centerItem-1 >= 0){ m.slider.setValue(values[m.centerItem-=1]); } }else{ if(m.centerItem+1 < m.covers.length){ m.slider.setValue(values[m.centerItem+=1]); } } } if (event.preventDefault) event.preventDefault(); event.returnValue = false; } //Parent DIV element this.parent = document.getElementById(parent); this.hookEvent(window, 'DOMMouseScroll', this.MouseWheel); this.hookEvent(document, 'mousewheel', this.MouseWheel); //Adds an image to the coverflow this.add = function(image, title, description){ var cover = document.createElement("img"); cover.src = image; cover.width = 200; cover.id = "cover "+this.covers.length; this.parent.appendChild(cover); this.covers[this.covers.length] = cover; this.titles[this.covers.length-1] = title; this.descriptions[this.descriptions.length] = description; } //Draws the coverflow with reflections, and 3d covers this.render = function(){ this.title = document.createElement("div"); this.title.style.color = "white"; this.title.style.position = "absolute"; this.title.style.left="0px"; this.title.style.top = "100px"; this.title.style.zIndex = 2000000; this.title.style.width=window.innerWidth+"px"; this.title.style.textAlign="center"; this.parent.appendChild(this.title); this.description = document.createElement("div"); this.description.style.color = "white"; this.description.style.position = "absolute"; this.description.style.left="0px"; this.description.style.top = "200px"; this.description.style.zIndex = 2000000; this.description.style.width=window.innerWidth+"px"; this.description.style.textAlign="center"; this.parent.appendChild(this.description); var t = this; this.ScrollBar = document.createElement('div'); this.ScrollBar.style.backgroundImage = "url(scrollbar-back.png)"; this.ScrollBar.style.position="absolute"; this.ScrollBar.style.top="300px"; this.ScrollBar.style.left=window.innerWidth/2 - 400/2 + 'px'; this.ScrollBar.style.width="400px"; this.ScrollBar.style.zIndex=200; this.left = document.createElement('img'); this.left.src = 'scrollbutton-left.png'; this.left.style.position="absolute"; this.left.style.left = parseInt(this.ScrollBar.style.left) - 20 + 'px'; this.left.style.top = parseInt(this.ScrollBar.style.top) + 'px'; this.left.style.zIndex=0; this.left.onclick = function(){t.slider.setValue(values[t.centerItem-=1]);}; this.parent.appendChild(this.left); this.right = document.createElement('img'); this.right.src = 'scrollbutton-right.png'; this.right.style.position="absolute"; this.right.style.zIndex=20; this.right.style.left="100%"; this.right.onclick = function(){t.slider.setValue(values[t.centerItem+=1]);}; this.scroller = document.createElement('img'); this.scroller.src = 'scrollbar-button.png'; this.scroller.style.zIndex=200; this.scroller.width = parseInt(this.ScrollBar.style.width)/this.covers.length; this.scroller.height = 16; this.ScrollBar.appendChild(this.scroller); this.ScrollBar.appendChild(this.right); this.parent.appendChild(this.ScrollBar); var tick = this.covers.length; for(var i=0;i<this.covers.length;i++){ values[values.length] = i*tick; } this.slider = new Control.Slider(this.scroller,this.ScrollBar,{ values: values, sliderValue:this.centerItem, alignX:-2, range: $R(0,this.covers.length*this.covers.length-this.covers.length), onChange:function(v){ t.centerItem = v/t.covers.length; t.reRender(); }, onSlide:function(v){ t.centerItem = v/t.covers.length; t.reRender(); } }); for(var i=0;i<this.covers.length;i++){ if(i != this.centerItem){ if(i < this.centerItem){ cvi_reflex.add(document.getElementById("cover "+i), { tilt: "right" }); } else{ cvi_reflex.add(document.getElementById("cover "+i), { tilt: "left" }); } } else{ cvi_reflex.add(document.getElementById("cover "+i), { }); this.title.innerHTML = "<b>"+this.titles[i]+"</b>"; this.description.innerHTML = this.descriptions[i]; } document.getElementById("cover "+i).style.position = "absolute"; document.getElementById("cover "+i).onclick = function(){t.slider.setValue(values[this.id.split(" ")[1]]);}; } this.ScrollBar.style.top = document.getElementById("cover "+this.centerItem).offsetTop+ (document.getElementById("cover "+this.centerItem).offsetHeight + 20)+"px"; this.left.style.top = parseInt(this.ScrollBar.style.top) + 'px'; this.title.style.top = document.getElementById("cover "+this.centerItem).offsetTop+ (document.getElementById("cover "+this.centerItem).offsetHeight-20)+"px"; this.description.style.top = parseInt(this.title.style.top)+15+"px"; this.alignCovers(); } //Re-renders the coverflow this.reRender = function(){ if((this.centerItem < this.covers.length)||(this.centerItem >= 0)){ if((this.centerItem != this.oldCenter +1)||(this.centerItem != this.oldCenter -1)){ for(var i=0;i<this.covers.length;i++){ if(i < this.centerItem){ cvi_reflex.modify(document.getElementById("cover "+i), {tilt: "right" }); }else{ cvi_reflex.modify(document.getElementById("cover "+i), {tilt: "left" }); } } } if(this.oldCenter < this.centerItem){ cvi_reflex.modify(document.getElementById("cover "+this.oldCenter), {tilt: "right" }); }else{ cvi_reflex.modify(document.getElementById("cover "+this.oldCenter), {tilt: "left" }); } cvi_reflex.modify(document.getElementById("cover "+this.centerItem), {tilt: "none" }); this.title.innerHTML = "<b>"+this.titles[this.centerItem]+"</b>"; this.description.innerHTML = this.descriptions[this.centerItem]; this.alignCovers(); } } //Animates covers going to their locations this.alignCovers = function(){ for(var i=0;i<this.covers.length;i++){ new Effect.Appear ("cover "+i, {duration: 1}); if(i == this.centerItem){ document.getElementById("cover "+i).style.visibility = "visible"; document.getElementById("cover "+i).style.zIndex = 100000; var yy = window.innerWidth/2 - (parseInt(document.getElementById("cover "+i).style.width)/2); new Effect.Move ("cover "+i,{ duration: 1, x: yy, y: 20, mode: 'absolute'}); } else if(i < this.centerItem){ document.getElementById("cover "+i).style.zIndex = i; var yy = ((window.innerWidth/2 - parseInt(document.getElementById("cover "+this.centerItem).style.width)/2 - parseInt(document.getElementById("cover "+i).style.width)) - ((this.centerItem - i)*(i + (i == this.centerItem-1?-15:40)))); new Effect.Move ("cover "+i,{ duration: 1, x: yy, y: 20, mode: 'absolute'}); } else if(i > this.centerItem){ document.getElementById("cover "+i).style.zIndex = this.covers.length-i; var yy = (window.innerWidth/2 - parseInt(document.getElementById("cover "+this.centerItem).style.width)/2 + parseInt(document.getElementById("cover "+i).style.width)) - (this.centerItem -i)*(i - (i == this.centerItem+1?15:-30)); new Effect.Move ("cover "+i,{ duration: 1, x: yy, y: 20, mode: 'absolute', }); } if(i < this.centerItem-4){ new Effect.Fade ("cover "+i, {duration: 1}); } else if(i > this.centerItem+4){ new Effect.Fade ("cover "+i, {duration: 1}); } this.oldCenter = this.centerItem; } } return this; }
Refactorings
No refactoring yet !
Recently I've been working on developing a Javascript based coverflow (an actual one - with 3D tilts, reflections, etc!). It is based on the cvi_reflex_lib.js from www.netzgesta.de/reflex/ as well as the Prototype and Scriptaculous frameworks for the animations. I won't include them as they are not modified from their original source, but my code is below.
My main problem is getting the animation to be smooth. Also Firefox (on Mac anyway) does some weird stuff with lines on the images when they are tilted. Any help would be appreciated.
Thanks,
Devon