SVG Assets for Mobile

In order to have high quality graphics on mobile, I want to avoid scaling my graphics, or use multiple sizes for different resolutions. My thought is to use svg and convert them all to bitmaps and save them somehow for subsequent plays, so this would only have to be done once, the first time played.

Does anyone have any pointers or know of any examples?

Thanks in advance.

Hey Dean I currently use my own framework designed specifically to deal with almost all svgs called app here’s the code snippet for my creation of svgs into a shape object cachedAsBitmap

public static function createSprite(?x:Float = 0, ?y:Float = 0, path:String,w:Int=-1,h:Int=-1,oval:Bool=false):Shape
		var shape = new Shape();
		var sub = path.substring(path.length - 4, path.length);
		if (sub == ".png" || sub == ".jpg")
	var bmd = Assets.getBitmapData(path);
	var mat = new Matrix();
	var sx:Float = 1;
	var sy:Float = 1;
	if(w > 0) sx = 1 / bmd.width * w;
	if(h > 0) sy = 1 / bmd.height * h;
	mat.scale(sx, sy);, mat, false, true);
	if (oval)
	{, 0, sx * bmd.width, sy * bmd.height);	
	}else{, 0, sx * bmd.width, sy * bmd.height);
		if (oval) trace("vector graphic does not support Oval");
		new SVG(Assets.getText(path)).render(, 0, 0, w, h);
		shape.x = x;
		shape.y = y;
		shape.cacheAsBitmap = true;
		return shape;

Love to chat more :slight_smile:

Hi Dean.

By ‘mobile’ you mean iOs/Android native targets?
Well, here’s an example I threw together real quick and just tested using Neko but it’ll surely get you started.
Be sure to put openfl.svg into assets/svg


import openfl.display.Sprite;
import openfl.display.Shape;
import openfl.display.BitmapData;
import openfl.display.Bitmap;
import openfl.display.PNGEncoderOptions;
import openfl.utils.ByteArray;
import openfl.Lib;
import format.SVG;
import openfl.Assets;

class Main extends Sprite 
    public function new() 
        if (sys.FileSystem.exists("initialized.dat") == false)
            var svgShape:Shape  = new Shape();
            var svg:SVG = new SVG (Assets.getText("svg/openfl.svg"));
            svg.render (;           
            var bmpData:BitmapData = new BitmapData(,, true);
            var data:ByteArray = bmpData.encode(bmpData.rect, new PNGEncoderOptions());
            var ="vec.png", true);
            output.writeBytes(data, 0, data.length);
            bmpData = null;
  "initialized.dat", "true");
    private function load():Void
        var data:ByteArray ="vec.png");
        var bmpData:BitmapData = BitmapData.fromBytes(data); 
        var bmp:Bitmap = new Bitmap(bmpData);

@PXshadow & @obscure

I’m targeting Android, but plan to target iOS at some point. Currently targeting C++ also, which shouldn’t be a problem saving to a folder, but Android is less straight forward since the app is zipped. I’ll have a look at this tomorrow.

Thank you both!

1 Like