简体   繁体   English

如何在bonsai.js上使用变量?

[英]how to use variable on bonsai.js?

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" . 它不起作用,但是如果我删除变量并直接使用字符串,例如fillColor:"#ef6360" Why? 为什么?

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) . 据我所知,您无法访问code内的变量,因为根据本文的文档(请阅读第一个说明),其上下文对浏览器的访问受到限制。

This is what the docs say exactly. 这正是文档所说的。

Note : The runner context has limited access to browser functionality (eg no DOM access) because in most cases the Bonsai code is executed in a worker. 注意 :运行程序上下文对浏览器功能的访问受到限制(例如,没有DOM访问权限),因为在大多数情况下,盆景代码是在工作程序中执行的。 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 ). 因此,您只能使用为工作程序提供的Bonsai API和常规JS函数( 有关详细信息,请参阅MDN上可用于工作程序的函数 )。 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 . 如果要将初始数据传递到运行程序上下文,则可以在本页底部阅读有关该数据的信息,或者如果您想通过Bonsai动态地操作DOM,则应该查看Communication概述

If there is way to access the window scope from inside a web worker, then it is possible. 如果可以从Web Worker内访问窗口范围,则可以。 (I do not know if it is possible) (我不知道是否可能)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM