我在瀏覽html5畫布示例時遇到了此代碼。 我已經粘貼了代碼,並且在我有疑問的地方發表了評論。請解釋此代碼片段
if(window.addEventListener) {
window.addEventListener('load', function() {
var canvas, context, tool;
function init() {
canvas = document.getElementById('imageView');
if (!canvas) {
alert('Error: I cannot find the canvas element!');
return;
}
if (!canvas.getContext) {
alert('Error: no canvas.getContext!');
return;
}
context = canvas.getContext('2d');
if (!context) {
alert('Error: failed to getContext!');
return;
}
tool = new tool_pencil();
canvas.addEventListener('mousedown', ev_canvas, false);
canvas.addEventListener('mousemove', ev_canvas, false);
canvas.addEventListener('mouseup', ev_canvas, false);
}
function tool_pencil() {
var tool = this;
this.started = false;
this.mousedown = function (ev) {
context.beginPath();
context.moveTo(ev._x, ev._y);
tool.started = true;
};
this.mousemove = function (ev) {
if (tool.started) {
context.lineTo(ev._x, ev._y);
context.stroke();
}
};
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
}
};
}
function ev_canvas (ev) {
if (ev.layerX || ev.layerX == 0) {
ev._x = ev.layerX;
ev._y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) {
ev._x = ev.offsetX;
ev._y = ev.offsetY;
}
//Please explain the follwing set of line
var func = tool[ev.type];
if (func) {
func(ev);
}
}
init();
}, false); }
如果沒有定義,func將是'undefined',而不是'null'。 – 2010-11-09 05:11:38