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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:05:11+00:00 2026-06-14T10:05:11+00:00

Is it possible to reference graphics drawn on an HTML5 canvas element with jQuery?

  • 0

Is it possible to reference graphics drawn on an HTML5 canvas element with jQuery? Here’s a function I wrote for EaselJS that iterates through an object containing arrays of vector coordinates:

function newShape(obj,shape,coords){
    obj.graphics.beginFill('green');
    obj.alpha = 0.05;
    for (var i=0; i<coords[shape].length; i+=2) {
        var arr = coords[shape].slice(i,i+2);
        obj.graphics.lineTo(arr[0],arr[1]);
    }
    obj.graphics.closePath();
    obj.graphics.endFill();
    stage.addChild(obj);
}

The coords object looks something like this with 100s of points:

var coords = {};

coords['0']  = [214,266,214,291,194,291];
coords['1']  = [243,1,254,2,278,9,290,14];
coords['2']  = [109,112,116,114,129,117];

And the stage:

canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);

shape1 = new createjs.Shape();
shape2 = new createjs.Shape();
shape3 = new createjs.Shape();

newShape(shape1,'shape1',coords);
newShape(shape2,'shape2',coords);
newShape(shape3,'shape3',coords);

I can add listeners to the rendered shape for click/touch events, however I’d like to have the ability to reference each graphic as a DOM element for use with plugins like Bootstrap Popover and niceScroll eg.. $(shape1).popover('show');. I’ve been actively working on a solution with no success, any help would be much appreciated!

  • 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-06-14T10:05:12+00:00Added an answer on June 14, 2026 at 10:05 am

    Option 1 :

    From Shmiddty’s post, a programmatic approach to referencing with 2d context:

    function Shape(can) {
        this.x = 0;
        this.y = 0;
        this.width = 0;
        this.height = 0;
        this.fill = "rgba(0,0,0,0)";
        this.parent = can;
    
        this.draw = function(ctx) {
            if (!$(me.parent).is(':visible')) return;
    
            var parts = me.fill.split(',');
            parts[3] = $(me.parent).css("opacity") + ')';
            me.fill = parts.join(',');
    
            ctx.fillStyle = me.fill;
            ctx.fillRect(me.x, me.y, me.width, me.height);
        };
    
        var me = this;
    }
    
    (function() {
        var can = $("#sandbox")[0],
            ctx = can.getContext('2d'),
            wid = can.width,
            hei = can.height,
            sha = new Shape($('<div id="box"/>').appendTo('body'));
    
        sha.x = 0;
        sha.y = 0;
        sha.width = 50;    
        sha.height = 50;
        sha.offsetWidth = 50;
        sha.offsetHeight = 50;
        sha.fill = "rgba(000,111,222,1)";
    
        (function draw(){
            ctx.clearRect(0,0,wid,hei);
            sha.draw(ctx);
            webkitRequestAnimationFrame(draw);
        })();
    
    
        $(can).click(function(){
            $(sha.parent).fadeToggle();
        });
        $('#box').click(function(){
            $(this).fadeToggle();
        });
    })();​  
    

    Option 2 :

    I was able to track x,y coords from click and touch events by applying CSS to a hidden DOM element.

    Calling a bootstrap popover :

    $("#canvas").click(function(e) {
        var x = e.pageX;
        var y = e.pageY;
    
        if ($.inArray(x, coords['shape1']) !== -1 || $.inArray(y, coords['shape1']) !== -1){
            $("#myObj").css({'position':'absolute','top':y,'left':x}).popover({
                trigger: 'click',
                placement:'top',
            }).popover('show');
        }
    });
    stage.update();
    

    HTML:

    <canvas id="canvas" width="375" height="322"></canvas>
    <span class="myObj" data-content="lorem ipsum content" data-original-title="pop-title"></span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: pass by reference not working I was going through some of the
Possible Duplicate: Reference assignment operator in php =& I saw this example: function __construct()
Possible Duplicate: Reference - What does this symbol mean in PHP? I have a
Possible Duplicate: Reference unit tests for common data structures? I'm trying to implement the
Possible Duplicate: Reference - What does this symbol mean in PHP? What's the difference
Is it possible to reference an outer query in a subquery with MySQL? I
I Have this error message : Possible unitended reference comparison; to get a value
Possible Duplicate: Ambiguous reference in WCF and client application I am using few classes
Possible Duplicate: Object reference not set to an instance of an object errors when
Possible Duplicate: Returning reference to a local variable I happened to find this code

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.