Wednesday 17 May 2017

Drawing graphics on canvas in node-js/Express

I want to draw a chart on canvas element. My Jade file contains following:

div
   canvas

In my index.js file in node.js web application, I tried the following:

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.beginPath();
context.strokeStyle = "rgba(255, 0, 0, 1)";
height = canvas.height;
width = canvas.width;
context.moveTo(0, 0);
context.lineTo(width, height);
context.lineWidth = 1;
context.stroke();

However, it fails to recognize "document". My question is how do I obtain the reference to "canvas" in my .js file? Note that the actual chart would be more complex than the simple line drawing code shown above for brevity. The data for the chart is generated in index.js file at the server end. So the chart will be created on the server.



via abcom

No comments:

Post a Comment