【问题标题】:Sorting list items based on postion on canvas in angularjs根据angularjs中画布上的位置对列表项进行排序
【发布时间】:2016-08-03 00:14:51
【问题描述】:

我正在使用 angularjs 和 javascript 在具有以下 json 格式的 html5 画布上绘制矩形。 我需要根据 x,y 位置对数组进行排序。

{
"obj0": {
"outerRects": [
  {
    "outerRectRoi": {
      "x1": 0,
      "x2": 650,
      "x3": 350,
      "x4": 340,
      "y1": 0,
      "y2": 680,
      "y3": 0,
      "y4": 680
    },
    "line": [
      {
        "lineRoi": {
          "x1": 471,
          "x2": 460,
          "y1": 1,
          "y2": 680
        },
        "innerRect": [
          {
            "innerRoi": {
              "x1": 368,
              "x2": 390,
              "y1": 50,
              "y2": 55
            }
          },
          {
            "innerRoi": {
              "x1": 368,
              "x2": 390,
              "y1": 70,
              "y2": 74
            }
          }
        ]
      },
      {
        "lineRoi": {
          "x1": 202,
          "x2": 197,
          "y1": 1,
          "y2": 680
        }
      },
      {
        "lineRoi": {
          "x1": 380,
          "x2": 372,
          "y1": 1,
          "y2": 680
        }
      }
    ]
  },
  {}
]
},
"obj1": {}
}

这些是根据画布上的 Roi 或位置进行排序的条件:

一个。外部矩形(outerRects)必须从左到右排序。它们可以在多行中,如下所示

1  2

3  4

b.这些线也将在外部矩形内从左到右排序。

c。内部矩形将根据它们在行顶部的位置从上到下排序。

这将是预期的 json 结果

{
"obj0": {
"outerRects": [
  {
    "outerRectRoi": {
      "x1": 0,
      "x2": 650,
      "x3": 350,
      "x4": 340,
      "y1": 0,
      "y2": 680,
      "y3": 0,
      "y4": 680
    },
    "line": [
      {
        "lineRoi": {
          "x1": 202,
          "x2": 197,
          "y1": 1,
          "y2": 680
        },
        "innerRect": []
      },
      {
        "lineRoi": {
          "x1": 380,
          "x2": 372,
          "y1": 1,
          "y2": 680
        },
        "innerRext": []
      },
      {
        "lineRoi": {
          "x1": 471,
          "x2": 460,
          "y1": 1,
          "y2": 680
        },
        "innerRect": [
          {
            "innerRoi": {
              "x1": 368,
              "x2": 390,
              "y1": 70,
              "y2": 74
            }
          },
          {
            "innerRoi": {
              "x1": 368,
              "x2": 390,
              "y1": 50,
              "y2": 55
            }
          }
        ]
      }
    ]
  },
  {}
]
},
"obj1": {}
}

如何使用 angularjs 和 javascript 做到这一点?

谢谢

【问题讨论】:

  • 请添加想要的结果。并添加参考点。
  • 能否请您检查我在问题中的编辑

标签: javascript angularjs html sorting canvas


【解决方案1】:

您可以根据需要迭代各种数组并使用其属性对子数组进行排序。

var object = { "obj0": { "outerRects": [{ "outerRectRoi": { "x1": 0, "x2": 650, "x3": 350, "x4": 340, "y1": 0, "y2": 680, "y3": 0, "y4": 680 }, "line": [{ "lineRoi": { "x1": 471, "x2": 460, "y1": 1, "y2": 680 }, "innerRect": [{ "innerRoi": { "x1": 368, "x2": 390, "y1": 50, "y2": 55 } }, { "innerRoi": { "x1": 368, "x2": 390, "y1": 70, "y2": 74 } }] }, { "lineRoi": { "x1": 202, "x2": 197, "y1": 1, "y2": 680 } }, { "lineRoi": { "x1": 380, "x2": 372, "y1": 1, "y2": 680 } }] }, {}] }, "obj1": {} }

object.obj0.outerRects.forEach(function (a) {
    if (a.line) {
        a.line.sort(function (a, b) {
            return a.lineRoi.x1 - b.lineRoi.x1;
        });
        a.line.forEach(function (b) {
            if (b.innerRect) {
                b.innerRect.sort(function (a, b) {
                    return a.innerRoi.y1 - b.innerRoi.y1;
                });
            }
        });
    }
});

document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

【讨论】:

  • 谢谢。这非常有用。我有一个查询。 outerRects 也将是必须根据它们的位置(x,y)排序的项目列表。并且还随机抽取。那么如何从左到右对它们进行排序呢?
  • 我的错。但是必须对内部矩形进行排序,使得具有较高值的​​点 y(因为它必须从上到下绘制)必须是列表中的第一个。我已经编辑了这个问题。 :)
  • 您可以更改返回值,如果排序顺序是相反的return a.lineRoi.x1 - b.lineRoi.x1;。对象obj0obj1无法排序,因为对象没有顺序,还是说outerRec里面的项目?
  • 是的。如果您注意到 outerRects 是一个对象数组,它具有属性 outerRectRoi ,即画布上的 x,y 坐标。我也需要从画布的左到右对它们进行排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多