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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:03:32+00:00 2026-06-01T22:03:32+00:00

I am trying to generate and draw shapes using HTML 5 canvas and JavaScript.

  • 0

I am trying to generate and draw shapes using HTML 5 canvas and JavaScript. I am trying to make it as DRY as possible but having some issues. I have the code as follows:

var sections = {
  "w_end" : {
    "name":"W End",
    "id":"w_end",
    "dimensions": {"move_to":[0,0], "coords":[[0,50], [50,50], [0,50]]}
  }
}
$(document).ready(function() {
  $.each(sections, function(k,v){
    make_shape(k, v);
  })
});

function make_shape(id, attributes) {
  var holder = document.getElementById('room');
  var grid_canvas = document.createElement("canvas");
  holder.appendChild(grid_canvas);
  grid_canvas.setAttribute("id", id);
  var item = grid_canvas.getContext("2d");
  item.fillStyle = "rgb(154,250,50)";
  item.beginPath();
  item.moveTo(attributes.dimensions.move_to[0],attributes.dimensions.move_to[1]);
  $.each(attributes.dimensions.coords, function(k ,v){
    item.lineTo(v[0],v[1]);
  });
  item.fill();
  item.closePath();
}

This does not seem to work when pulling the lineTo values from the json. I can switch the lineTo(v[0],v[1]) for lineTo(50,75) inside the lopp and it generates a filled shape. I am not great at JavaScript as you can tell. Does anyone know what the issue is here and maybe give some advice on generating multiple shapes from some sort of list?

Cheers

Tony

  • 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-01T22:03:33+00:00Added an answer on June 1, 2026 at 10:03 pm

    I played around with your code, it is flawless. There is no triangle drawn because somehow the path going back to itself fills nothing. Place only the first two vertices and it is OK.

    "dimensions": {"move_to":[0,0], "coords":[[0,50], [50,50]]}
    

    See this fiddle: http://jsfiddle.net/Nm7UQ/

    Note that I commented out document.ready.

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

Sidebar

Related Questions

I'm trying to draw shapes that will move around the screen using the LunarLander
I'm trying to generate a form using the form_for helper in RoR but I
I'm spinning my wheels trying to draw some shapes on the screen and failing.
Im trying to generate a color gradient using ColdFusion. My current code below works
I am trying to generate equivalent MD5 hashes in both JavaScript and .Net. Not
I'm trying to generate some code at runtime where I put in some boiler-plate
I am having a problem with my java code. I'm trying to make it
I'm using PDFLib v-8 and trying to generate triangle using that path point. I'm
TL;DR I'm trying to dynamically generate an asset URL using HTML5 data elements, on
I'm trying to draw a nine patch onto a Canvas object on the Android.

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.