Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[web] HTML5 Platform Independent 'mousemove' ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 18 November 2011 - 06:51 AM

I have started to learn html5, and honestly... I think its pretty lame that all these browser companies/platforms make us developers go through a song and a dance, just because they can't get a life and agree on some common variable names.

My nerd rage aside, what is the best way to customize my functions based on the platform my html5 document is being displayed on?

I am thinking... pc, mac, iPhone, FireFox, Chrome, Opera, etc.

I'd rather not go through each and every single event, parse for the event fields, then perform the action, which is what I see in almost all google searches I have done...
function OnMouseMove(e){
if(e.pageX) ...

else if (window.event.clientX) ....

else ....
}
I think that is totally bad way to do this. I have the following code working for both Chrome and Firefox, and I was hoping someone with more HTML5 coding experience than I have, could help flesh this out for other browsers / platforms...


var c;
var co;
function Init(){
// get the 'canvas' element
	c = document.getElementById("c");
// get the 2d context
	co = c.getContext("2d");
// here is where I want to determine my listeners... AT LOAD TIME
	if(!window.event){ // this means it is fire fox ....
		c.addEventListener("mousemove", _PageMouseMove, false);
	}
	else{ // this means it is chrome...
		c.addEventListener("mousemove", _ClientMouseMove, false);
	}
}

// here I have my personalized handlers, based on the platform....
function _PageMouseMove(e){
	document.getElementById("x").innerHTML = "pageX: "+e.pageX;
	document.getElementById("y").innerHTML = "pageY: "+e.pageY;
}
function _ClientMouseMove(e){
	document.getElementById("x").innerHTML = "clientX: "+e.clientX;
	document.getElementById("y").innerHTML = "clientY: "+e.clientY;
}

Isn't there a better way to do something like this? I could even imagine loading different src files based on this...

Please point me in the right direction!

Sponsor:

#2 DarkZlayer   Members   -  Reputation: 124

Like
1Likes
Like

Posted 18 November 2011 - 09:03 PM

I don't know a lot about cross browser compatability, but one thing you should do to help ease the pain is to have each browser just pass in their respective x,y into functions that actually handle it:

Instead of duplicating code do this:

function FirefoxMouseMove(e) {
	HandleMouseMove(e.pageX, e.pageY);
}

function ChromeMouseMove(e){
	HandleMouseMove(e.clientX, e.clientY);
}

function HandleMouseMove(x,y) {
	document.getElementById("x").innerHTML="clientX: "+x;
        document.getElementById("y").innerHTML="clientY: "+y;
}
Sorry I cannot help more than that, but hopefully it's better than nothing..

#3 grc   Members   -  Reputation: 102

Like
1Likes
Like

Posted 19 November 2011 - 02:24 AM

I strongly suggest that you use a javascript library, such as jQuery. jQuery is compatible with all recent browsers (and many old ones), and it makes writing in javascript significantly easier.

The following code achieves what I think you're looking for. It uses jQuery and it should work for all decent browsers.
$(function() {

        $("#c").mousemove(function(e) {

 			$("#x").html("Page X: " + e.pageX);
 			$("#y").html("Page Y: " + e.pageY);
        
    });
});


To answer your question, there are two ways to go about browser compatibility.

Technically, you could use $.browser to detect the browser and $.getScript to load a different script depending on the browser. However, browser detection is generally considered to be a bad idea.

The alternative is to test whether the features you need are supported (article). There are many ways of detecting whether a feature is supported, including jQuery.support "If you always use object detection, your scripts will never generate any error messages, although they might not work in certain browsers."

I hope I've been able to help Posted Image

#4 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 19 November 2011 - 03:50 AM

Yes, that is what I have been reading, that it is a bad idea to write your code based off of browser detection. I guess browser data can be forged.

Up until a few days ago, all of the Javascript I have written has been via a game engine. So I know the language quite well (several years experience), but I don't have any 'outside' experience with it. So now I am in this new world of HTML implementation of Javascript, and find that there is quite a bit I don't know how to do. :D

For example, JQuery... I have never used it. I'll take a look at that sight you provided and research about it, but to be honest, I'll probably stick with my native Javascript, and do it all the hard way. I like to know exactly what is going on, and have the first hand knowledge, instead of having things work 'auto-magically'. :)

My main goal here, is to write my own 2d game framework using html5. I am pretty sure I know enough Javascript and core game programming concepts/mechanics to do it, now I need to beef up my knowledge of HTML5 and the whole cross browser compatibility stuff.

Thanks for the responses guys. That is a good idea about the duplicate functions DarkZlayer, I'll be sure to do that.

I also appreciate your links you provided grc I have some time right now, so I am going over them.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS