I'm trying to store the JSON data from the file named a5.json in a variable called rects. However, I am not sure if I did it correctly. My goal is to display rectangles using the rects variable. Can someone guide me on how to achieve that?
<script src="a5.json" defer></script>
<canvas id="c" width="500" height="500"></canvas>
var canvas = document.getElementById("c");
var context = canvas.getContext("2d");
var json = [{x: "50", y: "50", w: "100", h: "50", f: true }, { x: "50", y: "150", w: "100", h: "50", f: false }];
var rects = json;
context.lineWidth = 2;
context.strokeStyle = "#FF0000";
context.fillStyle = "#FF0000";
json.forEach(shape =>
{
context[shape.f === true ? 'fillRect' : 'strokeRect'](shape.x, shape.y, shape.w, shape.h);
}
);