Reputation: 535
var star = document.getElementById("star");
var color = "#ef6360";
bonsai.run(star, {
code: function() {
new Star(30, 30, 20, 5, 0.7).attr({
fillColor: color
}).addTo(stage);
}
});
it doesn't work, but it'll work if I remove the variable and directly use the string, like fillColor:"#ef6360"
. Why?
Upvotes: 1
Views: 337
Reputation: 33628
As far as I know, you cannot access variables inside code
because its context has limited access to browser as per the documentation here (read the first note).
This is what the docs say exactly.
Note: The runner context has limited access to browser functionality (e.g. no DOM access) because in most cases the Bonsai code is executed in a worker. Therefore you are limited to use the provided Bonsai API and the normal JS functions that are provided for a worker (see Functions available for workers on MDN for details). If you want to pass initial data to the runner context you can read about that at the bottom of this page or if you want to dynamically manipulate the DOM through Bonsai you should have a look at the Communication overview.
If there is way to access the window scope from inside a web worker, then it is possible. (I do not know if it is possible)
Hope this helps.
Upvotes: 2