开发者

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:

Stretching a Graph to Fill the Canvas Element in JavaScript

into this:

开发者_运维问答

Stretching a Graph to Fill the Canvas Element in JavaScript

Thanks! C.Ruhl


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/


Distinguish between internal canvas size and visible size. 400x300 is your visible size and set by style="width:400px; height:300px". Everytime there is new point (e.g. 400,500) you set canvas.width=400; canvas.height=500; and replot the whole graph. From a certain point you might want to adjust the width of the line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜