javascript - Unable to get foreach working with canvas -


my setup nodejs connected mongodb using ejs.

i have bench/lab mapping app i'm making , using lodash foreach function populate benches based on database objects. have working dynamically build svg image of map, want start using canvas element instead, can't work.

here's working svg version:

<svg version="1.1"  xmlns="http://www.w3.org/2000/svg"   xmlns:xlink="http://www.w3.org/1999/xlink"   x="0px"   y="0px"  viewbox="0 0 520 1820"   enable-background="new 0 0 520 1820"   xml:space="preserve">  <g id="background"> <rect fill="#d1d3d4" stroke="#000000" stroke-miterlimit="10" width="520" height="1820"/> </g>     <!--run foreach each bench entry in db-->     <% _.each(labs, function(lab) { %>      <!--set bfill variable null-->         <% bfill = null; %>      <!--check if bench set active. if true, sets bfill green, if not sets grey-->         <% if (lab.active == true) { this.bfill='#008000'; this.labactive+1; }else{ this.bfill='#c0c0c0' ; } %>                  <!--fill in rect info db fields-->                 <g id="<%= lab.number %>">                     <title>- lab bench info -                      bench: <%= lab.number %>                     user: <%= lab.currentuser %>                     </title>                     <rect                            class="bench"                            id="<%= lab.number%>"                            x="<%= lab.x %>"                            y="<%= lab.y %>"                            fill="<%= this.bfill %>"                            stroke="#000000"                            stroke-width="0.25"                            stroke-miterlimit="10"                            width="<%= lab.w %>"                            height="<%= lab.h %>"                             />                 </g>                         <% }) %>  </svg> 

here not working canvas version:

<!--begin canvas--> <canvas id="labcanvas" width="520" height="1820" style="border:5px solid #000000"></canvas>     <script>         var c = document.getelementbyid("labcanvas");         var ctx = c.getcontext("2d");          //      run foreach each bench entry in db     _.each(labs, function(lab) {          //      fill in rect info db fields                  ctx.rect(lab.x, lab.y, lab.w, lab.h);     ctx.stroke();                   }) </script> 

am missing or doing wrong?

after lot of poking able figure out issue. i'm putting answer here in case runs across similar issue.

since i'm using sailsjs ejs, pages being rendered on server-side , sent client. fine when building svg version since making using ejs calls, new canvas version using script tags , page being rendered on clients-side. object array server-side controller defined (labs), unavailable client. solved doing json.stringify put server (labs) array client-side array. mind you, method dirty , insecure, in case data it's grabbing not confidential in nature.

here working version of code:

<!--begin canvas--> <canvas id="labcanvas" width="520" height="1820" style="border:5px solid #000000"></canvas>     <script>         var labs = <%- json.stringify(labs) %>          var c = document.getelementbyid("labcanvas");         var ctx = c.getcontext("2d");          //      run foreach each bench entry in db     labs.foreach(function(lab) {          //      fill in rect info db fields                  ctx.rect(lab.x, lab.y, lab.w, lab.h);     ctx.stroke();                   }); </script> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -