Flash 如何从位图数据中剪切形状?
我有一个填充的形状和一个与形状的边界框宽度和高度相同的位图数据。Flash 如何从位图数据中剪切形状?,flash,actionscript-3,actionscript,bitmap,bitmapdata,Flash,Actionscript 3,Actionscript,Bitmap,Bitmapdata,我有一个填充的形状和一个与形状的边界框宽度和高度相同的位图数据。 我需要从BitmapData中剪切形状(基本上是将BitmapData绘制到形状上…[类似:] 我使用了一种相当粗俗的方法: public static function cutPoly(img:BitmapData, s:Shape, bounds:Bounds):BitmapData { var temp:BitmapData = new BitmapData(bounds.width, bou
我需要从BitmapData中剪切形状(基本上是将BitmapData绘制到形状上…[类似:] 我使用了一种相当粗俗的方法:
public static function cutPoly(img:BitmapData, s:Shape, bounds:Bounds):BitmapData {
var temp:BitmapData = new BitmapData(bounds.width, bounds.height, true);
Main.inst.stageQuality("low"); //hack to kill anti-aliasing
temp.draw(s,new Matrix());
Main.inst.stageQuality("high"); // end hack
//0xFF00FF00 is the color of the shape
makeColTrans(temp,0xFF00FF00); //makes the color transparent :P
//return temp;
img.draw(temp);
//img.draw(temp);
temp.dispose();
makeColTrans(img, 0xFFFFFFFF);
return img;
}
我想知道是否有更好的方法…一种不仅仅是黑客攻击的方法。它也可以被视为黑客攻击,但您可以在容器精灵中添加位图和(绘制的)形状,用形状遮罩位图,然后再次绘制结果图像。您将获得的唯一好处是使用运行时的本机绘图算法,并且只有当makeColTrans逐像素扫描整个位图时才会出现这种情况 编辑代码示例:
public static function cutPoly(sourceBitmapData:BitmapData, maskShape:Shape, bounds:Rectangle):BitmapData {
// you might not need this, supplying just the sourceBitmap to finalBitmapData.draw(), it should be tested though.
var sourceBitmapContainer:Sprite = new Sprite();
sourceBitmapContainer.addChild(sourceBitmap);
sourceBitmapContainer.addChild(maskShape);
var sourceBitmap:Bitmap = new Bitmap(sourceBitmapData);
maskShape.x = bounds.x;
maskShape.y = bounds.y;
sourceBitmap.mask = maskShape;
var finalBitmapData:BitmapData = new BitmapData(bounds.width, bounds.height, true, 0x00ffffff);
// or var finalBitmapData:Bitmap = new BitmapData(maskShape.width, maskShape.height); not too sure about the contents of the bounds...
finalBitmapData.draw(sourceBitmapContainer);
return finalBitmapData;
}
draw()方法的第二个参数采用变换矩阵——您可以在此处指定偏移、旋转、倾斜等。然后使用该位图数据对象作为形状上beginBitmapFill的源。形状周围的颜色是否一致?如果是这样,您可以找到该彩色像素的第一个实例,并从该坐标以0x00000000填充位图数据,然后将结果绘制到新的透明位图数据 编辑 下面是另一个使用阈值的示例。这远非完美,但它确实让你走到了这一步。在本例中,类“Tile”只是您在问题中提供的带有链接ID的图像
import flash.display.BitmapData;
import flash.geom.Point;
import flash.display.Bitmap;
var sample : Tile = new Tile();
var alphaBitmap : BitmapData = new BitmapData ( sample.width, sample.height, true, 0x00000000 );
alphaBitmap .draw ( sample );
alphaBitmap.threshold( alphaBitmap, alphaBitmap.rect, new Point(), "<", 0xFFEFEFEF, 0xFFFFFFFF, 0xFFFFFFFF );
alphaBitmap.threshold( alphaBitmap, alphaBitmap.rect, new Point(), "!=", 0xFFFFFFFF, 0x00000000 );
addChild ( new Bitmap ( alphaBitmap ) );
var source : Tile = new Tile();
var output : BitmapData = new BitmapData ( sample.width, sample.height, true, 0x00000000 );
output.copyPixels( source, source.rect, new Point(), alphaBitmap );
var outputBitmap : Bitmap = new Bitmap ( output );
outputBitmap.x = outputBitmap.width;
addChild ( outputBitmap );
导入flash.display.BitmapData;
导入flash.geom.Point;
导入flash.display.Bitmap;
变量示例:Tile=new Tile();
var alphaBitmap:BitmapData=新的BitmapData(sample.width、sample.height、true、0x00000000);
字母位图绘制(示例);
阈值(字母表,字母表,ReCt,新点),“,如果你想做这个“cookie剪切”算法,主要是从BITMAPATION对象中,考虑这个:
var bmp:BitmapData = pSourceBmp.clone();
var alphaBmp:BitmapData = new BitmapData(shape.width, shape.height, true, 0);
alphaBmp.draw( shape );
var alphaOrigin:Point = new Point();
var pasteOrigin:Point = new Point();
bmp.copyPixels( bmp, bmp.rect, pasteOrigin, alphaBmp, alphaOrigin, true);
注意:此代码未经测试
我曾在“饼干切割”的闪电引擎中使用过类似的东西"用途,并且从任何给定的矢量图形中提取形状效果很好。面具的有趣替代品。Nah,makeColTrans使用位图数据。阈值:p。我不完全理解你的意思……你能给出一小段代码吗?很抱歉,回答晚了,请检查上面的示例代码。尽管没有测试在这段特殊的代码中,我使用了一种类似的方法从图像中雕刻出一个拼图,它只做了一次,而且速度相当快,大约有1000块。