How to draw a string in BitmapData

How can I draw strings in BitmapData, is there something like Graphics.drawString ()? Java?

+6
actionscript-3
source share
2 answers

In ActionScript, the most natural way to handle this, I think, would be to use a container like Sprite and draw using a graphics object and / or add other displayed objects as children. Then you can take your “snapshot” when / if necessary to get pixel data.

To add text, creating a TextField is the easiest option.

Anyway, you could write a little function that does this on an existing BitmapData if you want. Here is a sketch of how to write such a function:

 function drawString(target:BitmapData,text:String,x:Number,y:Number):void { var tf:TextField = new TextField(); tf.text = text; var bmd:BitmapData = new BitmapData(tf.width,tf.height); bmd.draw(tf); var mat:Matrix = new Matrix(); mat.translate(x,y); target.draw(bmd,mat); bmd.dispose(); } // use var bitmap:BitmapData = new BitmapData(400,400); // let draw something first (whatever is on the stage at this point) bitmap.draw(stage); drawString(bitmap,"testing",100,50); // display the result... addChild(new Bitmap(bitmap)); 
+8
source share

You can draw a TextField into a bitmap:

 import flash.text.TextField; import flash.display.BitmapData; import flash.display.Bitmap; var tf:TextField=new TextField(); tf.text="Hello world"; var bd:BitmapData=new BitmapData(200,200, false,0x00ff00); bd.draw(tf); var bm:Bitmap=new Bitmap(bd); addChild(bm); 
+1
source share

All Articles