Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load JSON data into an A-Frame component?

Tags:

aframe

What's the best way to load a custom JSON file as data into an A-frame component ? For example, the JSON file may contain coordinates of points. I want to load the file as an asset and use the parsed json object in the component.

{"coordinates": [{"x": 0, "y": 1, "z": 2}, // ...]}
like image 591
ngokevin Avatar asked Jan 09 '17 10:01

ngokevin


1 Answers

You can define your own property type in the schema that parses data how you wish.

To parse JSON from a component, create a parse function that does a JSON.parse:

AFRAME.registerComponent('foo', {
  schema: {
    jsonData: {
      parse: JSON.parse,
      stringify: JSON.stringify
    }
  }
});

Then use the component:

el.setAttribute('foo', 'jsonData', yourJsonData);

Or:

<a-entity foo='jsonData: {"coordinates": [{"x": 0, "y": 1, "z": 2}]}'></a-entity>
like image 101
ngokevin Avatar answered Sep 30 '22 14:09

ngokevin