Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made a drag implementation with jquery.grab #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 26 additions & 44 deletions index.htm
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* Authors :
* Mark Boas @maboa
* Silvia Benvenuti @aulentina
* Jussi Kalliokoski @quinirill
* Jussi Kalliokoski @quinnirill
*
* Inspired by :
* Neway @imneway http://imneway.net/ http://forrst.com/posts/Untitled-CPt
*
* Standing on the shoulders of :
* John Resig @jresig
* Mark Panaghiston @thepag
* Remy Sharp @rem
* Louis-Rémi Babé @Louis_Remi
*
* Dual Licensed GPL/MIT
Expand All @@ -22,13 +21,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.5; user-scalable:no;">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.5; user-scalable:no;">
<title>jPlayer - Circle Player</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.rotate.js"></script>
<script type="text/javascript" src="js/jquery.mousehold.js"></script>
<script type="text/javascript" src="js/jquery.rotate.js"></script>
<script type="text/javascript" src="js/jquery.grab.js"></script>
<script type="text/javascript" src="js/jquery.jplayer.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
Expand Down Expand Up @@ -76,15 +75,15 @@
$('#progress2').css({rotate: '0deg'}).hide();
});

var dragging = false; // Indicates if the progressbar is being 'dragged'.
var dragging = false, dragPc, dragTimer; // Indicates if the progressbar is being 'dragged'.

var clickControl = $('.click-control');

function getArcPc(event) {
function getArcPc(pageX, pageY) {
var self = clickControl,
offset = self.offset(),
x = event.pageX - offset.left - self.width()/2,
y = event.pageY - offset.top - self.height()/2,
x = pageX - offset.left - self.width()/2,
y = pageY - offset.top - self.height()/2,
a = Math.atan2(y,x);

if (a > -1*Math.PI && a < -0.5*Math.PI) {
Expand All @@ -99,42 +98,25 @@
return pc;
}

function onMouseUp(event) {
var pc = getArcPc(event);
myPlayer.jPlayer("playHead", pc).jPlayer("play");
$(document).unbind('mouseup', onMouseUp);
dragging = false;

return false;
};

clickControl.mousedown(function(event){
if (event.preventDefault){
event.preventDefault();
}
function scrub(){
myPlayer.jPlayer("playHead", dragPc).jPlayer("play");
displayProgress(dragPc);
}

$(document).bind('mouseup', onMouseUp);
return false;
});

// Can't figure out how to pass event through to mousehold so I made it global

var moveEvent;

clickControl.mousemove(function(event){
moveEvent = event;
});

clickControl.mousehold(function(){
var pc = getArcPc(moveEvent);
dragging = true;

myPlayer.jPlayer("playHead", pc).jPlayer("play"); // This bit does the scrubbing
displayProgress(pc);
clickControl.grab({
onstart: function(){
dragPc = getArcPc(event.position.x, event.position.y);
dragTimer = setInterval(scrub, 250);
dragging = true;
}, onmove: function(event){
dragPc = getArcPc(event.position.x, event.position.y);
scrub();
}, onfinish: function(event){
dragging = false;
scrub();
clearInterval(dragTimer);
}
});

document.addEventListener('touchmove', function(e){e.preventDefault();}, false);

});
</script>
</head>
Expand Down
201 changes: 201 additions & 0 deletions js/jquery.grab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
jQuery grab
Ported from Jin.js::gestures
Created by Jussi Kalliokoski
Licensed under MIT License.
*/


(function($){
var extend = $.extend,
mousedown = 'mousedown',
mousemove = 'mousemove',
mouseup = 'mouseup',
touchstart = 'touchstart',
touchmove = 'touchmove',
touchend = 'touchend',
touchcancel = 'touchcancel',
gesturestart = 'gesturestart',
gesturechange = 'gesturechange',
gestureend = 'gestureend',
gesturecancel = 'gesturecancel',
MozTouchDown = 'MozTouchDown',
MozTouchMove = 'MozTouchMove',
MozTouchUp = 'MozTouchUp',
mousewheel = 'mousewheel',
DOMMouseScroll = 'DOMMouseScroll';


function unbind(elem, type, func){
var fnc, i;
for (i=0; i<bind._binds.length; i++){
if (bind._binds[i].elem === elem && bind._binds[i].type === type && bind._binds[i].func === func){
if (document.addEventListener){
elem.removeEventListener(type, bind._binds[i].fnc, false);
} else {
elem.detachEvent('on'+type, bind._binds[i].fnc);
}
bind._binds.splice(i--, 1);
}
}
}

function bind(elem, type, func, pass){
var fnc, i;
if (bind[type]){
return bind[type].bind(elem, type, func, pass);
}
fnc = function(e){
if (!e){ // Fix some ie bugs...
e = window.event;
}
if (!e.stopPropagation){
e.stopPropagation = function(){ this.cancelBubble = true; };
}
e.data = pass;
func.call(elem, e);
};
if (document.addEventListener){
elem.addEventListener(type, fnc, false);
} else {
elem.attachEvent('on' + type, fnc);
}
bind._binds.push({elem: elem, type: type, func: func, fnc: fnc});
}

function grab(elem, options)
{
var data = {
move: {x: 0, y: 0},
offset: {x: 0, y: 0},
position: {x: 0, y: 0},
start: {x: 0, y: 0},
affects: document.documentElement,
stopPropagation: false,
preventDefault: true,
touch: true // Implementation unfinished, and doesn't support multitouch
};
extend(data, options);
data.element = elem;
bind(elem, mousedown, mouseDown, data);
if (data.touch){
bind(elem, touchstart, touchStart, data);
}
}
function ungrab(elem){
unbind(elem, mousedown, mousedown);
}
function mouseDown(e){
e.data.position.x = e.pageX;
e.data.position.y = e.pageY;
e.data.start.x = e.pageX;
e.data.start.y = e.pageY;
e.data.event = e;
if (e.data.onstart && e.data.onstart.call(e.data.element, e.data)){
return;
}
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.stopPropagation){
e.stopPropagation();
}
bind(e.data.affects, mousemove, mouseMove, e.data);
bind(e.data.affects, mouseup, mouseUp, e.data);
}
function mouseMove(e){
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.preventDefault){
e.stopPropagation();
}
e.data.move.x = e.pageX - e.data.position.x;
e.data.move.y = e.pageY - e.data.position.y;
e.data.position.x = e.pageX;
e.data.position.y = e.pageY;
e.data.offset.x = e.pageX - e.data.start.x;
e.data.offset.y = e.pageY - e.data.start.y;
e.data.event = e;
if (e.data.onmove){
e.data.onmove.call(e.data.element, e.data);
}
}
function mouseUp(e){
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.stopPropagation){
e.stopPropagation();
}
unbind(e.data.affects, mousemove, mouseMove);
unbind(e.data.affects, mouseup, mouseUp);
e.data.event = e;
if (e.data.onfinish){
e.data.onfinish.call(e.data.element, e.data);
}
}
function touchStart(e){
e.data.position.x = e.touches[0].pageX;
e.data.position.y = e.touches[0].pageY;
e.data.start.x = e.touches[0].pageX;
e.data.start.y = e.touches[0].pageY;
e.data.event = e;
if (e.data.onstart && e.data.onstart.call(e.data.element, e.data)){
return;
}
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.stopPropagation){
e.stopPropagation();
}
bind(e.data.affects, touchmove, touchMove, e.data);
bind(e.data.affects, touchend, touchEnd, e.data);
}
function touchMove(e){
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.stopPropagation){
e.stopPropagation();
}
e.data.move.x = e.touches[0].pageX - e.data.position.x;
e.data.move.y = e.touches[0].pageY - e.data.position.y;
e.data.position.x = e.touches[0].pageX;
e.data.position.y = e.touches[0].pageY;
e.data.offset.x = e.touches[0].pageX - e.data.start.x;
e.data.offset.y = e.touches[0].pageY - e.data.start.y;
e.data.event = e;
if (e.data.onmove){
e.data.onmove.call(e.data.elem, e.data);
}
}
function touchEnd(e){
if (e.preventDefault && e.data.preventDefault){
e.preventDefault();
}
if (e.stopPropagation && e.data.stopPropagation){
e.stopPropagation();
}
unbind(e.data.affects, touchmove, touchMove);
unbind(e.data.affects, touchend, touchEnd);
e.data.event = e;
if (e.data.onfinish){
e.data.onfinish.call(e.data.element, e.data);
}
}

bind._binds = [];

$.fn.grab = function(a, b){
return this.each(function(){
return grab(this, a, b);
});
};
$.fn.ungrab = function(a){
return this.each(function(){
return ungrab(this, a);
});
};
})(jQuery);
Loading