how can I draw strings onto BitmapData, is there something like Java´s Graphics.drawString()?
In Actionscript, the most natural way of handling this, I think, would be using a container such as Sprite
and drawing using it's graphics
object and / or adding other display objects as children. Then you could take your "snapshot" when / if necessary, to get the pixel data.
For adding text, creating a TextField
is the simplest option.
Anyway, you could write a little function that does this on an existing BitmapData, if you wanted. Here's a sketch of how such a function could be written:
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's 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));
You can draw a TextField
into your 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With