AS3: How to clear graphics in a specific pixel / area

I know what you use graphics.clearto clear all the graphics, but which clears the graphics from the scene, I would like to clear the graphics in a specific pixel (s) or between the xy value, how do I do this?

+4
source share
3 answers

There is no way to do this with graphics. I just tried drawing transparent shapes does not create holes, alas.

You must convert the graphics you use to the Bitmap instance and work with pixels:

package
{
    import flash.geom.Matrix;
    import flash.geom.Rectangle;

    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;

    public class Holey extends Sprite
    {
        public function Holey() 
        {
            super();

            // Lets create some example graphics.
            graphics.beginFill(0x990000);
            graphics.drawCircle(200, 200, 100);
            graphics.endFill();

            // Convert into raster and make 1 pixel transparent.
            var aBit:Bitmap = rasterize(this);
            aBit.bitmapData.setPixel32(50, 50, 0x00000000);

            graphics.clear();
            addChild(aBit);
        }

        private function rasterize(source:DisplayObject):Bitmap
        {
            // Obtain bounds of the graphics.
            var aBounds:Rectangle = source.getBounds(source);

            // Create raster of appropriate size.
            var aRaster:BitmapData = new BitmapData(aBounds.width, aBounds.height, true, 0x00000000);

            // Make an offset to capture all the graphics.
            var aMatrix:Matrix = new Matrix;
            aMatrix.translate(-aBounds.left, -aBounds.top);

            aRaster.draw(source, aMatrix);
            return new Bitmap(aRaster);
        }
    }
}
+7
source

: mask. - ( , cacheAsBitmap=true), . :

  • , ​​ ( , , : stage root .)

  • , "" , . :

    // fill the stage with a solid rectangle
    var maskBitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xff000000);
    // erase part of it by drawing transparent pixels
    maskBitmapData.fillRect(new Rectangle(20, 20, 200, 100), 0);
    
    // create the mask object
    var maskBitmap:Bitmap = new Bitmap(maskBitmapData);
    maskBitmap.cacheAsBitmap = true; // this makes the mask an alpha mask
    addChild(maskBitmap);
    
  • .mask . , :

    root.cacheAsBitmap = true; // this makes the mask an alpha mask
    root.mask = maskBitmap;
    
+5

, , , ...:)

blendMode BlendMode.ERASE cacheAsBitmap. , , , . :

//make cheese
var cheese:Sprite = new Sprite();
cheese.cacheAsBitmap = true;
stage.addChild(cheese);
cheese.x = cheese.y = 10;

//define holes 
var holes:Shape = new Shape();
holes.blendMode = BlendMode.ERASE;
cheese.addChild(holes);

//draw cheese
var g = cheese.graphics;
g.beginFill(0xFFCC00);
g.drawRect(0,0,200,150);

//**Attack chees with mices.
g = holes.graphics;
for (var i:int = 0; i < 10; i++){
    g.beginFill(0xFF00FF);
    var hx = Math.random()*(cheese.width-7)+7;
    var hy = Math.random()*(cheese.height-7)+7;
    var s = Math.random()*15;
    g.drawCircle(hx, hy, s);
    g.endFill();
}

The result will be something like this:

enter image description here

change

Turns out you don't need to use cacheAsBitmapif you set the parent object's blending mode to LAYER(doc says it should be set automatically ...) So you can use cheese.blendMode = BlendMode.LAYER;instead cheese.cacheAsBitmap = true;. And if I remember correctly, masks also do not require cahceAsBitmap, even with blending mode NORMAL.

+3
source

Source: https://habr.com/ru/post/1671581/


All Articles