我正在開發Paint應用程序是AS3。我正在嘗試的是基本模仿MS Paint。我需要創建畫筆工具。刷子工具有很多形狀(刷子的尖端),如sqaure,circle,菱形。我最初計劃使用圖形類,但一些聰明人在這裏建議我使用Bitmap類。我已經開發了這個類,它似乎工作正常。但現在影響我的兩個問題使用AS3代碼重新創建畫筆工具來開發繪畫應用程序(使用位圖類)
- 如果我增加鼠標的速度,繪圖變得不連續。
- 我想要克服這個問題的一個想法是計算最後一點和當前點之間的點並在這些點上繪製圓。但是,當我將鼠標移動到曲線圖案中時,這個想法會給我帶來問題。
包{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.ui.Keyboard;
[SWF(width=600,height=400,backgroundColor=0x000000)]
/**
* Demonstrates BitmapData's draw() command by duplicating the entirety of the stage using draw()
* whenever the space bar is pressed. A smaller copy is also drawn onto the stage to show how draw()
* can be used to transform the pixels in the image being drawn.
*/
public class DrawTest extends Sprite {
private var _circle:Sprite;
private var _bitmapData:BitmapData;
/**
* Constructor. Creates bitmap that will be drawn into and circle that will follow the mouse.
* A listener is also set up to handle then a key is pressed.
*/
public function DrawTest() {
createBitmap();
createCircle();
stage.addEventListener(MouseEvent.MOUSE_DOWN,_handleMouseEventBrush);
stage.addEventListener(MouseEvent.MOUSE_UP,_handleMouseEventBrush);
}
/**
* Creates bitmap data that the stage contents will be drawn into and adds this to the stage.
*/
private function createBitmap():void {
_bitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0x000000);
var bitmap:Bitmap=new Bitmap(_bitmapData);
addChild(bitmap);
}
/**
* Creates a circle shape that is set to follow the mouse as it moves.
*/
private function createCircle():void {
_circle=new Sprite ;
_circle.graphics.beginFill(0xFF0000);
_circle.graphics.drawCircle(0,0,10);
_circle.graphics.endFill();
addChild(_circle);
_circle.x=stage.mouseX;
_circle.y=stage.mouseY;
_circle.startDrag();
}
private function _handleMouseEventBrush(e:MouseEvent):void {
switch (String(e.type)) {
case "mouseDown" :
_bitmapData.draw(stage);
stage.addEventListener(MouseEvent.MOUSE_MOVE,_handleMouseEventBrush);
break;
case "mouseUp" :
stage.removeEventListener(MouseEvent.MOUSE_MOVE,_handleMouseEventBrush);
break;
case "mouseMove" :
_bitmapData.draw(stage);
}
}
}
有誰想法,如何克服這一點。
只是一些提示使事情變得更快。在繪製之前,您不需要將圓圈添加到任何父級('_bitmapData.draw(_circle)')。你可以使用'Shape'而不是'Sprite',因爲你只需要繪製它。 – Kodiak 2011-04-06 11:41:23
_bitmapData.draw(階段)非常慢。我已經回答了有關優化的另一個問題,請參閱http://stackoverflow.com/questions/3996984/is-it-possible-to-improve-the-performance-of-this-drawing-technique/3997079#3997079 – alxx 2011-04-06 18:43:16