我有一个前段时间构建的小型绘图应用程序。我刚刚重新打开项目以添加一些改进,但现在每当我调用 graphics.clear() 时,我都无法再次绘制。我已经扭转了我改变的一切,但无济于事。这是我的自定义绘图组件中的相关代码(基于 s:Group):
private var prevX:Number = 0;
private var prevY:Number = 0;
private function startDrawing(event:MouseEvent):void {
addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false, 0, true);
}
private function stopDrawing(event:MouseEvent):void {
prevX = 0;
prevY = 0;
removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false);
}
private function handleMouseMove(event:MouseEvent) {
//Set line style using graphics.lineStyle()
graphics.moveTo(prevX, prevY);
graphics.lineTo(event.localX, event.localY);
prevX = event.localX;
prevY = event.localY;
}
public function clearCanvas():void {
//After I call this I can no longer draw.
graphics.clear();
}
任何帮助将不胜感激,谢谢!
编辑:一些附加信息:在 MouseDown 上调用 startDrawing,在 MouseUp 上调用 stopDrawing,并通过按下按钮调用 clearCanvas。我也从 Flash Builder 4.6 升级到 4.7,不确定是否有 SDK 更改。
编辑:我今天对代码做了一些工作,这就是我想出的。单击并拖动时,我可以看到该线,但由于某种原因无法将其重新绘制到 MouseUp 的主画布上。当我跟踪代码时,它正在传输并且显然正确地绘制了所有内容(尽管平滑模式还没有工作)。
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:local="*"
xmlns:s="library://ns.adobe.com/flex/spark"
mouseEnabledWhereTransparent="true"
mouseDown="startDrawing(event)"
mouseUp="stopDrawing(event)">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.containers.Canvas;
//########## Public Variables ##########
public var __DrawingColor:uint = 0x000000;
public var __DrawingWidth:Number = 4;
public var __DrawingAlpha:Number = 1;
public var __DrawingSmoothing:Boolean = false;
//########## Private Variables ##########
private static const ___SMOOTHING_AMOUNT:Number = 5;
private var _History:ArrayCollection;
private var _CurrentPoints:Array;
private var _LiveCanvas:Canvas;
private var _LiveObject:Object;
private var _PrevX:Number = 0;
private var _PrevY:Number = 0;
//########## Public Functions ##########
public function init():void{
_History = new ArrayCollection();
}
public function clearAll():void {
graphics.clear();
_History = new ArrayCollection();
graphics.beginFill(0xffffff, 0);
graphics.drawRect(0, 0, width, height);
graphics.endFill();
}
public function clearLast():void {
if (_History.length > 0) {
_History.removeItemAt(_History.length - 1);
drawLines(_History);
}
}
//########## Private Functions ##########
private function startDrawing(event:MouseEvent):void {
addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false, 0, true);
_CurrentPoints = new Array();
_LiveCanvas = new Canvas();
addElement(_LiveCanvas);
_LiveCanvas.x = 0;
_LiveCanvas.y = 0;
_LiveCanvas.width = this.width;
_LiveCanvas.height = this.height;
_LiveCanvas.setStyle("backgroundColor", 0xFFFFFF);
_LiveCanvas.setStyle("backgroundAlpha", "0");
}
private function handleMouseMove(event:MouseEvent):void {
if (_PrevX != 0 && _PrevY != 0) {
_CurrentPoints.push(new Point(event.localX, event.localY));
_LiveCanvas.graphics.lineStyle(__DrawingWidth, __DrawingColor, __DrawingAlpha);
_LiveCanvas.graphics.moveTo(_PrevX, _PrevY);
_LiveCanvas.graphics.lineTo(event.localX, event.localY);
}
_PrevX = event.localX;
_PrevY = event.localY;
}
private function stopDrawing(event:MouseEvent):void {
_CurrentPoints.push(new Point(event.localX, event.localY));
_PrevX = 0;
_PrevX = 0;
this.removeElement(_LiveCanvas);
_LiveCanvas = null;
if (__DrawingSmoothing) {
_CurrentPoints = smoothPoints(_CurrentPoints);
}
var addItem:Object = new Object();
addItem.color = __DrawingColor;
addItem.width = __DrawingWidth;
addItem.alpha = __DrawingAlpha;
addItem.lines = pointsToLines(_CurrentPoints);
_History.addItem(addItem);
drawLines(_History);
removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false);
}
private function smoothPoints(points:Array):Array {
var len:uint = points.length;
var p:Point;
var ret:Array = new Array();
var t:Number = 1 / ___SMOOTHING_AMOUNT;
while (t < 1) {
p = getPoint(t, points);
ret.push(p);
t += 1 / ___SMOOTHING_AMOUNT;
}
return ret;
}
private function pointsToLines(points:Array):Array {
var ret:Array = new Array();
var lineObject:Object = new Object();
for (var i:Number = 0; i < points.length; i++) {
if (!lineObject.hasOwnProperty("x1")) {
lineObject.x1 = Point(points[i]).x;
lineObject.y1 = Point(points[i]).y;
} else if (!lineObject.hasOwnProperty("x2")) {
lineObject.x2 = Point(points[i]).x;
lineObject.y2 = Point(points[i]).y;
} else {
ret.push(lineObject);
lineObject = new Object();
lineObject.x1 = Point(points[i]).x;
lineObject.y1 = Point(points[i]).y;
}
}
return ret;
}
private function drawLines(lines:ArrayCollection):void {
graphics.clear();
graphics.beginFill(0xFFFFFF, 0);
graphics.drawRect(0, 0, width, height);
graphics.endFill();
var currentPoints:Object;
for (var x:Number = 0; x < lines.length; x++) {
currentPoints = lines[x];
graphics.lineStyle(currentPoints.width, currentPoints.color, currentPoints.alpha);
for (var i:Number = 0; i < currentPoints.lines.length; i++) {
graphics.moveTo(currentPoints.lines[i].x1, currentPoints.lines[i].y1);
graphics.lineTo(currentPoints.lines[i].x2, currentPoints.lines[i].y2);
}
}
}
private function getPoint(t:Number, points:Array):Point {
var x:Number = 0;
var y:Number = 0;
var n:uint = points.length-1;
var factn:Number = factoral(n);
for (var i:uint=0;i<=n;i++) {
var b:Number = factn/(factoral(i)*factoral(n-i));
var k:Number = Math.pow(1-t, n-i)*Math.pow(t, i);
x += b*k*points[i].x;
y += b*k*points[i].y;
}
return new Point(x, y);
}
private function factoral(value:uint):Number {
if (value==0)
return 1;
var total:Number = value;
while (--value>1)
total *= value;
return total;
}
]]>
</mx:Script>
</s:Group>