【问题标题】:About html5 canvas figure关于html5画布图
【发布时间】:2018-04-20 16:05:43
【问题描述】:

如果我不确定我是否要从服务器接收矩形或菱形或圆形或梯形或多边形坐标,那么我应该选择哪个图形在 Angular 框架中的画布上绘制对象?

【问题讨论】:

    标签: javascript angular html5-canvas


    【解决方案1】:

    您可以执行以下操作以避免任何混淆。

    • 每个图表都有一组特定的坐标。比如 r矩形有 4 个,正方形有 4 个,圆形有无限点(有一个中心和一个用于绘制的半径)。
    • 因此,您可以在 javascript 中检查您正在获取哪个图表的坐标,并相应地从服务器响应中获取坐标。
    • 或者您可以在服务器响应中添加图表类型的附加属性,并在javascript中对其进行过滤。
    • 您可以轻松地从服务器发送图表类型,您可以使用

    switch 语句或 If 条件来检查图表类型并相应地从服务器响应中获取结果,如果您在 json 中收到响应,那么它将是这样的。

    if(response.data.diagram_type === "square"){
       // now you will know what attributes you need for this shape
       // assuming that you have sent coordinates in an array from server and each array has (x, y) so create objects {} not arrays [].
       var coordinate_0 = {};
       var coordinate_1 = {};
       var coordinate_2 = {};
       var coordinate_3 = {};
       coordinate_0 = data.response.coordinates[0];
       coordinate_1 = data.response.coordinates[1];
       coordinate_2 = data.response.coordinates[2];
       coordinate_3 = data.response.coordinates[3];
    }else if (response.data.diagram_type === "circle"){
       var coordinate_center = {};
       coordinate_center = data.response.circle_center;
       var radius= data.response.circle_radius;
       // create your shape accordingly.
    }
    // similarly for other diagram.
    
    // hope it helps.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2012-04-27
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多