我写了一个基本程序来将鼠标定位在画布上,并且每次运行它都会冻结我的电脑,有人可以看一下吗?此外,如果你知道更好的方法,那就太棒了。
我决定学习这门语言,因为我不喜欢JavaScript而且我读它是Java / C ++人的理想选择,这就是我开始使用这种语言的原因。
import 'dart:html';
import 'dart:async';
import 'package:polymer/polymer.dart';
CanvasElement canvas = document.querySelector('#myPaintCanvas');
CanvasRenderingContext2D context = canvas.getContext('2d');
void pencil(mouseX, mouseY){
print("This is from pencil method: MouseX: " + mouseX.toString() + " MouseY: " + mouseY.toString());
}
void mouseDragginStarts(moveX, moveY){
print("This is from mouseDragging: " + "mouseX: "+ moveX + " mouseY:" + moveX);
}
void main(){
//get a refrence to the canvas
//canvas.onClick.listen((e) {
canvas.onDrag.listen((e){
print("Got a click (${e.client.x-canvas.offsetLeft}, ${e.client.y-canvas.offsetTop})");
pencil(e.client.x-canvas.offsetLeft, e.client.y-canvas.offsetTop);
//while(canvas.onClick.listen(e) == true && canvas.onDrag.listen(e) == true){
while(canvas.onDrag == true){
mouseDragginStarts(e.client.x, e.client.y);
if(e==false){
break;
}
}
});
/*context.moveTo(100, 150);
context.lineTo(450, 50);
context.lineWidth = 4;
//set the line color
context.strokeStyle = '#ff0000'; //red
context.stroke();*/
}
答案 0 :(得分:1)
while(canvas.onDrag == true){ <== I wonder if it enters the loop, canvas.onDrag never becomes true
mouseDragginStarts(e.client.x, e.client.y);
if(e==false){ <== e never becomes false
break;
}
}
你的代码没有任何意义。你应该重新解释一下你真正想要完成的事情。
当你只想跟踪鼠标移动时,你只需要
canvas.onMouseMove.listen((e) {
// do something with mouse coordinates
// e.client.x, e.client.y
});