├── readme.md ├── style.css ├── index.html ├── script.js └── tween.js /readme.md: -------------------------------------------------------------------------------- 1 | # JS Tweening Boxes 2 | 3 | ## Post 4 | 5 | - [https://f90.co.uk/labs/js-tweening-boxes/](https://f90.co.uk/labs/js-tweening-boxes/) 6 | 7 | ## Example 8 | 9 | - [http://orangespaceman.github.io/js-tweening-boxes](http://orangespaceman.github.io/js-tweening-boxes) 10 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | margin:0; 4 | padding:0; 5 | background: #fff; 6 | } 7 | 8 | /* 9 | * stage container 10 | */ 11 | #stagecontainer { 12 | position: absolute; 13 | top: 50%; 14 | left: 50%; 15 | margin-top: -125px; 16 | margin-left: -240px; 17 | width : 480px; 18 | height : 250px; 19 | overflow : auto; 20 | border : 2px solid #f90; 21 | } 22 | 23 | 24 | 25 | /* 26 | * boxes 27 | */ 28 | .box { 29 | width : 410px; 30 | height : 190px; 31 | margin : 1.5em 0px 0px 1.5em; 32 | padding : 10px; 33 | border : 1px solid #f90; 34 | background : #fed; 35 | } 36 | 37 | .box li { 38 | padding-bottom : 0px; 39 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | JS Tweening Boxes 4 | 5 | 6 | 7 | 8 | 9 |
10 |
11 | 25 |
26 |

box 1

27 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

28 |

« back

29 |
30 |
31 |

box 2

32 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

33 |

« back

34 |
35 |
36 |

box 3

37 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

38 |

« back

39 |
40 |
41 |

box 4

42 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

43 |

« back

44 |
45 |
46 |

box 5

47 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

48 |

« back

49 |
50 |
51 |

box 6

52 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

53 |

« back

54 |
55 |
56 |

box 7

57 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

58 |

« back

59 |
60 |
61 |

box 8

62 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

63 |

« back

64 |
65 |
66 |

box 9

67 |

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tempor. Suspendisse potenti. Nullam quis risus ac est faucibus porta. Aenean porta interdum urna. Duis volutpat nonummy ipsum. Quisque arcu est, aliquam ut, condimentum vitae, condimentum quis, sem. Duis lacus mauris.

68 |

« back

69 |
70 |
71 |
72 | 73 | 74 | 75 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | /** 2 | * This is the stage scrolling class 3 | */ 4 | var stageScroller = { 5 | 6 | // string : the type of tween to use 7 | // backEaseIn, backEaseOut, backEaseInOut, elasticEaseIn, elasticEaseOut, elasticEaseInOut, bounceEaseOut, bounceEaseIn, 8 | // bounceEaseInOut, regularEaseIn, regularEaseOut, regularEaseInOut, strongEaseIn, strongEaseOut, strongEaseInOut 9 | tweenType : Tween.elasticEaseInOut, 10 | 11 | // int : the length of time for each tween (in seconds) 12 | tweenTime : 3, 13 | 14 | // boolean : detect whether we are currently scrolling 15 | currentlyScrolling : false, 16 | 17 | // the setInterval js object for the main scroll (gets cleared and reset) 18 | scrollInterval : null, 19 | 20 | // object : the html stage container element 21 | stageContainer : null, 22 | 23 | // object : the html stage element 24 | stage : null, 25 | 26 | // array : container for all box html elements 27 | boxes : null, 28 | 29 | // int : time between each movement in milliseconds 30 | tweenTime : 50, 31 | 32 | // int : max amount of movement per movement in em 33 | maxTween : 50, 34 | 35 | // ints : the current position of the stage (x=left, y=top) 36 | stageX : 0, 37 | stageY : 0, 38 | 39 | // ints : the target position for the stage (x=left, y=top) 40 | targetX : 0, 41 | targetY : 0, 42 | 43 | // array : links out from the first (links) box to each individual box 44 | boxlinks : null, 45 | 46 | // array : links back from each individual box to the first (links) box 47 | backlinks : null, 48 | 49 | // object : the debugbox created by code 50 | debugbox : null, 51 | 52 | 53 | /* 54 | * initialisation function 55 | */ 56 | init: function(tweenType, tweenTime) { 57 | 58 | // condition : change tween type 59 | if (tweenType != "") { 60 | this.tweenType = tweenType; 61 | } 62 | 63 | // condition : change tween time 64 | if (tweenTime != "") { 65 | this.tweenTime = tweenTime; 66 | } 67 | 68 | // get the stage container and set initial styles 69 | this.stageContainer = document.getElementById('stagecontainer'); 70 | this.stageContainer.style.position = "relative"; 71 | this.stageContainer.style.overflow = "visible"; 72 | this.stageContainer.style.overflow = "hidden"; 73 | 74 | 75 | // get the stage and set initial styles 76 | this.stage = document.getElementById('stage'); 77 | this.stage.style.position = "relative"; 78 | this.stage.style.width = "4000px"; 79 | this.stage.style.height = "4000px"; 80 | this.stage.style.right = this.stageX + "px"; 81 | this.stage.style.bottom = this.stageY + "px"; 82 | //this.stage.style.overflow = "hidden"; 83 | 84 | 85 | // get all boxes 86 | this.boxes = document.querySelectorAll(".box"); 87 | for (var x = 0; x < this.boxes.length; x++) { 88 | this.boxes[x].style.position = "absolute"; 89 | this.boxes[x].top = Math.round(Math.cos((2 * Math.PI * x)/this.boxes.length) * 480); 90 | this.boxes[x].left = Math.round(Math.sin((2 * Math.PI * x)/this.boxes.length) * 480); 91 | this.boxes[x].style.left = this.boxes[x].left + "px"; 92 | this.boxes[x].style.top = this.boxes[x].top + "px"; 93 | this.boxes[x].style.zIndex = this.boxes.length - x; 94 | } 95 | 96 | //position the first box in the centre 97 | this.boxes[0].left = this.stageX; 98 | this.boxes[0].top = this.stageY; 99 | this.boxes[0].style.left = this.boxes[0].left + "px"; 100 | this.boxes[0].style.top = this.boxes[0].top + "px"; 101 | 102 | 103 | // get the links to each box 104 | this.boxlinks = this.boxes[0].getElementsByTagName('a'); 105 | for (var i=0; i < this.boxlinks.length; i++) { 106 | 107 | // set a value for the current box on the link 108 | this.boxlinks[i].x = i; 109 | 110 | // remove default link behaviour 111 | this.boxlinks[i].onclick = function() { return false }; 112 | 113 | // go to the box! 114 | this.boxlinks[i].addEventListener('click', this.goToBox, false); 115 | }; 116 | 117 | 118 | // get all the back links 119 | this.backlinks = document.querySelectorAll('.back'); 120 | for (var q=0; q < this.backlinks.length; q++) { 121 | 122 | // set a value for the current box on the link 123 | this.backlinks[q].x = q; 124 | 125 | // remove default link behaviour 126 | this.backlinks[q].onclick = function() { return false }; 127 | 128 | // go to the box! 129 | this.backlinks[q].addEventListener('click', this.goFromBox, false); 130 | }; 131 | 132 | 133 | // start debug 134 | this.debug('debug initialised'); 135 | 136 | }, 137 | 138 | 139 | /* 140 | * function to select a random number - for the box positioning 141 | */ 142 | random: function(min, max) { 143 | return Math.floor(Math.random() * (max - min + 1) + min); 144 | }, 145 | 146 | 147 | /* 148 | * function to initiate a scroll to a specific box from the centre 149 | */ 150 | goToBox: function() { 151 | 152 | // if no scroll is currently going on 153 | if (!stageScroller.currentlyScrolling) { 154 | 155 | // get the box in question 156 | var box = stageScroller.boxes[this.x + 1]; 157 | 158 | // start the scroll to the box's location 159 | stageScroller.scrollStage(box.left, box.top); 160 | 161 | // debug 162 | stageScroller.debug('scrolling to box ' + (this.x+1) + ', x:' + box.left + ', y:' + box.top); 163 | } 164 | }, 165 | 166 | 167 | /* 168 | * function to initiate a scroll from a specific box back to centre 169 | */ 170 | goFromBox: function() { 171 | 172 | // if no scroll is currently going on 173 | if (!stageScroller.currentlyScrolling) { 174 | 175 | // get the home box 176 | var homebox = stageScroller.boxes[0]; 177 | 178 | // start the scroll to the home box 179 | stageScroller.scrollStage(homebox.left, homebox.top); 180 | 181 | // debug 182 | stageScroller.debug('heading back from box ' + (this.x+1)); 183 | } 184 | }, 185 | 186 | 187 | /* 188 | * function to start the scrolling of the stage from it's current position to point (x,y) 189 | */ 190 | scrollStage: function(x, y) { 191 | 192 | // set the currently scrolling 193 | stageScroller.currentlyScrolling = true; 194 | 195 | // set the target location 196 | stageScroller.targetX = x; 197 | stageScroller.targetY = y; 198 | 199 | // start the scroll! 200 | t1 = new Tween(stageScroller.stage.style,'right',stageScroller.tweenType,stageScroller.stageX,stageScroller.targetX,stageScroller.tweenTime,'px'); 201 | t2 = new Tween(stageScroller.stage.style,'bottom',stageScroller.tweenType,stageScroller.stageY,stageScroller.targetY,stageScroller.tweenTime,'px'); 202 | 203 | t1.onMotionFinished = function() { 204 | stageScroller.currentlyScrolling = false; 205 | stageScroller.stageX = stageScroller.targetX; 206 | stageScroller.stageY = stageScroller.targetY; 207 | this.onMotionFinished = undefined; 208 | }; 209 | t1.start(); 210 | t2.start(); 211 | 212 | //debug 213 | stageScroller.debug('targetX = ' + stageScroller.targetX + ", stageX = " + stageScroller.stageX); 214 | }, 215 | 216 | /* 217 | * function to create a debugbox and add message 218 | */ 219 | debug: function(newmessage) { 220 | 221 | // if debugbox doesn't exist, create it 222 | if (stageScroller.debugbox == null) { 223 | 224 | //get the body element 225 | var body = document.getElementsByTagName("body")[0]; 226 | 227 | //create the new html element 228 | stageScroller.debugbox = document.createElement('div'); 229 | stageScroller.debugbox.id = "debugbox"; 230 | stageScroller.debugbox.style.height = "10%"; 231 | stageScroller.debugbox.style.position = "absolute"; 232 | stageScroller.debugbox.style.bottom = "2em"; 233 | stageScroller.debugbox.style.right = "2em"; 234 | stageScroller.debugbox.style.left = "2em"; 235 | stageScroller.debugbox.style.overflow = "scroll"; 236 | stageScroller.debugbox.style.border = "1px solid #f90"; 237 | stageScroller.debugbox.style.padding = "1em"; 238 | stageScroller.debugbox.style.zindex = "9999"; 239 | 240 | //add the new html element to the document 241 | //body.appendChild(stageScroller.debugbox); 242 | 243 | } 244 | 245 | // add message to debugbox 246 | var message = document.createElement('p'); 247 | var messagetxt = document.createTextNode("> " + newmessage); 248 | message.appendChild(messagetxt); 249 | stageScroller.debugbox.insertBefore(message, stageScroller.debugbox.firstChild); 250 | 251 | } 252 | } -------------------------------------------------------------------------------- /tween.js: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | TERMS OF USE - EASING EQUATIONS 3 | Open source under the BSD License. 4 | Copyright (c) 2001 Robert Penner 5 | JavaScript version copyright (C) 2006 by Philippe Maegerman 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are 10 | met: 11 | 12 | * Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | * Redistributions in binary form must reproduce the above 15 | copyright notice, this list of conditions and the following disclaimer 16 | in the documentation and/or other materials provided with the 17 | distribution. 18 | * Neither the name of the author nor the names of contributors may 19 | be used to endorse or promote products derived from this software 20 | without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | 34 | *****************************************/ 35 | function Delegate() {} 36 | Delegate.create = function (o, f) { 37 | var a = new Array() ; 38 | var l = arguments.length ; 39 | for(var i = 2 ; i < l ; i++) a[i - 2] = arguments[i] ; 40 | return function() { 41 | var aP = [].concat(arguments, a) ; 42 | f.apply(o, aP); 43 | } 44 | } 45 | 46 | Tween = function(obj, prop, func, begin, finish, duration, suffixe){ 47 | this.init(obj, prop, func, begin, finish, duration, suffixe) 48 | } 49 | var t = Tween.prototype; 50 | 51 | t.obj = new Object(); 52 | t.prop=''; 53 | t.func = function (t, b, c, d) { return c*t/d + b; }; 54 | t.begin = 0; 55 | t.change = 0; 56 | t.prevTime = 0; 57 | t.prevPos = 0; 58 | t.looping = false; 59 | t._duration = 0; 60 | t._time = 0; 61 | t._pos = 0; 62 | t._position = 0; 63 | t._startTime = 0; 64 | t._finish = 0; 65 | t.name = ''; 66 | t.suffixe = ''; 67 | t._listeners = new Array(); 68 | t.setTime = function(t){ 69 | this.prevTime = this._time; 70 | if (t > this.getDuration()) { 71 | if (this.looping) { 72 | this.rewind (t - this._duration); 73 | this.update(); 74 | this.broadcastMessage('onMotionLooped',{target:this,type:'onMotionLooped'}); 75 | } else { 76 | this._time = this._duration; 77 | this.update(); 78 | this.stop(); 79 | this.broadcastMessage('onMotionFinished',{target:this,type:'onMotionFinished'}); 80 | } 81 | } else if (t < 0) { 82 | this.rewind(); 83 | this.update(); 84 | } else { 85 | this._time = t; 86 | this.update(); 87 | } 88 | } 89 | t.getTime = function(){ 90 | return this._time; 91 | } 92 | t.setDuration = function(d){ 93 | this._duration = (d == null || d <= 0) ? 100000 : d; 94 | } 95 | t.getDuration = function(){ 96 | return this._duration; 97 | } 98 | t.setPosition = function(p){ 99 | this.prevPos = this._pos; 100 | var a = this.suffixe != '' ? this.suffixe : ''; 101 | this.obj[this.prop] = Math.round(p) + a; 102 | this._pos = p; 103 | this.broadcastMessage('onMotionChanged',{target:this,type:'onMotionChanged'}); 104 | } 105 | t.getPosition = function(t){ 106 | if (t == undefined) t = this._time; 107 | return this.func(t, this.begin, this.change, this._duration); 108 | }; 109 | t.setFinish = function(f){ 110 | this.change = f - this.begin; 111 | }; 112 | t.geFinish = function(){ 113 | return this.begin + this.change; 114 | }; 115 | t.init = function(obj, prop, func, begin, finish, duration, suffixe){ 116 | if (!arguments.length) return; 117 | this._listeners = new Array(); 118 | this.addListener(this); 119 | if(suffixe) this.suffixe = suffixe; 120 | this.obj = obj; 121 | this.prop = prop; 122 | this.begin = begin; 123 | this._pos = begin; 124 | this.setDuration(duration); 125 | if (func!=null && func!='') { 126 | this.func = func; 127 | } 128 | this.setFinish(finish); 129 | } 130 | t.start = function(){ 131 | this.rewind(); 132 | this.startEnterFrame(); 133 | this.broadcastMessage('onMotionStarted',{target:this,type:'onMotionStarted'}); 134 | //alert('in'); 135 | } 136 | t.rewind = function(t){ 137 | this.stop(); 138 | this._time = (t == undefined) ? 0 : t; 139 | this.fixTime(); 140 | this.update(); 141 | } 142 | t.fforward = function(){ 143 | this._time = this._duration; 144 | this.fixTime(); 145 | this.update(); 146 | } 147 | t.update = function(){ 148 | this.setPosition(this.getPosition(this._time)); 149 | } 150 | t.startEnterFrame = function(){ 151 | this.stopEnterFrame(); 152 | this.isPlaying = true; 153 | this.onEnterFrame(); 154 | } 155 | t.onEnterFrame = function(){ 156 | if(this.isPlaying) { 157 | this.nextFrame(); 158 | setTimeout(Delegate.create(this, this.onEnterFrame), 0); 159 | } 160 | } 161 | t.nextFrame = function(){ 162 | this.setTime((this.getTimer() - this._startTime) / 1000); 163 | } 164 | t.stop = function(){ 165 | this.stopEnterFrame(); 166 | this.broadcastMessage('onMotionStopped',{target:this,type:'onMotionStopped'}); 167 | } 168 | t.stopEnterFrame = function(){ 169 | this.isPlaying = false; 170 | } 171 | 172 | t.continueTo = function(finish, duration){ 173 | this.begin = this._pos; 174 | this.setFinish(finish); 175 | if (this._duration != undefined) 176 | this.setDuration(duration); 177 | this.start(); 178 | } 179 | t.resume = function(){ 180 | this.fixTime(); 181 | this.startEnterFrame(); 182 | this.broadcastMessage('onMotionResumed',{target:this,type:'onMotionResumed'}); 183 | } 184 | t.yoyo = function (){ 185 | this.continueTo(this.begin,this._time); 186 | } 187 | 188 | t.addListener = function(o){ 189 | this.removeListener (o); 190 | return this._listeners.push(o); 191 | } 192 | t.removeListener = function(o){ 193 | var a = this._listeners; 194 | var i = a.length; 195 | while (i--) { 196 | if (a[i] == o) { 197 | a.splice (i, 1); 198 | return true; 199 | } 200 | } 201 | return false; 202 | } 203 | t.broadcastMessage = function(){ 204 | var arr = new Array(); 205 | for(var i = 0; i < arguments.length; i++){ 206 | arr.push(arguments[i]) 207 | } 208 | var e = arr.shift(); 209 | var a = this._listeners; 210 | var l = a.length; 211 | for (var i=0; i