本文介绍了AS3划线-复制像素会得到一些源图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在屏幕上绘制一些东西,然后将其复制到舞台上的位图上.

I am trying to draw something on the screen and then, copy that onto a bitmap which is on stage.

我以前是用程序绘制的形状(例如圆形)来完成此操作的,但是当我使用库项目时,大多数源像素都会被切除.

I have done this before, with a procedurally drawn shape like a circle but when I use a library item most of the source pixels get cut off.

这是我的代码-在另一个函数中,将位图对象添加到了舞台上,我可以看到copyPixels可以正常工作,但是正如我所说的,它只能复制一些像素.我尝试过与Rectangle一起玩,但到目前为止还没有运气.

here's my code - in another function the bitmap object is added to the stage and I can see that copyPixels work but as I have said copies only some of the pixels. I have tried playing with the Rectangle but no luck so far.

var s:StarAsset = new StarAsset();

        s.x = e.stageX;
        s.y = e.stageY;
        s.scaleX = e.pressure * 10;
        s.scaleY = e.pressure * 10;
        s.rotation = Math.random() * 360;



        var bms:BitmapData = new BitmapData(s.width + 6, s.height + 6, true, 0x00000000);
        bms.draw(s);

        var srect:Rectangle = new Rectangle();
        srect.width = s.width + 6;
        srect.height = s.height + 6;

        var destpoint:Point = new Point(s.x, s.y);
        bmcontainer.copyPixels(bms, srect, destpoint, null, null, true);

推荐答案

使用星级资产:

并假设您正在舞台上使用画布位图:

And assuming your are blitting to a canvas bitmap on the stage:

var canvas:BitmapData = new BitmapData(600, 600, true, 0x0);
var bitmap:Bitmap = new Bitmap(canvas, PixelSnapping.AUTO, true);
addChild(bitmap);

此实现将实例化StarAsset,将其绘制到BitmapData,然后随机变换绘制到画布上的每个副本的比例,位置和旋转:

This implementation would instantiate your StarAsset, draw it to BitmapData, and then randomly transform scale, position, and rotation per copy drawn to the canvas:

makeStars();

function makeStars():void
{
    // get the star asset
    var s:StarAsset = new StarAsset();

    // copy star asset to bitmap data
    var bd:BitmapData = new BitmapData(s.width, s.height, true, 0x0);
    bd.draw(s);

    // draw 100 variants on BitmapData
    for(var i:uint = 0; i < 100; i++)
    {
        var positionX:Number = Math.random() * 600;
        var positionY:Number = Math.random() * 600;
        var scale:Number = Math.random();
        var angle:Number = Math.random() * 360;

        var matrix:Matrix = new Matrix();
        matrix.scale(scale, scale);
        matrix.rotate(angle * Math.PI / 180);
        matrix.translate(positionX, positionY);

        canvas.draw(bd, matrix, null, null, null, true);
    }
}

哪个会产生:

或者在这里绘制了1,000颗星:

Or here 1,000 stars are drawn:

或者最终获得10,000颗星星:

Or finally 10,000 stars are drawn:

这篇关于AS3划线-复制像素会得到一些源图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:46