views:

10316

answers:

3

Hey Stackoverflow,

I'm trying to recreate the iPhone flick / scroll event in a window using JavaScript.

Starting with JQuery, I'm measuring the mouse's acceleration and offset during click - drag - release events using a timer:

var MouseY = {

    init: function(context) {
        var self = this;
        self._context = context || window
        self._down = false;
        self._now = 0;
        self._last = 0;
        self._offset = 0;
        self._timer = 0;
        self._acceleration = 0;

        $(self._context).mousedown(function() {self._down = true;});
        $(self._context).mouseup(function() {self._down = false;});
        $(self._context).mousemove(function(e) {self.move(e);});

    },

    move: function(e) {
        var self = this;
        self._timer++;
        self._last = self._now;
        self._now = e.clientY + window.document.body.scrollTop;
        self._offset = self._now - self._last;
        self._acceleration = self._offset / self._timer;
    },

    reset: function() {
        this._offset = 0;
        this._acceleration = 0;
        this._timer = 0;
    }
};


$(function() {
    MouseY.init();
    setInterval(function() {
        $('#info').html(
            '_acceleration:' + MouseY._acceleration + '<br />' +
            '_now:' + MouseY._now + '<br />' +
            '_offset:' + MouseY._offset + '<br />' +
            '_timer:' + MouseY._timer + '<br />'
        );
        MouseY.reset();
    }, 10);

});

Now the problem is translating that acceleration into screen movement - are there any algorithms (easing?) or animation libraries that could help me out on this? (I've looked into JQuery's .animate() but I'm unsure of how to apply it continuously during the drag events!

Update - final solution here:

http://johnboxall.github.com/iphone.html

+2  A: 

Hit up this link for the full explanation of one approach that seems to be what you're looking for.

http://www.faqts.com/knowledge_base/view.phtml/aid/14742/fid/53

Here's an excerpt:

This handler then sets up event capture for mouse movement and stores mouse cursor positions in variables mouseX and mouseY. It then starts the timer monitorMouse() which measures mouse cursor speed by sampling the values in these variables at regular intervals. The variables mouseLeft and mouseTop hold each samplings mouse positions and the sampling rate is set to 100 milliseconds in the variable monitor.timerDelay.

And some of the author's code:

nn4 = (document.layers)? true:false;
mouseLeft = mouseTop = mouseX = mouseY = 0;
monitor = {
    timerDelay:100,
    moveLimit:2,
    sampleLimit:10
};

function startMonitor(thisText) {
    if (!tip) return;
    toolTipText = thisText;
    writeTooltip(toolTipText);

    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = function (evt) {
     mouseX = evt.pageX;
     mouseY = evt.pageY;
     return true;
    }
    monitorMouse();
}

function stopMonitor() {
    if (!tip) return;
    hideTooltip();
        if (monitor.timer) {
     clearTimeout(monitor.timer);
     monitor.timer = null;
    }
    document.releaseEvents(Event.MOUSEMOVE);
    document.onmousemove = null;
    monitor.slowSamples = 0;
}

function monitorMouse() {
    if (Math.abs(mouseX - mouseLeft)   > monitor.moveLimit
        || Math.abs(mouseY - mouseTop) > monitor.moveLimit)
    {
     monitor.slowSamples = 0;
    }
    else if (++monitor.slowSamples > monitor.sampleLimit) {
     showTooltip();
     return;
    }
    mouseLeft = mouseX;
    mouseTop  = mouseY;
    monitor.timer = setTimeout("monitorMouse()",monitor.timerDelay);
}
Chuck
A: 

Visit this page:

http://www.shinedraw.com/text-effect/silverlight-3-and-flash-iphone-dragging-effect/

There is some code.

moan
+1  A: 

You might be interested in the jQuery plugin named overscroll: http://www.ajaxupdates.com/an-iphone-scrolling-plugin-for-jquery/

Jeremy Dunck