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 6159341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:58+00:00 2026-05-23T21:06:58+00:00

Is it possible to get the RGB value pixel under the mouse? Is there

  • 0

Is it possible to get the RGB value pixel under the mouse? Is there a complete example of this? Here’s what I have so far:

function draw() {
      var ctx = document.getElementById('canvas').getContext('2d');
      var img = new Image();
      img.src = 'Your URL';

      img.onload = function(){
        ctx.drawImage(img,0,0);


      };

      canvas.onmousemove = function(e) {
            var mouseX, mouseY;

            if(e.offsetX) {
                mouseX = e.offsetX;
                mouseY = e.offsetY;
            }
            else if(e.layerX) {
                mouseX = e.layerX;
                mouseY = e.layerY;
            }
            var c = ctx.getImageData(mouseX, mouseY, 1, 1).data;
            
            $('#ttip').css({'left':mouseX+20, 'top':mouseY+20}).html(c[0]+'-'+c[1]+'-'+c[2]);
      };
    }
  • 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-23T21:06:59+00:00Added an answer on May 23, 2026 at 9:06 pm

    Here’s a complete, self-contained example. First, use the following HTML:

    <canvas id="example" width="200" height="60"></canvas>
    <div id="status"></div>
    

    Then put some squares on the canvas with random background colors:

    var example = document.getElementById('example');
    var context = example.getContext('2d');
    context.fillStyle = randomColor();
    context.fillRect(0, 0, 50, 50);
    context.fillStyle = randomColor();
    context.fillRect(55, 0, 50, 50);
    context.fillStyle = randomColor();
    context.fillRect(110, 0, 50, 50);
    

    And print each color on mouseover:

    $('#example').mousemove(function(e) {
        var pos = findPos(this);
        var x = e.pageX - pos.x;
        var y = e.pageY - pos.y;
        var coord = "x=" + x + ", y=" + y;
        var c = this.getContext('2d');
        var p = c.getImageData(x, y, 1, 1).data; 
        var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
        $('#status').html(coord + "<br>" + hex);
    });
    

    The code above assumes the presence of jQuery and the following utility functions:

    function findPos(obj) {
        var curleft = 0, curtop = 0;
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
            return { x: curleft, y: curtop };
        }
        return undefined;
    }
    
    function rgbToHex(r, g, b) {
        if (r > 255 || g > 255 || b > 255)
            throw "Invalid color component";
        return ((r << 16) | (g << 8) | b).toString(16);
    }
    
    function randomInt(max) {
      return Math.floor(Math.random() * max);
    }
    
    function randomColor() {
        return `rgb(${randomInt(256)}, ${randomInt(256)}, ${randomInt(256)})`
    }
    

    See it in action here:

    • https://bl.ocks.org/wayneburkett/ca41a5245a9f48766b7bc881448f9203
    // set up some sample squares with random colors
    var example = document.getElementById('example');
    var context = example.getContext('2d');
    context.fillStyle = randomColor();
    context.fillRect(0, 0, 50, 50);
    context.fillStyle = randomColor();
    context.fillRect(55, 0, 50, 50);
    context.fillStyle = randomColor();
    context.fillRect(110, 0, 50, 50);
    
    $('#example').mousemove(function(e) {
        var pos = findPos(this);
        var x = e.pageX - pos.x;
        var y = e.pageY - pos.y;
        var coord = "x=" + x + ", y=" + y;
        var c = this.getContext('2d');
        var p = c.getImageData(x, y, 1, 1).data; 
        var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
        $('#status').html(coord + "<br>" + hex);
    });
    
    function findPos(obj) {
        var curleft = 0, curtop = 0;
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
            return { x: curleft, y: curtop };
        }
        return undefined;
    }
    
    function rgbToHex(r, g, b) {
        if (r > 255 || g > 255 || b > 255)
            throw "Invalid color component";
        return ((r << 16) | (g << 8) | b).toString(16);
    }
    
    function randomInt(max) {
      return Math.floor(Math.random() * max);
    }
    
    function randomColor() {
        return `rgb(${randomInt(256)}, ${randomInt(256)}, ${randomInt(256)})`
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <canvas id="example" width="200" height="60"></canvas>
    <div id="status"></div>
    
        
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get the RGB color of a pixel using PIL? I'm
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Get a list of dates between two dates This is my sql:
Is it possible to get an array of RGB values from a local image
Possible Duplicate: OpenCV rgb value for cv::Point in cv::Mat As you know, in matlab
Is it possible to get the hex or rgb values of the color names
Possible Duplicate: How to get pixel data from a UIImage (Cocoa Touch) or CGImage
does anyone know how to (if possible) get the owner of the tag on
I am trying to work out if it is possible get JPA to persist
Possible Duplicate: Get file name from URI string in C# How to extract file

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.