关键点:知道如何画线就行了,代码不复杂,直接看吧(从同事一本书上抄的)
代码语言:javascript复制var drawing:Boolean;
Mouse.hide(); //隐藏默认的光标
stage.addEventListener(MouseEvent.MOUSE_MOVE,Stage_MouseMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN,Stage_MouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,Stage_MouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,Stage_KeyDown);
function Stage_MouseDown(e:MouseEvent):void {
this.graphics.moveTo(this.mouseX,this.mouseY);//设置划线的起点
this.graphics.lineStyle(3,0x99cc00,100);//设置笔触大小及颜色
drawing=true;
}
function Stage_MouseMove(e:MouseEvent):void {
//铅笔鼠标跟随
pen1.x=this.mouseX;
pen1.y=this.mouseY;
if (drawing) {
this.graphics.lineTo(this.mouseX,this.mouseY);//关键代码就在这里
}
e.updateAfterEvent();
}
function Stage_MouseUp(e:MouseEvent):void {
drawing=false;
}
function Stage_KeyDown(e:KeyboardEvent):void {
if (e.keyCode==Keyboard.DELETE) { //注意这里:虽然代码只写了Delete键,但实际测试中发现如果按Ctrl Delete组合键,也能触发
this.graphics.clear();
}
}
做为对比,大家也可以参看:silverlight:手写板/涂鸦/墨迹/InkPresenter示例程序