问题
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.
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);
回答1:
Using a star asset:
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);
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);
}
}
Which produces:
Or here 1,000 stars are drawn:
Or finally 10,000 stars are drawn:
来源:https://stackoverflow.com/questions/11487663/as3-blitting-copy-pixels-getting-some-of-the-source-image