Skip to content

Commit 2e99ea5

Browse files
committed
feat: add rect and polygon
feat: add rect and polygon
1 parent 130e9a8 commit 2e99ea5

File tree

10 files changed

+228
-2
lines changed

10 files changed

+228
-2
lines changed

lib/src/core/data.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Data {
2+
String? mark;
3+
double x = 0;
4+
double y = 0;
5+
double speed = 0;
6+
double alpha = 1;
7+
double radius = 0;
8+
double angle = 0;
9+
double value = 0;
10+
11+
reset() {
12+
mark = null;
13+
angle = 0;
14+
x = 0;
15+
y = 0;
16+
speed = 0;
17+
alpha = 1;
18+
radius = 0;
19+
value = 0;
20+
}
21+
22+
destroy() {
23+
reset();
24+
}
25+
}

lib/src/extras/circle.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ class ZKCircle extends ZKNode {
4646
void draw(Canvas canvas, [Size? size]) {
4747
canvas.drawCircle(_offset, radius, this.paint);
4848
}
49+
50+
@override
51+
void reset() {
52+
super.reset();
53+
//radius = 10;
54+
}
4955
}

lib/src/extras/flower.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:zerker/zerker.dart';
3+
import '../math/mathutil.dart';
4+
import 'dart:math';
5+
6+
class ZKFlower extends ZKNode {
7+
@override
8+
String type = "ZKFlower";
9+
double radius = 10;
10+
Offset? _offset;
11+
12+
////////////////////////////////////////////////////////////
13+
///
14+
/// ZKFlower constructor
15+
///
16+
////////////////////////////////////////////////////////////
17+
ZKFlower(double radius, [Color? color]) : super() {
18+
this.oriWidth = radius;
19+
this.oriHeight = radius;
20+
this._offset = Offset(0, 0);
21+
if (color != null) this.color = color;
22+
}
23+
24+
@override
25+
void draw(Canvas canvas, [Size? size]) {
26+
double count = 5;
27+
double unit = MathUtil.PIx2 / count;
28+
double tha0 = rotation * MathUtil.PI_180;
29+
30+
for (int i = 0; i < count; i++) {
31+
double tha = tha0 + unit * i;
32+
double x = radius * cos(tha);
33+
double y = radius * sin(tha);
34+
canvas.drawCircle(Offset(x, y), radius, this.paint);
35+
}
36+
37+
canvas.drawCircle(_offset!, radius, this.paint);
38+
}
39+
}

lib/src/extras/polygon.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:ui';
2+
import 'dart:math';
3+
import 'package:flutter/material.dart';
4+
import '../node/node.dart';
5+
import '../core/constant.dart';
6+
7+
class ZKPolygon extends ZKNode {
8+
@override
9+
String type = "ZKPolygon";
10+
Path? _path;
11+
int _count = 5;
12+
13+
////////////////////////////////////////////////////////////
14+
///
15+
/// ZKPolygon constructor
16+
///
17+
////////////////////////////////////////////////////////////
18+
ZKPolygon(int count, double radius, [Color? color]) : super() {
19+
if (color != null) this.color = color;
20+
21+
_path = Path();
22+
_count = count;
23+
_fillPath(count, radius);
24+
}
25+
26+
void _fillPath(int count, double radius) {
27+
_path!.reset();
28+
29+
double unit = (Constant.PI * 2) / count;
30+
for (int i = 0; i < count; i++) {
31+
double x = radius * cos(unit * i);
32+
double y = radius * sin(unit * i);
33+
34+
if (i == 0)
35+
_path!.moveTo(x, y);
36+
else
37+
_path!.lineTo(x, y);
38+
}
39+
_path!.close();
40+
}
41+
42+
set radius(double radius) {
43+
_fillPath(_count, radius);
44+
}
45+
46+
@override
47+
void draw(Canvas canvas, [Size? size]) {
48+
canvas.drawPath(_path!, this.paint);
49+
}
50+
}

lib/src/extras/rect.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import '../node/node.dart';
55
class ZKRect extends ZKNode {
66
@override
77
String type = "ZKRect";
8+
bool _center = false;
89

910
////////////////////////////////////////////////////////////
1011
///
@@ -17,8 +18,26 @@ class ZKRect extends ZKNode {
1718
if (color != null) this.color = color;
1819
}
1920

21+
void setCenter([bool center = true]) {
22+
_center = center;
23+
}
24+
25+
void setScaleY(double scale) {
26+
setHeight(scale * oriHeight);
27+
}
28+
29+
void setHeight(double height) {
30+
this.oriHeight = height;
31+
}
32+
2033
@override
2134
void draw(Canvas canvas, [Size? size]) {
22-
canvas.drawRect(Rect.fromLTRB(0, 0, oriWidth, oriHeight), this.paint);
35+
if (_center) {
36+
canvas.drawRect(
37+
Rect.fromLTWH(-oriWidth / 2, -oriHeight / 2, oriWidth, oriHeight),
38+
this.paint);
39+
} else {
40+
canvas.drawRect(Rect.fromLTRB(0, 0, oriWidth, oriHeight), this.paint);
41+
}
2342
}
2443
}

lib/src/extras/star.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import 'dart:ui';
2+
import 'dart:math';
3+
import 'package:flutter/material.dart';
4+
import '../node/node.dart';
5+
import '../core/constant.dart';
6+
7+
class ZKStar extends ZKNode {
8+
@override
9+
String type = "ZKStar";
10+
Path? _path;
11+
int _count = 5;
12+
13+
////////////////////////////////////////////////////////////
14+
///
15+
/// ZKStar constructor
16+
///
17+
////////////////////////////////////////////////////////////
18+
ZKStar(int count, double radius, [Color? color]) : super() {
19+
if (color != null) this.color = color;
20+
_count = count;
21+
_path = Path();
22+
_fillPath(count, radius);
23+
}
24+
25+
void _fillPath(int count, double radius) {
26+
_path!.reset();
27+
double n = count * 2;
28+
double r1 = radius;
29+
double r2 = count == 3 ? r1 * 0.35 : r1 * 0.5;
30+
double unit = (Constant.PI * 2) / n;
31+
double angle = 0;
32+
33+
for (int i = 0; i < count; i++) {
34+
int a = 2 * i;
35+
int b = 2 * i + 1;
36+
// first point
37+
double x1 = r1 * cos(angle + a * unit);
38+
double y1 = r1 * sin(angle + a * unit);
39+
if (i == 0)
40+
_path!.moveTo(x1, y1);
41+
else
42+
_path!.lineTo(x1, y1);
43+
44+
// second point
45+
double x2 = r2 * cos(angle + b * unit);
46+
double y2 = r2 * sin(angle + b * unit);
47+
_path!.lineTo(x2, y2);
48+
}
49+
_path!.close();
50+
}
51+
52+
set radius(double radius) {
53+
_fillPath(_count, radius);
54+
}
55+
56+
@override
57+
void draw(Canvas canvas, [Size? size]) {
58+
canvas.drawPath(_path!, this.paint);
59+
}
60+
}

lib/src/math/mathutil.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import "dart:math";
22
import 'package:flutter/widgets.dart';
33

44
class MathUtil {
5+
static const double PI = 3.14159267;
6+
static const double PI_2 = PI / 2;
7+
static const double PIx2 = PI * 2;
8+
static const double PI_180 = PI / 180;
9+
static const double N180_PI = 180 / PI;
10+
511
static double round(double n, [int size = 4]) {
612
num k = pow(10, size);
713
return (n * k).round() / k;

lib/src/node/node.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "dart:math";
22
import 'package:flutter/material.dart';
33

44
import "../core/context.dart";
5+
import "../core/data.dart";
56
import "../utils/util.dart";
67
import '../event/event.dart';
78
import '../math/point.dart';
@@ -26,6 +27,7 @@ class ZKNode {
2627

2728
ZKNode? parent;
2829
Canvas? canvas;
30+
Data data = Data();
2931
Paint paint = new Paint();
3032

3133
ZKContext? context;
@@ -84,6 +86,7 @@ class ZKNode {
8486
}
8587

8688
set colorFilter(ColorFilter filter) {
89+
paint.colorFilter = null;
8790
this.paint.colorFilter = filter;
8891
}
8992

@@ -204,6 +207,21 @@ class ZKNode {
204207
this.paint.color = this.paint.color.withOpacity(a);
205208
}
206209

210+
void reset() {
211+
this.position.reset();
212+
this.anchor.reset();
213+
this.scale.reset();
214+
this.skew.reset();
215+
this.rotation = 0;
216+
this.alpha = 1;
217+
this.worldAlpha = 1;
218+
219+
this.parent = null;
220+
this.onTapDown = null;
221+
this.onTapUp = null;
222+
this.onTouchMove = null;
223+
}
224+
207225
void dispose() {
208226
this.position.reset();
209227
this.anchor.reset();

lib/zerker.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export 'src/extras/rect.dart';
1616
export 'src/extras/button.dart';
1717
export 'src/extras/circle.dart';
1818
export 'src/extras/scrollbg.dart';
19+
export 'src/extras/polygon.dart';
20+
export 'src/extras/star.dart';
21+
export 'src/extras/flower.dart';
1922

2023
export 'src/event/bus.dart';
2124
export 'src/event/event.dart';

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: Zerker is a flexible and lightweight flutter canvas graphic animati
1111
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
1212
# Read more about iOS versioning at
1313
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
14-
version: 2.1.1
14+
version: 2.2.1
1515
homepage: https://github.com/flutterkit/zerker
1616

1717
environment:

0 commit comments

Comments
 (0)