#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Thumbs up Singleton and Window eventlisteners


    Hi there .. im trying to add an eventlistener to my singleton pattern, so i can get the mouse position .. for some reason the event in function(event) isnt being caught .. can anyone help me?
    here's the basic code
    Code:
    var App = {};
    
    App = function(){
    
    	this.mouse;
    	this.SCREEN_WIDTH = 100;
    	this.SCREEN_HEIGHT = 100;
    	this.init();
    
    }
    App.prototype = {
    	init:function(){
    		var me = this;
    		
    		window.addEventListener( 'mousemove', function(e){
    			this.mouse = {x: e.clientX - this.SCREEN_WIDTH/2, y: e.clientY - this.SCREEN_HEIGHT/2};
    			me.render();
    		});
    	
    	},
    	render:function(){
    		console.log(this.mouse.x);
    	}
    }
    var prog = new App();
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    Welcome to DevShed Forums, vonkristoff.

    Try calling addEventListener on "document" or "document.body" instead of "window".
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Lightbulb Ta./


    Originally Posted by Kravvitz
    Welcome to DevShed Forums, vonkristoff.

    Try calling addEventListener on "document" or "document.body" instead of "window".
    Hey thanks, And thanks - worked for me!

    Nice Huge forum here! .. I was finding Stack Overflow annoying for feedback and basic communication ..

IMN logo majestic logo threadwatch logo seochat tools logo