【发布时间】:2016-01-07 17:16:01
【问题描述】:
我最近从使用可汗学院的 processing.js 环境飞跃到真正的交易,有点困惑。
我有一个简单的 processing.js 程序,基本上画了一个圆,我希望这个圆的大小由画布的宽度决定。
如果我在 processing.js 函数(如 setup)中打印宽度,则会显示正确的 500px 宽度。不幸的是,每当我尝试在 processing.js 函数之外访问 width 属性时,它会显示默认的 100px 大小,即使画布本身是 500px 宽。
我认为我可能使用了相当丑陋的处理和 javascript 组合,这可能是我问题的根源。非常感谢您的帮助!
Processing.js
///* PROCESSING.JS SETUP *///
void setup() {
size(500, 500);
println(width); // WORKS! :)
}
println(width); // DOESN'T WORK... :(
///* GLOBAL VARIABLES *///
var moleculeQuantity = 1;
///* OBJECT CONSTUCTORS *///
var Molecule = function(x, y) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
Molecule.prototype.draw = function() {
noStroke();
fill(88, 91, 183);
ellipse(this.x, this.y, 70, 70);
fill(225, 227, 228);
ellipse(this.x, this.y, 40, 40);
};
// Fill array with molecule objects
var molecules = [];
for (var i = 0; i < moleculeQuantity; i++) {
molecules[i] = new Molecule(200, 100);
}
///* DRAW FUNCTION *///
void draw() {
background(225, 227, 228);
molecules[0].draw();
}
【问题讨论】:
-
在全局范围内写
var width;? -
感谢 Jeremy,但@Kevin 指出了为什么这在下面不一定有效。在调用 setup 函数之前,全局声明的宽度仍未定义。
标签: javascript canvas processing processing.js