mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-04 13:08:09 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'dart:ui';
 | 
						|
import 'package:flutter_svg/flutter_svg.dart';
 | 
						|
 | 
						|
import 'flame.dart';
 | 
						|
import 'position.dart';
 | 
						|
 | 
						|
class Svg {
 | 
						|
  DrawableRoot svgRoot;
 | 
						|
  Size size;
 | 
						|
 | 
						|
  Svg(String fileName) {
 | 
						|
    Flame.assets.readFile(fileName).then((svgString) async {
 | 
						|
      svgRoot = await svg.fromSvgString(svgString, svgString);
 | 
						|
    });
 | 
						|
  }
 | 
						|
 | 
						|
  /// Renders the svg on the [canvas] using the dimensions provided on [width] and [height]
 | 
						|
  ///
 | 
						|
  /// If not loaded, does nothing
 | 
						|
  void render(Canvas canvas, double width, double height) {
 | 
						|
    if (!loaded()) {
 | 
						|
      return;
 | 
						|
    }
 | 
						|
 | 
						|
    svgRoot.scaleCanvasToViewBox(canvas, Size(width, height));
 | 
						|
    svgRoot.draw(canvas, null);
 | 
						|
  }
 | 
						|
 | 
						|
  /// Renders the svg on the [canvas] on the given [position] using the dimensions provided on [width] and [height]
 | 
						|
  ///
 | 
						|
  /// If not loaded, does nothing
 | 
						|
  void renderPosition(
 | 
						|
      Canvas canvas, Position position, double width, double height) {
 | 
						|
    if (!loaded()) {
 | 
						|
      return;
 | 
						|
    }
 | 
						|
 | 
						|
    canvas.save();
 | 
						|
    canvas.translate(position.x, position.y);
 | 
						|
    render(canvas, width, height);
 | 
						|
    canvas.restore();
 | 
						|
  }
 | 
						|
 | 
						|
  bool loaded() {
 | 
						|
    return svgRoot != null;
 | 
						|
  }
 | 
						|
}
 |