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

  • Home
  • SEARCH
  • 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 6374637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:30:46+00:00 2026-05-25T01:30:46+00:00

I have a project regarding the Dijkstra’s algorithm with a canvas to draw the

  • 0

I have a project regarding the Dijkstra’s algorithm with a canvas to draw the graph. I have the following code in the JavaScript of my html file. This is a portion of JavaScript code.

var graph = {};
var i = 0;
function ondblclick(e)
{
    var cell = getpos(e) 
    canvasX = cell.x-10;
    canvasY = cell.y-10;
    graph = {i: {coord : [cell.x,cell.y]}};
    var g_canvas = document.getElementById("canvas");
    var context = g_canvas.getContext("2d");
    var cat = new Image();
    cat.src = "dot1.png";
    cat.onload = function() 
    {
        context.drawImage(cat,canvasX , canvasY);
    }
}

i = i+1
function submit()
{
for (i = 0; i<= 5; i = i + 1)
  alert(graph.i.coord);
}

here I have a canvas and the ‘ondblclick’ is the function when the user double clicks on the canvas. Canvas is used to draw nodes and edges. ‘ondblclick’ is used for drawing the nodes.

My issue is on saving the coordinate values of the nodes to the ‘graph’ in dictionary format. it accepts only the last value. How can I update/append/extend the all values to the same dictionary using the variables I and cell.x,cell.y values??

  • 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-25T01:30:47+00:00Added an answer on May 25, 2026 at 1:30 am

    You nearly had it.

    var graph = [];
    function ondblclick(e)
    {
        var cell = getpos(e) 
        canvasX = cell.x-10;
        canvasY = cell.y-10;
        graph.push({coord : [cell.x,cell.y]});
        var g_canvas = document.getElementById("canvas");
        var context = g_canvas.getContext("2d");
        var cat = new Image();
        cat.src = "dot1.png";
        cat.onload = function() 
        {
            context.drawImage(cat,canvasX , canvasY);
        }
    }
    
    function submit()
    {
    for (i = 0; i<= 5; i = i + 1)
      alert(graph[i].coord);
    }
    

    In order to get data into graph you need to push objects into an array, then loop through that array. Your problem was that you used the i variable incorrectly, thus storing (and retrieving) your coords ontop of each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of question's regarding the following project. *Scott, Recently some developers
I have got a question regarding suprepositories. Our project is set up like this:
I have C++ project (VS2005) which includes header file with version number in #define
What would be the best way to implement fluent-nhibernate regarding project architecture? I have
This question is regarding migration project. Currently the legacy Application is in ColdFusion and
What is your opinion regarding a project that will try to take a code
I have started to do the questions on Project Euler regarding lists of names
What I have in my project is the binary file that contains list of
I have a question regarding the Phonegap basic architecture design: 1.The phonegap project contains
I have the following question regarding Flex/AIR data grids: Can I access the value

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.