Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/src/lottie.dart
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,15 @@ class _State extends State<Lottie> {
}

class TVGCanvas extends CustomPainter {
TVGCanvas(
{required this.image,
required this.width,
required this.height,
required this.renderWidth,
required this.renderHeight});
TVGCanvas({
required this.image,
required this.width,
required this.height,
required this.renderWidth,
required this.renderHeight,
this.fit = BoxFit.none,
this.alignment = Alignment.center,
});

double width;
double height;
Expand All @@ -384,6 +387,9 @@ class TVGCanvas extends CustomPainter {

ui.Image image;

BoxFit fit;
Alignment alignment;

@override
void paint(Canvas canvas, Size size) {
final left = (width - renderWidth) / 2;
Expand All @@ -393,14 +399,15 @@ class TVGCanvas extends CustomPainter {
canvas: canvas,
rect: Rect.fromLTWH(left, top, renderWidth, renderHeight),
image: image,
fit: BoxFit.none, //NOTE: Should make it a param
filterQuality: FilterQuality.high, //NOTE: Should make it a param
alignment: Alignment.center, //NOTE: Should make it a param
fit: fit,
alignment: alignment,
);
}

@override
bool shouldRepaint(TVGCanvas oldDelegate) {
return image != oldDelegate.image;
return image != oldDelegate.image ||
fit != oldDelegate.fit ||
alignment != oldDelegate.alignment;
}
}
35 changes: 18 additions & 17 deletions lib/src/thorvg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ final DynamicLibrary _dylib = () {

final ThorVGFlutterBindings tvg = ThorVGFlutterBindings(_dylib);

/* ThorVG Play State */
enum PlayState { paused, playing, destroyed }

/* ThorVG Dart */

class Thorvg {
Expand All @@ -53,9 +56,7 @@ class Thorvg {
double startTime = DateTime.now().millisecond / 1000;
double speed = 1.0;

// FIXME(jinny): Should be like enumeration for each status
bool isPlaying = false;
bool deleted = false;
PlayState state = PlayState.paused;

late bool animate = false;
late bool reverse = false;
Expand All @@ -69,8 +70,8 @@ class Thorvg {
}

Uint8List? animLoop() {
if (deleted) {
throw Exception('Thorvg is already deleted');
if (state == PlayState.destroyed) {
throw Exception('Thorvg is already destroyed');
}

if (!update()) {
Expand All @@ -82,8 +83,8 @@ class Thorvg {
}

bool update() {
if (deleted) {
throw Exception('Thorvg is already deleted');
if (state == PlayState.destroyed) {
throw Exception('Thorvg is already destroyed');
}

final duration = tvg.duration(animation);
Expand All @@ -102,7 +103,7 @@ class Thorvg {
return true;
}

isPlaying = false;
state = PlayState.paused;
return false;
}

Expand All @@ -116,8 +117,8 @@ class Thorvg {
}

Uint8List? render() {
if (deleted) {
throw Exception('Thorvg is already deleted');
if (state == PlayState.destroyed) {
throw Exception('Thorvg is already destroyed');
}

tvg.resize(animation, width, height);
Expand All @@ -136,8 +137,8 @@ class Thorvg {
}

void play() {
if (deleted) {
throw Exception('Thorvg is already deleted');
if (state == PlayState.destroyed) {
throw Exception('Thorvg is already destroyed');
}

if (!animate) {
Expand All @@ -146,12 +147,12 @@ class Thorvg {

totalFrame = tvg.totalFrame(animation);
startTime = DateTime.now().millisecondsSinceEpoch / 1000;
isPlaying = true;
state = PlayState.playing;
}

void load(String src, int w, int h, bool animate, bool repeat, bool reverse) {
if (deleted) {
throw Exception('Thorvg is already deleted');
if (state == PlayState.destroyed) {
throw Exception('Thorvg is already destroyed');
}

List<int> list = utf8.encode(src);
Expand Down Expand Up @@ -183,12 +184,12 @@ class Thorvg {
}

void delete() {
if (deleted) {
if (state == PlayState.destroyed) {
return;
}

if (tvg.destroy(animation)) {
deleted = true;
state = PlayState.destroyed;
}
}
}
Expand Down