我可以绘制HTML5画布x/y点编程使用jQuery与外部JSON数据
Can I plot HTML5 Canvas x/y points programatically using jQuery with external JSON data?
我有一个HTML5画布'加入点'类型的东西- 2线与点在他们的角度点-这很好,但我想用外部JSON数据编程地绘制X坐标(从'本地'服务器拉出,所以不需要JSONP) -我希望我解释清楚…
我没有尝试将JSON数据转换为新的DOM元素,而是需要将数据应用于映射画布坐标的实际脚本。理想情况下,我想使用jQuery,我的猜测是,我将需要通过。getjson()解析JSON对象,但这是我需要一些帮助的地方。
X和Y坐标目前都是用画布脚本中的硬编码变量初始化的,但我希望JSON数据以编程方式解析为X变量(Y协同词可以保持硬编码,并且对两行都有效)。
这里是我到目前为止的一个小提琴:http://jsfiddle.net/ByT58/6/
这里是供参考的标记/脚本-并提前感谢任何帮助!: HTML:<div class="canvas-wrap">
<canvas id="myCanvas" width="200" height="115"></canvas>
</div>
下面是外部JSON的样子:
{
"red": {
"r01x": 20,
"r02x": 149,
"r03x": 50
},
"blue": {
"b01x": 80,
"b02x": 179,
"b03x": 20
}
}
JS:
var ctx = document.getElementsByTagName('canvas')[0].getContext('2d');
// set attributes for all circles
var radius = 7;
// set attributes for all lines
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
// set X co-ords for Red
var r01x = 20;
var r02x = 149;
var r03x = 50;
// set X co-ords for Blue
var b01x = 80;
var b02x = 179;
var b03x = 20;
// Set default Y coordinates for both Red and Blue
var y01 = 20;
var y02 = 50;
var y03 = 100;
// RED dots
ctx.beginPath();
ctx.fillStyle = "#E51919";
ctx.arc(r01x, y01, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = "#E51919";
ctx.arc(r02x, y02, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = "#E51919";
ctx.arc(r03x, y03, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
// RED line
ctx.beginPath();
ctx.moveTo(r01x, y01);
ctx.lineTo(r02x, y02);
ctx.lineTo(r03x, y03);
ctx.strokeStyle = "#E51919";
ctx.stroke();
ctx.closePath();
// BLUE dots
ctx.beginPath();
ctx.fillStyle = "#133175";
ctx.arc(b01x, y01, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = "#133175";
ctx.arc(b02x, y02, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = "#133175";
ctx.arc(b03x, y03, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
// BLUE line
ctx.beginPath();
ctx.moveTo(b01x, y01);
ctx.lineTo(b02x, y02);
ctx.lineTo(b03x, y03);
ctx.strokeStyle = "#133175";
ctx.stroke();
ctx.closePath();
如果您要将JSON数据放入表单:
{
red: { color: "#E51919", x: [20,149,50] },
blue: { color: "#133175", x: [80,179,20] }
}
你的绘制函数看起来像(jsFiddle在这里):
function draw(data) {
var ctx = document.getElementsByTagName('canvas')[0].getContext('2d');
// set attributes for all circles
var radius = 7;
// set attributes for all lines
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
var y = [20,50,100];
for(var key in data) {
var x = data[key].x;
ctx.fillStyle = data[key].color;
for(var i = 0; i < x.length; ++i) {
ctx.beginPath();
ctx.arc(x[i], y[i], radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
}
ctx.beginPath();
ctx.moveTo(x[0], y[0]);
ctx.lineTo(x[1], y[1]);
ctx.lineTo(x[2], y[2]);
ctx.strokeStyle = data[key].color;
ctx.stroke();
ctx.closePath();
}
}
draw({
red: { color: "#E51919", x: [20,149,50] },
blue: { color: "#133175", x: [80,179,20] }
});
使用JQuery从服务器检索JSON数据使用JQuery . getjson()或JQuery .ajax()
例如(没有错误处理…):
$.getJSON('path/data.json', function(data, textStatus, jqXHR) {
console.log(textStatus);
draw(data);
})
.fail(function( jqXHR, textStatus, errorThrown) { console.log(textStatus + " :: " + errorThrown ); });
相关文章:
- 单击元素外部时进行JQuery
- 什么更好?使用iframe或jQuery之类的东西在外部网站中加载HTML文件
- 使用 jquery 获取外部父 UL
- 使用jquery删除输入框上的外部处理程序/库
- 使用 jQuery,当我在输入框外部单击时,如何更改输入元素的值
- 如何使用外部Javascript库(如jQuery)重命名Google闭包样式表
- jQuery对请求图像的外部文本进行查询
- ajax外部url加载完成后调用jquery函数
- 什么'这是使用jQuery将一些文本锚定到外部链接的最简单方法
- 无法从 jquery 中的外部文件调用特定内容
- 在扩展 jQuery 之前加载外部资源
- 内部函数不会为外部函数在 jQuery 中动态创建的元素赋值
- 我的函数不起作用,Ajax调用,JQuery,调用外部?我不知道
- jquery .load() 不加载外部 JS
- 捕获元素jquery外部的mousedown事件
- jQuery:外部插件调用选项中的 if 条件
- Jquery 外部页面平滑滚动
- 无法访问函数jquery外部的变量
- JQuery外部链接确认移动兼容性
- 初始化在jQuery外部声明的变量$.getJSON回调函数