访问对象上的 p5.js 函数和属性,而不是在全局范围内?
Access p5.js functions and properties on an object, instead of in the global scope?
有很多p5.js变量,而且它们都是全局定义的,这会严重破坏全局范围。有没有办法将它们存储在一个对象中,像这样:
const p5 = [something goes here]
p5.setup
p5.draw
p5.preload
p5.color
p5.fill
p5.stroke
// instead of
setup
draw
preload
// ...etc
有办法吗?
我认为您正在寻找的是 p5.js 的使用模式,称为 instance mode. In this mode you create an instance of the p5
object,带有定义草图的回调。
此外,如果您使用 TypeScript and/or 将 Node.js 包编译为包或模块以在浏览器中使用,那么您可以使用 npm 包 p5 and @types/p5 这样您可以 import/require p5 作为一个模块,如果您使用的是 TypeScript,则可以进行类型检查。
有很多p5.js变量,而且它们都是全局定义的,这会严重破坏全局范围。有没有办法将它们存储在一个对象中,像这样:
const p5 = [something goes here]
p5.setup
p5.draw
p5.preload
p5.color
p5.fill
p5.stroke
// instead of
setup
draw
preload
// ...etc
有办法吗?
我认为您正在寻找的是 p5.js 的使用模式,称为 instance mode. In this mode you create an instance of the p5
object,带有定义草图的回调。
此外,如果您使用 TypeScript and/or 将 Node.js 包编译为包或模块以在浏览器中使用,那么您可以使用 npm 包 p5 and @types/p5 这样您可以 import/require p5 作为一个模块,如果您使用的是 TypeScript,则可以进行类型检查。