Search code examples
javascriptmathcanvasgraphing

Stretching a Graph to Fill the Canvas Element in JavaScript


Quick question involving javascript canvas... I have a set points (connected with a line) I want to graph on a 400x300 canvas element. I will constantly be adding more points. I need the line to stretch to fill the entire canvas (leaving no unnecessary space).

Example:

enter image description here

into this:

enter image description here

Thanks! C.Ruhl


Solution

  • You want to find the step by doing canvasWidth / (number of points - 1)

    and adding X += step each time.

    Example here:

    http://jsfiddle.net/pDDTQ/