如何使用 BitmapData 用铅笔绘图?

发布于 2024-12-25 06:19:30 字数 826 浏览 4 评论 0原文

现在我只有这段代码,但我没有使用 BitmapData.draw()。如何使用 BitmapData.draw() 编写代码?

penSprite.graphics.lineStyle(3,045666);

addChild(penSprite);

addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
addEventListener(MouseEvent.MOUSE_UP,   mouseUp);

penSprite = new Sprite();                  //var penSprite:Sprite = new Sprite();
mouseDownFlag = new Boolean();            //var mouseDownFlag:Boolean = false;

private function mouseDown(e:MouseEvent):void
{
    penSprite.graphics.moveTo(e.localX, e.localY);
    mouseDownFlag = true;
}

private function mouseMove(e:MouseEvent):void
{
    if (mouseDownFlag) penSprite.graphics.lineTo(e.localX, e.localY);
}

private function mouseUp(e:MouseEvent):void
{
    mouseDownFlag = false;
}

Right now I only have this code, but I'm not using BitmapData.draw(). How can I write my code using BitmapData.draw()?

penSprite.graphics.lineStyle(3,045666);

addChild(penSprite);

addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
addEventListener(MouseEvent.MOUSE_UP,   mouseUp);

penSprite = new Sprite();                  //var penSprite:Sprite = new Sprite();
mouseDownFlag = new Boolean();            //var mouseDownFlag:Boolean = false;

private function mouseDown(e:MouseEvent):void
{
    penSprite.graphics.moveTo(e.localX, e.localY);
    mouseDownFlag = true;
}

private function mouseMove(e:MouseEvent):void
{
    if (mouseDownFlag) penSprite.graphics.lineTo(e.localX, e.localY);
}

private function mouseUp(e:MouseEvent):void
{
    mouseDownFlag = false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2025-01-01 06:19:30

这是基于您的代码的代码示例。您可以通过将其设为电影的基类来尝试一下。

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;

/**
 * Drawing app
 */
public class DrawingApp extends MovieClip {

    /** Drawing sprite */
    protected var penSprite     : Sprite = new Sprite();

    /** Bitmap where drawing will be saved */
    protected var canvasBitmap  : Bitmap;

    /**
     * Class constructor
     */
    public function DrawingApp () {

        // create bitmap that hold a bitmap data, where your drawing will displayed
        this.canvasBitmap = new Bitmap( new BitmapData( this.stage.stageWidth, this.stage.stageHeight ), "auto", true );

        // add children. Canvas must be below sprite
        this.addChild( canvasBitmap );
        this.addChild(penSprite);

        this.penSprite.graphics.lineStyle(3, 0x000000 );

        // listen to stage, not to the root or you won't get mouse down events root sprite is empty.
        this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);

    }

    /**
     * Start drawing 
     */
    private function mouseDown(e:MouseEvent):void {

        this.penSprite.graphics.moveTo(e.localX, e.localY);

        // add movement listeners here instead of using flag
        this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
        this.stage.addEventListener(MouseEvent.MOUSE_UP,   this.mouseUp);

    }

    /**
     * Draw line on sprite
     */
    private function mouseMove(e:MouseEvent):void {
        penSprite.graphics.lineTo( e.localX, e.localY );
    }

    /**
     * Draw sprite graphics to canvas and end drawing by cleaning up sprite graphics and unregistering movement listeners
     */
    private function mouseUp(e:MouseEvent):void {

        // draw sprite graphics to bitmap data (last parameter makes drawing smooth)
        canvasBitmap.bitmapData.draw( penSprite, null, null, null, null, true );

        // remove listeners 
        this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
        this.stage.removeEventListener(MouseEvent.MOUSE_UP,   mouseUp);

    }           

}

}

Here's the code sample based on you code. You try it out by just making it a base class of the movie.

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;

/**
 * Drawing app
 */
public class DrawingApp extends MovieClip {

    /** Drawing sprite */
    protected var penSprite     : Sprite = new Sprite();

    /** Bitmap where drawing will be saved */
    protected var canvasBitmap  : Bitmap;

    /**
     * Class constructor
     */
    public function DrawingApp () {

        // create bitmap that hold a bitmap data, where your drawing will displayed
        this.canvasBitmap = new Bitmap( new BitmapData( this.stage.stageWidth, this.stage.stageHeight ), "auto", true );

        // add children. Canvas must be below sprite
        this.addChild( canvasBitmap );
        this.addChild(penSprite);

        this.penSprite.graphics.lineStyle(3, 0x000000 );

        // listen to stage, not to the root or you won't get mouse down events root sprite is empty.
        this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);

    }

    /**
     * Start drawing 
     */
    private function mouseDown(e:MouseEvent):void {

        this.penSprite.graphics.moveTo(e.localX, e.localY);

        // add movement listeners here instead of using flag
        this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
        this.stage.addEventListener(MouseEvent.MOUSE_UP,   this.mouseUp);

    }

    /**
     * Draw line on sprite
     */
    private function mouseMove(e:MouseEvent):void {
        penSprite.graphics.lineTo( e.localX, e.localY );
    }

    /**
     * Draw sprite graphics to canvas and end drawing by cleaning up sprite graphics and unregistering movement listeners
     */
    private function mouseUp(e:MouseEvent):void {

        // draw sprite graphics to bitmap data (last parameter makes drawing smooth)
        canvasBitmap.bitmapData.draw( penSprite, null, null, null, null, true );

        // remove listeners 
        this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
        this.stage.removeEventListener(MouseEvent.MOUSE_UP,   mouseUp);

    }           

}

}

空城仅有旧梦在 2025-01-01 06:19:30

好吧,你的问题不是很清楚,我想你想将你的内容复制到位图中。
如果这就是您想要实现的目标,只需创建一个新的 bitmapData,在其中绘制精灵并将其用作位图的源即可。
例如,当您完成在 penSprite 中绘制时,您可以将其复制到位图中,如下所示:

 var bData:BitmapData = new BitmapData(penSprite.width, penSprite.height,true,0xFFFFFFFF);
 bData.draw(penSprite);
 var image:Bitmap=new Bitmap(bData);
 addChild(image);

希望这就是您正在寻找的。

编辑:
我看到您只是在问题中添加了一条评论,说您想绘制“使用 bitmapData.draw”。所以,我认为您误解了该方法的使用。如果您查看文档:

“使用 Flash 运行时矢量渲染器将源显示对象绘制到位图图像上。”

那么您基本上可以使用它在 bitmapData 中绘制另一个显示对象。

Ok, your question is not very clear, I suppose that you want to copy your content in a Bitmap.
If this is what you want to accomplish, just create a new bitmapData, draw your sprite in it and use it as a source of a Bitmap.
For example, when you finish to draw in your penSprite, you can copy it in a Bitmap like this:

 var bData:BitmapData = new BitmapData(penSprite.width, penSprite.height,true,0xFFFFFFFF);
 bData.draw(penSprite);
 var image:Bitmap=new Bitmap(bData);
 addChild(image);

Hope this is what you're looking for.

EDIT:
I see that you just add a comment in your question, saying that you want to draw "using bitmapData.draw". So, I think you are misunderstanding the use of that method. If you look in the documentation:

"Draws the source display object onto the bitmap image, using the Flash runtime vector renderer. "

So you basically use it to draw another display object in a bitmapData.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文