Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:50:29+00:00 2026-05-27T16:50:29+00:00

So i just recently got into canvas and decide to play around with it

  • 0

So i just recently got into canvas and decide to play around with it by making a simple game.

However it doesn’t work in chrome but it does work in firefox. The game is supposed to be a very simple RTS. You select a box and when you right click you can move the box from one point to another.

This works in firefox. In chrome however, it draws the boxes but doesn’t register any clicking (can’t select/move boxes). Also in chrome nothing appears in the firebug console. I am running the script off of xampp in case that would be relevant.

EDIT: ha, I’m sorry. I honestly didn’t know where to start looking before so i just posted the whole thing up. I took the above code down and replaced it with a much shorter version below. also edited formatting

EDIT2: narrowed down below code even more

/*Game mouse controls*/
_screen.mousemove(function(e){
    var playerUnitHover =   _game.onUnit(playerUnits, e),
        enemyUnitHover  =   _game.onUnit(enemyUnits, e);

        if(typeof(playerUnitHover)=='number')   _screen.css('cursor','pointer');
        if(typeof(enemyUnitHover)=='number')    _screen.css('cursor','not-allowed');
});

The code above handles the mouse functions. This is where the problems would probably occur. In firebug chrome i can get the variables to show in the console but it fails to register when the cursor is over the box. This is the function below that handles that.

 /*If mouse coordinates are ontop of unit, then return index of that unit from supplied array argument*/
this.onUnit =   function(array,e){
var numOfUnits      =   array.length,
    mouseOffsetX    =   e.pageX - offsetX,
    mouseOffsetY    =   e.pageY - offsetY;

for(var i = 0; i < numOfUnits; i++){
    var unit    =   array[i],
        xRange  =   mouseOffsetX > unit.x && (unit.x+unit.width) > mouseOffsetX,
        yRange  =   mouseOffsetY > unit.y && (unit.y+unit.height) > mouseOffsetY;

    if (xRange && yRange){
        return i;
    }

    if(!xRange || !yRange)  _screen.css('cursor','crosshair');
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:50:30+00:00Added an answer on May 27, 2026 at 4:50 pm

    The problem is that

    mouseOffsetX    =   e.pageX - offsetX,
    mouseOffsetY    =   e.pageY - offsetY;
    

    is not enough for consistent cross-browser mouse coordinates.

    There are probably many valid ways to get mouse coordinates for a canvas. The bullet-proof way that I use might be overcomplicated but it does work in all browsers and is aware of things like CSS borders:

    getMouse = function(e, canvas) {
      var element = canvas, offsetX = 0, offsetY = 0;
      if (element.offsetParent) {
        do {
          offsetX += element.offsetLeft;
          offsetY += element.offsetTop;
        } while ((element = element.offsetParent));
      }
    
      // Add padding and border style widths to offset
      offsetX += stylePaddingLeft;
      offsetY += stylePaddingTop;
      offsetX += styleBorderLeft;
      offsetY += styleBorderTop;
    
      // We return an javascript object with x and y defined
      return {
        x: e.pageX - offsetX,
        y: e.pageY - offsetY
      };
    }
    

    The stylePadding and styleBorder are probably not necessary unless you have padding/border. You can get the canvas’ stylePadding like this:

      var stylePaddingLeft, stylePaddingTop, styleBorderLeft, styleBorderTop;
      if (document.defaultView && document.defaultView.getComputedStyle) {
        stylePaddingLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingLeft'], 10)      || 0;
        stylePaddingTop  = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingTop'], 10)       || 0;
        styleBorderLeft  = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderLeftWidth'], 10)  || 0;
        styleBorderTop   = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderTopWidth'], 10)   || 0;
      } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just got into python very recently and now I'm practicing by (what I
I've recently been doing a lot of Objective-C programming, and just got back into
I just recently got around to converting an old svn project to git. Everything
I just recently got into using vim, and am having great fun adding load
Just recently got into experimenting with NLog, and it occurs to me that I
Ok, so I just recently got into this Android app development using Eclipse for
I have just recently got involved in a classic ASP.NET project which contains lots
We have just recently got our SVN server installed. My developers have added the
I've been programming for many years and have just recently got the RoR itch.
do you know any software similar to NDepend? I've got it just recently, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.