I'm trying to draw through on a HTML5 canvas
. I managed to draw on the canvas but I need to do it dynamically. This is my JavaScript
code:
var c=document.getElementById("yellow");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(247,373);
ctx.lineTo(0,390);
ctx.lineTo(5,21);
ctx.lineTo(245,0);
ctx.lineTo(247,373);
ctx.closePath();
ctx.fillStyle="#ffca05";
ctx.globalAlpha=0.7;
ctx.strokeStyle = '#ffca05';
ctx.fill();
ctx.stroke();
I need to read the data from this json array
and draw using these coordinates.
[{"x":"247", "y":"373"}, {"x":"0", "y":"390"},{"x":"5", "y":"21"},{"x":"245", "y":"0"}, {"x":"247", "y":"373"}]
I'm trying to draw through on a HTML5 canvas
. I managed to draw on the canvas but I need to do it dynamically. This is my JavaScript
code:
var c=document.getElementById("yellow");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(247,373);
ctx.lineTo(0,390);
ctx.lineTo(5,21);
ctx.lineTo(245,0);
ctx.lineTo(247,373);
ctx.closePath();
ctx.fillStyle="#ffca05";
ctx.globalAlpha=0.7;
ctx.strokeStyle = '#ffca05';
ctx.fill();
ctx.stroke();
I need to read the data from this json array
and draw using these coordinates.
[{"x":"247", "y":"373"}, {"x":"0", "y":"390"},{"x":"5", "y":"21"},{"x":"245", "y":"0"}, {"x":"247", "y":"373"}]
Share
Improve this question
edited Oct 11, 2019 at 1:29
Sebastian Simon
19.6k8 gold badges61 silver badges84 bronze badges
asked Apr 27, 2015 at 19:53
Bruno LaiseBruno Laise
1172 silver badges6 bronze badges
1
-
1
Have you tried it with a
for
loop that repeatedly executesctx.lineTo(json[i].x,json[i].y);
? (The firstctx.lineTo
acts like actx.moveTo
). – Sebastian Simon Commented Apr 27, 2015 at 19:58
2 Answers
Reset to default 3All you have to do is iterate over the JS object in a for
loop and repeatedly execute ctx.lineTo()
. Note: the first ctx.lineTo()
after a ctx.beginPath()
acts like a ctx.moveTo()
.
You can run this code snippet to verify the result:
var c=document.getElementById("yellow");
var ctx=c.getContext("2d");
var json=[
{"x":"247", "y":"373"},
{"x":"0", "y":"390"},
{"x":"5", "y":"21" },
{"x":"245", "y":"0" },
{"x":"247", "y":"373"}
];
ctx.beginPath();
for(var i in json){
ctx.lineTo(json[i].x,json[i].y);
}
ctx.closePath();
ctx.fillStyle="#ffca05";
ctx.globalAlpha=0.7;
ctx.strokeStyle="#ffca05";
ctx.fill();
ctx.stroke();
<canvas id="yellow" width="250" height="400"></canvas>
PS: I can notice that the top corner at the top edge of the canvas (and presumably the left one as well) are a bit cut off. Just add 1
or so to each coordinate to fix this:
[
{"x":"248", "y":"374"},
{"x":"1", "y":"391"},
{"x":"6", "y":"22" },
{"x":"246", "y":"1" },
{"x":"248", "y":"374"}
];
I cannot ment just yet, but as to the matter of reading an external JSON file: If your file is available on some URL, you can use AJAX through jQuery to easily get the JSON data you need, parse it and store it in a variable local to your webpage/application. Quick example:
var myJSON; //the json object data you're going to draw to canvas with
$(document).ready(function(){
$.ajax({
url: "myurl./myjsonfile",
dataType: "text",
success: function(data) {
myJSON = $.parseJSON(data);
drawToCanvas(myJSON); //you can, perhaps, make the code
//Xufox provided into a function that
//you pass your myJSON var to once it
//is loaded.
}
});
})
The '$.ajax' call will handle obtaining the data from the URL you specified above, and will execute the 'drawToCanvas()' method only once the data has been loaded and passed to myJSON (which is then passed to the method).
You could reference the parameter directly in your function, or store it in a local variable:
function drawToCanvas(jsonObj) {
var json = jsonObj;
ctx.beginPath();
for(var i in json){
ctx.lineTo(json[i].x,json[i].y);
}
...
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744769676a4592682.html
评论列表(0条)