var Style = { getElementStyle: function(elem, property){ var ME = arguments.callee; var value = null; if(window.getComputedStyle) { var compStyle = window.getComputedStyle(elem, ''); value = compStyle.getPropertyValue(property); }else if(elem.currentStyle){ value = elem.currentStyle[property.camelize()]; //!!String.camelize() } if(value){ return value; }else{ ME.errorMessage("Please, turn on the JS"); } } } Function.prototype.errorMessage = function(message){ alert(message); } String.prototype.camelize = function(){ var arrThis = this.split('-'); if(arrThis.length == 1){ return this; }else{ var wordCamelized = arrThis[0]; var firstSymbol; for(var i = 1; i < arrThis.length; i++){ firstSymbol = arrThis[i].substr(0, 1); arrThis[i] = arrThis[i].substr(1); arrThis[i] = firstSymbol.toUpperCase() + arrThis[i]; wordCamelized+=arrThis[i]; } return wordCamelized; } } function Movement2(elem, stepX, stepY, interval, time, distance, position, stopPos){ this.elem = elem; this.stepX = stepX; this.stepY = stepY; this.errX = 0; this.errY = 0; this.interval = interval ? interval : 50; this.time = (time) ? time * 1000 : -1; this.distance = (distance) ? distance : -1; this.distancePassed = 0; this.timeElapsed = 0; this.currentTop = 0; this.currentLeft = 0; this.position = (position) ? position : null; this.stopPos = (stopPos) ? stopPos : null; this.isMoving = false; this.objInterval = null; this.onmove = null; this.onstart = null; this.onstop = null; this.bool_correctPosition = false; this.init() } Movement2.prototype.init = function(){ this.setPos(); //this.start(); } Movement2.prototype.setPos = function(){ this.elem.style.position = 'absolute'; if(this.position){ if(typeof this.position.X != "undefined"){ this.elem.style.left = this.position.X + "px"; this.currentLeft = this.position.X; }else{ var pX = parseInt(Style.getElementStyle(this.elem, 'left')); if(isNaN(pX)){ pX = this.elem.offsetLeft; } this.elem.style.left = pX + "px"; this.currentLeft = pX; } if(this.position.Y){ this.elem.style.top = this.position.Y + 'px'; this.currentTop = this.position.Y; }else{ var pY = parseInt(Style.getElementStyle(this.elem, 'top')); if(isNaN(pY)){ pY = this.elem.offsetTop; } this.elem.style.top = pY + 'px'; this.currentTop = pY; } } } Movement2.prototype.move = function(){ var stop = false; var sX = parseInt(this.stepX); var sY = parseInt(this.stepY); if(Math.abs(sX) < Math.abs(this.stepX)){ this.errX += (this.stepX - sX); if(Math.abs(this.errX) > 1){ sX += parseInt(this.errX); this.errX = (this.errX > 0) ? this.errX - 1 : this.errX + 1; } } if(Math.abs(sY) < Math.abs(this.stepY)){ this.errY += (this.stepY - sY); if(Math.abs(this.errY) > 1){ sY += parseInt(this.errY); this.errY = (this.errY > 0) ? this.errY - 1 : this.errY + 1; } } // this.Debug.out(document.body, "errX: " + this.errX + " errY: " + this.errY); //checking for stop flag //time if(this.time > 0){ this.timeElapsed += this.interval; if(this.timeElapsed > this.time){ stop = true; } } //distance if(this.distance > 0){ var hypo = Math.sqrt(sX*sX + sY*sY); if((this.distancePassed + hypo) >= this.distance){ var new_hypo = this.distance - this.distancePassed; var h_diff = hypo / new_hypo; var newStepY = Math.round(sY / h_diff); var newStepX = Math.round(sX / h_diff); hypo = new_hypo; sX = newStepX; sY = newStepY; stop = true; } this.distancePassed += hypo; } //position if(this.stopPos){ if(typeof this.stopPos.X != "undefined"){ if(sX > 0){ if((this.currentLeft + sX) > this.stopPos.X){ sX = this.stopPos.X - this.currentLeft; var s_diff = this.stepX / sX; sY = Math.round(this.stepY / s_diff); stop = true; } }else{ if((this.currentLeft + sX) < this.stopPos.X){ sX = this.stopPos.X - this.currentLeft; var s_diff = this.stepX / sX; sY = Math.round(this.stepY / s_diff); stop = true; } } }else if(typeof this.stopPos.Y != "undefined"){ if((this.currentTop + sY) > this.stopPos.Y){ sY = this.stopPos.Y - this.currentTop; var s_diff = this.stepY / sY; sX = Math.round(this.stepX / s_diff); stop = true; } } } this.currentLeft += sX; this.currentTop += sY; this.elem.style.left = this.currentLeft + 'px'; this.elem.style.top = this.currentTop + 'px'; // alert(stop); if(this.onmove){ this.onmove.func.apply(this.onmove.oThis, this.onmove.args); } if(stop){ this.stop(); } } Movement2.prototype.moveTo = function(x, y, speed){ this.moveToX = x; this.moveToY = y; this.errX = 0; this.errY = 0; this.distancePassed = 0; var distanceX = x - this.currentLeft; var distanceY = y - this.currentTop; if(!speed) speed = 5; this.stepX = distanceX / 100 * speed; this.stepY = distanceY / 100 * speed; this.distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY); // alert("stepX: " + this.stepX + "\nstepY: " + this.stepY + "\nDistance: "+ this.distance); this.start(); } Movement2.prototype.start = function(preventOnStart){ if(this.stepX == 0 && this.stepY == 0){ this.stop(); return; } this.isMoving = true; var oThis = this; oThis.move.call(oThis); this.objInterval = window.setInterval( function(){ oThis.move.call(oThis) }, this.interval ); if(this.onstart && !preventOnStart){ this.onstart.func.apply(this.onstart.oThis, this.onstart.args); } } Movement2.prototype.stop = function(preventOnStop){ var oInt = this.objInterval; this.isMoving = false; window.clearInterval(oInt); this.correctPosition(); if(this.onstop && !preventOnStop){ this.onstop.func.apply(this.onstop.oThis, this.onstop.args); } } Movement2.prototype.correctPosition = function(){ if(this.bool_correctPosition){ this.elem.style.left = this.moveToX + "px"; this.elem.style.top = this.moveToY + "px"; this.currentLeft = this.moveToX; this.currentTop = this.moveToY; } } Movement2.prototype.Debug = { out: function(console, what, inline){ var oDiv = document.createElement('div'); oDiv.appendChild(document.createTextNode(what)); if(inline){ oDiv.style.display = 'inline'; } console.appendChild(oDiv); }, clear: function(console){ while(console.firstChild){ console.removeChild(console.firstChild); } } }