Skip to content

Delta return 0 when stick is release #41

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
100 changes: 50 additions & 50 deletions virtualjoystick.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ var VirtualJoystick = function(opts)

this._pressed = false;
this._touchIdx = null;

if(this._stationaryBase === true){
this._baseEl.style.display = "";
this._baseEl.style.left = (this._baseX - this._baseEl.width /2)+"px";
this._baseEl.style.top = (this._baseY - this._baseEl.height/2)+"px";
}

this._transform = this._useCssTransform ? this._getTransformProperty() : false;
this._has3d = this._check3D();

var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this._$onTouchStart = __bind(this._onTouchStart , this);
this._$onTouchEnd = __bind(this._onTouchEnd , this);
Expand Down Expand Up @@ -92,7 +92,7 @@ VirtualJoystick.touchScreenAvailable = function()
destObj.dispatchEvent = function(event /* , args... */){
if(this._events === undefined) this._events = {};
if( this._events[event] === undefined ) return;
var tmpArray = this._events[event].slice();
var tmpArray = this._events[event].slice();
for(var i = 0; i < tmpArray.length; i++){
var result = tmpArray[i].apply(this, Array.prototype.slice.call(arguments, 1))
if( result !== undefined ) return result;
Expand All @@ -105,8 +105,8 @@ VirtualJoystick.touchScreenAvailable = function()
// //
//////////////////////////////////////////////////////////////////////////////////

VirtualJoystick.prototype.deltaX = function(){ return this._stickX - this._baseX; }
VirtualJoystick.prototype.deltaY = function(){ return this._stickY - this._baseY; }
VirtualJoystick.prototype.deltaX = function(){ return (this._pressed ? this._stickX - this._baseX : 0) }
VirtualJoystick.prototype.deltaY = function(){ return (this._pressed ? this._stickY - this._baseY : 0) }

VirtualJoystick.prototype.up = function(){
if( this._pressed === false ) return false;
Expand All @@ -122,23 +122,23 @@ VirtualJoystick.prototype.down = function(){
var deltaY = this.deltaY();
if( deltaY <= 0 ) return false;
if( Math.abs(deltaX) > 2*Math.abs(deltaY) ) return false;
return true;
return true;
}
VirtualJoystick.prototype.right = function(){
if( this._pressed === false ) return false;
var deltaX = this.deltaX();
var deltaY = this.deltaY();
if( deltaX <= 0 ) return false;
if( Math.abs(deltaY) > 2*Math.abs(deltaX) ) return false;
return true;
return true;
}
VirtualJoystick.prototype.left = function(){
if( this._pressed === false ) return false;
var deltaX = this.deltaX();
var deltaY = this.deltaY();
if( deltaX >= 0 ) return false;
if( Math.abs(deltaY) > 2*Math.abs(deltaX) ) return false;
return true;
return true;
}

//////////////////////////////////////////////////////////////////////////////////
Expand All @@ -147,68 +147,68 @@ VirtualJoystick.prototype.left = function(){

VirtualJoystick.prototype._onUp = function()
{
this._pressed = false;
this._pressed = false;
this._stickEl.style.display = "none";
if(this._stationaryBase == false){

if(this._stationaryBase == false){
this._baseEl.style.display = "none";

this._baseX = this._baseY = 0;
this._stickX = this._stickY = 0;
}
}

VirtualJoystick.prototype._onDown = function(x, y)
{
this._pressed = true;
this._pressed = true;
if(this._stationaryBase == false){
this._baseX = x;
this._baseY = y;
this._baseEl.style.display = "";
this._move(this._baseEl.style, (this._baseX - this._baseEl.width /2), (this._baseY - this._baseEl.height/2));
}

this._stickX = x;
this._stickY = y;

if(this._limitStickTravel === true){
var deltaX = this.deltaX();
var deltaY = this.deltaY();
var stickDistance = Math.sqrt( (deltaX * deltaX) + (deltaY * deltaY) );
if(stickDistance > this._stickRadius){
var stickNormalizedX = deltaX / stickDistance;
var stickNormalizedY = deltaY / stickDistance;

this._stickX = stickNormalizedX * this._stickRadius + this._baseX;
this._stickY = stickNormalizedY * this._stickRadius + this._baseY;
}
}
}

this._stickEl.style.display = "";
this._move(this._stickEl.style, (this._stickX - this._stickEl.width /2), (this._stickY - this._stickEl.height/2));
this._move(this._stickEl.style, (this._stickX - this._stickEl.width /2), (this._stickY - this._stickEl.height/2));
}

VirtualJoystick.prototype._onMove = function(x, y)
{
if( this._pressed === true ){
this._stickX = x;
this._stickY = y;

if(this._limitStickTravel === true){
var deltaX = this.deltaX();
var deltaY = this.deltaY();
var stickDistance = Math.sqrt( (deltaX * deltaX) + (deltaY * deltaY) );
if(stickDistance > this._stickRadius){
var stickNormalizedX = deltaX / stickDistance;
var stickNormalizedY = deltaY / stickDistance;

this._stickX = stickNormalizedX * this._stickRadius + this._baseX;
this._stickY = stickNormalizedY * this._stickRadius + this._baseY;
}
}
}
this._move(this._stickEl.style, (this._stickX - this._stickEl.width /2), (this._stickY - this._stickEl.height/2));
}

this._move(this._stickEl.style, (this._stickX - this._stickEl.width /2), (this._stickY - this._stickEl.height/2));
}
}


Expand Down Expand Up @@ -248,7 +248,7 @@ VirtualJoystick.prototype._onTouchStart = function(event)
// notify event for validation
var isValid = this.dispatchEvent('touchStartValidation', event);
if( isValid === false ) return;

// dispatch touchStart
this.dispatchEvent('touchStart', event);

Expand All @@ -275,7 +275,7 @@ VirtualJoystick.prototype._onTouchEnd = function(event)
// try to find our touch event
var touchList = event.changedTouches;
for(var i = 0; i < touchList.length && touchList[i].identifier !== this._touchIdx; i++);
// if touch event isnt found,
// if touch event isnt found,
if( i === touchList.length) return;

// reset touchIdx - mark it as no-touch-in-progress
Expand Down Expand Up @@ -320,20 +320,20 @@ VirtualJoystick.prototype._buildJoystickBase = function()
var canvas = document.createElement( 'canvas' );
canvas.width = 126;
canvas.height = 126;

var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 6;
ctx.arc( canvas.width/2, canvas.width/2, 40, 0, Math.PI*2, true);
ctx.stroke();

ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 2;
ctx.arc( canvas.width/2, canvas.width/2, 60, 0, Math.PI*2, true);
ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 6;
ctx.arc( canvas.width/2, canvas.width/2, 40, 0, Math.PI*2, true);
ctx.stroke();


ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 2;
ctx.arc( canvas.width/2, canvas.width/2, 60, 0, Math.PI*2, true);
ctx.stroke();

return canvas;
}

Expand All @@ -346,16 +346,16 @@ VirtualJoystick.prototype._buildJoystickStick = function()
canvas.width = 86;
canvas.height = 86;
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 6;
ctx.arc( canvas.width/2, canvas.width/2, 40, 0, Math.PI*2, true);
ctx.beginPath();
ctx.strokeStyle = this._strokeStyle;
ctx.lineWidth = 6;
ctx.arc( canvas.width/2, canvas.width/2, 40, 0, Math.PI*2, true);
ctx.stroke();
return canvas;
}

//////////////////////////////////////////////////////////////////////////////////
// move using translate3d method with fallback to translate > 'top' and 'left'
// move using translate3d method with fallback to translate > 'top' and 'left'
// modified from https://github.com/component/translate and dependents
//////////////////////////////////////////////////////////////////////////////////

Expand All @@ -373,7 +373,7 @@ VirtualJoystick.prototype._move = function(style, x, y)
}
}

VirtualJoystick.prototype._getTransformProperty = function()
VirtualJoystick.prototype._getTransformProperty = function()
{
var styles = [
'webkitTransform',
Expand All @@ -391,11 +391,11 @@ VirtualJoystick.prototype._getTransformProperty = function()
if (null != el.style[style]) {
return style;
}
}
}
}
VirtualJoystick.prototype._check3D = function()
{

VirtualJoystick.prototype._check3D = function()
{
var prop = this._getTransformProperty();
// IE8<= doesn't have `getComputedStyle`
if (!prop || !window.getComputedStyle) return module.exports = false;
Expand Down