function moveable(elem, relative){ this.elem = elem; this.type = relative ? 'relative' : 'absolute'; this.onmove = new DOMEvent(); this.onaftermove = new DOMEvent(); this.STOP_X = false; this.STOP_Y = false; this.STOP = false; this.movedX = 0; this.movedY = 0; this.moveX = 0; this.moveY = 0; this.errX = 0; this.errY = 0; this.limits = {}; this.PREVENT_DOUBLE_FIRE = true; this.init(); } moveable.prototype.init = function(){ this.elem.style.position = this.type; var coords = Style.getCoords(this.elem); this.left = coords.left; this.top = coords.top; this.startX = this.left; this.startY = this.top; } moveable.prototype.move = function(){ this.elem.style.left = this.left + 'px'; this.elem.style.top = this.top + 'px'; this.onaftermove.fire(); } moveable.prototype.moveTo = function(x, y){ this.moveBy(x - this.left, y - this.top); } moveable.prototype.moveBy = function(x, y){ this.moveX = Math.floor(Math.abs(x)) * x.signOf(); this.moveY = Math.floor(Math.abs(y)) * y.signOf(); this.errX += (Math.abs(x) - Math.floor(Math.abs(x))) * x.signOf(); this.errY += (Math.abs(y) - Math.floor(Math.abs(y))) * y.signOf(); if(Math.abs(this.errX) > 1){ this.moveX += 1 * x.signOf(); this.errX -= 1 * x.signOf(); } if(Math.abs(this.errY) > 1){ this.moveY += 1 * y.signOf(); this.errY -= 1 * y.signOf(); } this.onmove.fire(); this.movedX += this.moveX; this.movedY += this.moveY; this.distanceX += Math.abs(this.moveX); this.distanceY += Math.abs(this.moveY); this.left += this.moveX; this.top += this.moveY; if(!this.STOP){ this.move(); }else{ this.STOP = false; } } moveable.prototype.setLimits2 = function(type, value, startValue){ type = type.toUpperCase(); if(typeof this.limits[type] != "undefined"){ return; } var PRESETS = moveable.LIMITS[type] this.limits[type] = new Limit(0, value); if(startValue){ this.limits[type].value = startValue; } this.limits[type].type = type; this.limits[type].MINUS_REACHED = false this.limits[type].PLUS_REACHED = false; this.limits[type]._REACHED = false; this.limits[type].onmore.register( 'limit-more', function(objLim){ this['move' + objLim.type] = objLim.MORE_VALUE; objLim._REACHED = true; objLim.MORE_REACHED = true; }.bind(this, this.limits[type]) ); this.limits[type].onless.register( 'limit-less', function(objLim){ this['move' + objLim.type] = objLim.LESS_VALUE; objLim._REACHED = true; objLim.LESS_REACHED = true; }.bind(this, this.limits[type]) ); this[PRESETS.onmore] = {}; this[PRESETS.onless] = {}; this[PRESETS.onmore].register = function(name, func, times){ this.limits[type].onmore.register(name, function(){ this.onaftermove.register(name, func, 1); }.bind(this), times); }.bind(this) this[PRESETS.onless].register = function(name, func, times){ this.limits[type].onless.register(name, function(){ this.onaftermove.register(name, func, 1) }.bind(this), times); }.bind(this) this.onmove.register( 'limit' + type, function(objLim){ objLim.change(this['move' + objLim.type]); }.bind(this, this.limits[type]) ); } moveable.prototype.setDistance = function(objDistance){ } moveable.prototype.setPositionFromLimits = function(objPosition){ } moveable.LIMITS = { X: { onless: 'onleftlimitreach', onmore: 'onrightlimitreach' }, Y: { onless: 'ontoplimitreach', onmore: 'onbottomlimitreach' } }