I have a JSON file containing multiple points that need to be drawn gradually. I came across a helpful tutorial on this topic, but it only covers drawing a single line. What I'm looking to achieve is sequentially drawing several lines with different starting points. Below is the structure of the JSON file:
{
"data": [
{
"line": {
"color": "#96c23b",
"points": [
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 3
},
{
"x": 4,
"y": 5
},
{
"x": 7,
"y": 8
}
],
"width": 2.0
},
"type": "line",
"line_id": "1"
},
{
"line": {
"color": "#DF5453",
"points": [
{
"x": 33,
"y": 34
},
{
"x": 34,
"y": 35
},
{
"x": 38,
"y": 39
},
{
"x": 40,
"y": 42
},
{
"x": 45,
"y": 46
}
],
"width": 5.0
},
"type": "line",
"line_id": "2"
}
]
}
The speed at which the lines are drawn is not a concern for me.
I am able to parse the JSON and draw the lines in canvas without animation using the following jQuery code:
var points_list = {"data":[
{"line":{"color":"#96c23b","points":[{"x":1,"y":2},{"x":2,"y":3},{"x":4,"y":5},{"x":7,"y":8}],"width":2.0},"type":"line","line_id":"1"},
{"line":{"color":"#DF5453","points":[{"x":33,"y":34},{"x":34,"y":35},{"x":38,"y":39},{"x":40,"y":42},{"x":45,"y":46}],"width":5.0},"type":"line","line_id":"2"}
]}
function drawLines() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d");
$.each(points_list.data, function (key, value) {
var info = value.line;
var color = info.color;
var width = info.width;
var points = info.points;
context.beginPath();
context.moveTo(points[0].x, points[0].y);
context.lineWidth = width;
context.strokeStyle = color;
context.fillStyle = color;
for (var p = 1; p < points.length; p++) {
context.lineTo(points[p].x, points[p].y);
}
context.stroke();
});
}