【问题标题】:AS3 Blitting - Copy Pixels getting some of the source imageAS3 Blitting - 复制像素获取一些源图像
【发布时间】:2012-07-14 07:38:09
【问题描述】:

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

我以前做过这个,程序绘制的形状像一个圆形,但是当我使用库项目时,大部分源像素都会被切断。

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

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);

【问题讨论】:

    标签: actionscript-3 bitmap bitmapdata


    【解决方案1】:

    使用明星资产:

    假设你正在舞台上的画布位图:

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

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

    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);
        }
    }
    

    产生:

    或者这里画了 1000 颗星星:

    或者最终抽取 10,000 颗星:

    【讨论】:

    • 天哪,你是一个乐于助人的人!我没有使用矩阵,今天完成工作后会尝试。非常感谢杰森。
    • 谢谢,很有用.. : ) ,,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2012-08-17
    相关资源
    最近更新 更多