以下代码应在工具栏中选择鼠标后在画布中移动时绘制图像:
import 'dart:html';
class Test {
ImageElement toolbar_selected_element = new ImageElement();
CanvasElement canvas;
var loaded_image_toolbar = false;
var mouseX = null, mouseY = null;
void main() {
canvas = query("#canvas");
canvas.onMouseMove.listen(canvas_OnMouseMove);
query(".toolbuton").onDragStart.listen(toolbar_button_OnClick);
}
void toolbar_button_OnClick(MouseEvent event) {
toolbar_selected_element.onLoad.listen(tool_button_image_OnLoad);
toolbar_selected_element.src = event.target.src;
}
void tool_button_image_OnLoad(event) {
loaded_image_toolbar = true;
window.requestAnimationFrame(draw);
}
void canvas_OnMouseMove(MouseEvent event) {
mouseX = event.clientX;
mouseY = event.clientY;
window.requestAnimationFrame(draw);
}
void draw(num _) {
if(loaded_image_toolbar && mouseX != null && mouseY != null) {
CanvasRenderingContext2D context = canvas.context2d;
context.drawImage(toolbar_selected_element, mouseX, mouseY);
}
}
}
void main() {
new Test().main();
}
调用了 draw 方法,但图像没有显示在画布中,缺少什么?