使用JSON文件绘制到画布 [英] Drawing to canvas using JSON file

查看:320
本文介绍了使用JSON文件绘制到画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax将json对象中的行信息调用到画布中.我是json的新手,所以我不完全确定该怎么做.这就是我到目前为止所拥有的.

I am trying to call line info from a json object into a canvas using ajax. I'm new to json, so I'm not entirely sure how to go about doing this. Here's what I have so far.

JSON

{
    "line": {
        "width": 3,
        "stroke": "#FFFFFF",
        "x1": "640.386",
        "y1": "258.163",
        "x2": "816.364",
        "y2": "258.163"
    }   
}

JS

$(document).ready(function(){
    var canvas = document.getElementById("schematic_holder");
    var ctx = canvas.getContext("2d");

    $.ajax({
        type: "GET",
        dataType: "json",
        url: "js/app/json/nst.json",
        success: function(result){
            $.each(result.line, function(){
                console.log(result.line);
            })
        },
        complete: function(){
            console.log("Complete!");
        }
    })
})

HTML

<body>
    <canvas id="schematic_holder"></canvas>
</body>

现在,当我打印到控制台时,出现一个未定义的错误.我称这个对象不对吗?我知道如何用画布绘制线条,我只是对如何从JSOn文件中绘制线条感到困惑.预先感谢.

Right now, when I print to the console, I'm getting an undefined error. Am I calling the object wrong? I know how to get the lines to draw with canvas, I'm just confused about how to do so from a JSOn file. Thanks in advance.

推荐答案

尝试将整个json文件包装在方括号[]中,就像这样

Try wrapping the entire json file in brackets [], like this

[{
    "line": {
        "width": 3,
        "stroke": "#FFFFFF",
        "x1": "640.386",
        "y1": "258.163",
        "x2": "816.364",
        "y2": "258.163"
    }   
}]

这篇关于使用JSON文件绘制到画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆