我发现了一个可以使CANVAS更容易工作的API。它允许非常容易地选择和修改画布上的各个元素。这是EaselJS。 API文档在这里。 http://easeljs.com/docs/画架JavaScript结构说明
到目前为止,我对这个API还不错。令我困惑的是其中的一些javascript。粗体或* *内的部分(无法获得格式化) 这是什么样的JavaScript结构?
(函数(目标){...的内容...})(位图) 和内容,它引用位,这也是之外的东西。
这里是代码
for(var i = 0; i < 100; i++){
bitmap = new Bitmap(image);
container.addChild(bitmap);
bitmap.x = canvas.width * Math.random()|0;
bitmap.y = canvas.height * Math.random()|0;
bitmap.rotation = 360 * Math.random()|0;
bitmap.regX = bitmap.image.width/2|0;
bitmap.regY = bitmap.image.height/2|0;
bitmap.scaleX = bitmap.scaleY = bitmap.scale = Math.random()*0.4+0.6;
bitmap.mouseEnabled = true;
bitmap.name = "bmp_"+i;
(功能(目标){
* bitmap.onPress =函数(EVT)*
{if (window.console && console.log) { console.log("press!"); }
target.scaleX = target.scaleY = target.scale*1.2;
container.addChild(target);
// update the stage while we drag this target
//Ticker provides a central heartbeat for stage to listen to
//At each beat, stage.tick is called and the display list re-rendered
Ticker.addListener(stage);
var offset = {x:target.x-evt.stageX, y:target.y-evt.stageY};
evt.onMouseMove = function(ev) {
target.x = ev.stageX+offset.x;
target.y = ev.stageY+offset.y;
if (window.console && console.log) { console.log("move!"); }
}
evt.onMouseUp = function() {
target.scaleX = target.scaleY = target.scale;
// update the stage one last time to render the scale change, then stop updating:
stage.update();
Ticker.removeListener(stage);
if (window.console && console.log) { console.log("up!"); }
}
**}})(bitmap); **
bitmap.onClick = function() { if (window.console && console.log) { console.log("click!"); } }
}
我已经通过他们的代码至少50次,仍然有理解他们的一些代码的问题。如果你是一个新手中间程序员,它可能很难破译。 – puk 2011-12-06 21:15:46