books
This commit is contained in:
1
packages/idraw/lib/p18/p18.dart
Normal file
1
packages/idraw/lib/p18/p18.dart
Normal file
@@ -0,0 +1 @@
|
||||
export 'p18_page.dart';
|
||||
27
packages/idraw/lib/p18/p18_page.dart
Normal file
27
packages/idraw/lib/p18/p18_page.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:components/components.dart';
|
||||
import 's01.dart' as s1;
|
||||
import 's02.dart' as s2;
|
||||
import 's03.dart' as s3;
|
||||
import 's04.dart' as s4;
|
||||
import 's05.dart' as s5;
|
||||
|
||||
class P18Page extends StatelessWidget {
|
||||
const P18Page({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const DemoShower(
|
||||
srcCodeDir: 'draw/p18',
|
||||
|
||||
demos: [
|
||||
s1.Paper(),
|
||||
s2.Paper(),
|
||||
s3.Paper(),
|
||||
s4.Paper(),
|
||||
// s5.Paper(),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
201
packages/idraw/lib/p18/s01.dart
Normal file
201
packages/idraw/lib/p18/s01.dart
Normal file
@@ -0,0 +1,201 @@
|
||||
import 'dart:math';
|
||||
|
||||
// import 'dart:ui' as ui;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020/11/5
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class Paper extends StatelessWidget {
|
||||
const Paper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
alignment: Alignment.center,
|
||||
child: const World(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class World extends StatefulWidget {
|
||||
const World({super.key});
|
||||
|
||||
@override
|
||||
_WorldState createState() => _WorldState();
|
||||
}
|
||||
|
||||
class _WorldState extends State<World> with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
ParticleManage pm = ParticleManage();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initParticleManage();
|
||||
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
)
|
||||
..addListener(pm.tick)
|
||||
// ..repeat()
|
||||
;
|
||||
}
|
||||
|
||||
void initParticleManage() {
|
||||
pm.size = Size(300, 200);
|
||||
Particle particle = Particle(x: 0, y: 0, vx: 3, vy: 0,ay: 0.05,
|
||||
color: Colors.blue, size: 8);
|
||||
pm.particles = [particle];
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
theWorld(){
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: theWorld,
|
||||
child: CustomPaint(
|
||||
size: pm.size,
|
||||
painter: WorldRender(manage: pm),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParticleManage with ChangeNotifier {
|
||||
List<Particle> particles = [];
|
||||
|
||||
Size size;
|
||||
|
||||
ParticleManage({this.size = Size.zero});
|
||||
|
||||
void setParticles(List<Particle> particles) {
|
||||
this.particles = particles;
|
||||
}
|
||||
|
||||
void addParticle(Particle particle) {
|
||||
particles.add(particle);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
particles.forEach(doUpdate);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void doUpdate(Particle p) {
|
||||
p.vy += p.ay; // y加速度变化
|
||||
p.vx += p.ax; // x加速度变化
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
if (p.x > size.width) {
|
||||
p.x = size.width;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.x < 0) {
|
||||
p.x = 0;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.y > size.height) {
|
||||
p.y = size.height;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
if (p.y < 0) {
|
||||
p.y = 0;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
particles.forEach((p) {
|
||||
p.x = 0;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class WorldRender extends CustomPainter {
|
||||
|
||||
final ParticleManage manage;
|
||||
|
||||
Paint fillPaint = Paint();
|
||||
|
||||
Paint stokePaint = Paint()
|
||||
..strokeWidth = 0.5
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
WorldRender({required this.manage}) : super(repaint: manage);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
canvas.drawRect(Offset.zero & size, stokePaint);
|
||||
manage.particles.forEach((particle) {
|
||||
drawParticle(canvas, particle);
|
||||
});
|
||||
}
|
||||
|
||||
void drawParticle(Canvas canvas, Particle particle) {
|
||||
fillPaint.color = particle.color;
|
||||
canvas.drawCircle(Offset(particle.x, particle.y), particle.size, fillPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant WorldRender oldDelegate) =>
|
||||
oldDelegate.manage != manage;
|
||||
}
|
||||
|
||||
|
||||
class Particle {
|
||||
/// x 位移.
|
||||
double x;
|
||||
|
||||
/// y 位移.
|
||||
double y;
|
||||
|
||||
/// 粒子水平速度.
|
||||
double vx;
|
||||
|
||||
// 粒子水平加速度
|
||||
double ax;
|
||||
|
||||
// 粒子竖直加速度
|
||||
double ay;
|
||||
|
||||
///粒子竖直速度.
|
||||
double vy;
|
||||
|
||||
|
||||
/// 粒子大小.
|
||||
double size;
|
||||
|
||||
/// 粒子颜色.
|
||||
Color color;
|
||||
|
||||
Particle({
|
||||
this.x = 0,
|
||||
this.y = 0,
|
||||
this.ax = 0,
|
||||
this.ay = 0,
|
||||
this.vx = 0,
|
||||
this.vy = 0,
|
||||
this.size = 0,
|
||||
this.color = Colors.black,
|
||||
});
|
||||
}
|
||||
215
packages/idraw/lib/p18/s02.dart
Normal file
215
packages/idraw/lib/p18/s02.dart
Normal file
@@ -0,0 +1,215 @@
|
||||
import 'dart:math';
|
||||
|
||||
// import 'dart:ui' as ui;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020/11/5
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class Paper extends StatelessWidget {
|
||||
const Paper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
alignment: Alignment.center,
|
||||
child: const World(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class World extends StatefulWidget {
|
||||
const World({super.key});
|
||||
|
||||
@override
|
||||
_WorldState createState() => _WorldState();
|
||||
}
|
||||
|
||||
class _WorldState extends State<World> with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
ParticleManage pm = ParticleManage();
|
||||
Random random = Random();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initParticleManage();
|
||||
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
)..addListener(pm.tick)
|
||||
// ..repeat()
|
||||
;
|
||||
}
|
||||
|
||||
void initParticleManage() {
|
||||
pm.size = Size(300, 200);
|
||||
for (var i = 0; i < 30; i++) {
|
||||
pm.particles.add(Particle(
|
||||
color: randomRGB(),
|
||||
size: 5 + 4 * random.nextDouble(),
|
||||
vx: 3 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
vy: 3 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
ay: 0.1,
|
||||
x: 150,
|
||||
y: 100));
|
||||
}
|
||||
}
|
||||
|
||||
Color randomRGB({int limitR = 0, int limitG = 0, int limitB = 0,}) {
|
||||
var r = limitR + random.nextInt(256 - limitR); //红值
|
||||
var g = limitG + random.nextInt(256 - limitG); //绿值
|
||||
var b = limitB + random.nextInt(256 - limitB); //蓝值
|
||||
return Color.fromARGB(255, r, g, b); //生成argb模式的颜色
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
theWorld() {
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: theWorld,
|
||||
child: CustomPaint(
|
||||
size: pm.size,
|
||||
painter: WorldRender(manage: pm),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParticleManage with ChangeNotifier {
|
||||
List<Particle> particles = [];
|
||||
|
||||
Size size;
|
||||
|
||||
ParticleManage({this.size = Size.zero});
|
||||
|
||||
void setParticles(List<Particle> particles) {
|
||||
this.particles = particles;
|
||||
}
|
||||
|
||||
void addParticle(Particle particle) {
|
||||
particles.add(particle);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
particles.forEach(doUpdate);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void doUpdate(Particle p) {
|
||||
p.vy += p.ay; // y加速度变化
|
||||
p.vx += p.ax; // x加速度变化
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
if (p.x > size.width) {
|
||||
p.x = size.width;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.x < 0) {
|
||||
p.x = 0;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.y > size.height) {
|
||||
p.y = size.height;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
if (p.y < 0) {
|
||||
p.y = 0;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
particles.forEach((p) {
|
||||
p.x = 0;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class WorldRender extends CustomPainter {
|
||||
|
||||
final ParticleManage manage;
|
||||
|
||||
Paint fillPaint = Paint();
|
||||
|
||||
Paint stokePaint = Paint()
|
||||
..strokeWidth = 0.5
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
WorldRender({required this.manage}) : super(repaint: manage);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
canvas.drawRect(Offset.zero & size, stokePaint);
|
||||
manage.particles.forEach((particle) {
|
||||
drawParticle(canvas, particle);
|
||||
});
|
||||
}
|
||||
|
||||
void drawParticle(Canvas canvas, Particle particle) {
|
||||
fillPaint.color = particle.color;
|
||||
canvas.drawCircle(Offset(particle.x, particle.y), particle.size, fillPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant WorldRender oldDelegate) =>
|
||||
oldDelegate.manage != manage;
|
||||
}
|
||||
|
||||
|
||||
class Particle {
|
||||
/// x 位移.
|
||||
double x;
|
||||
|
||||
/// y 位移.
|
||||
double y;
|
||||
|
||||
/// 粒子水平速度.
|
||||
double vx;
|
||||
|
||||
// 粒子水平加速度
|
||||
double ax;
|
||||
|
||||
// 粒子竖直加速度
|
||||
double ay;
|
||||
|
||||
///粒子竖直速度.
|
||||
double vy;
|
||||
|
||||
|
||||
/// 粒子大小.
|
||||
double size;
|
||||
|
||||
/// 粒子颜色.
|
||||
Color color;
|
||||
|
||||
Particle({
|
||||
this.x = 0,
|
||||
this.y = 0,
|
||||
this.ax = 0,
|
||||
this.ay = 0,
|
||||
this.vx = 0,
|
||||
this.vy = 0,
|
||||
this.size = 0,
|
||||
this.color = Colors.black,
|
||||
});
|
||||
}
|
||||
218
packages/idraw/lib/p18/s03.dart
Normal file
218
packages/idraw/lib/p18/s03.dart
Normal file
@@ -0,0 +1,218 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
// import 'dart:ui' as ui;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020/11/5
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class Paper extends StatelessWidget {
|
||||
const Paper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
alignment: Alignment.center,
|
||||
child: const World(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class World extends StatefulWidget {
|
||||
const World({super.key});
|
||||
|
||||
@override
|
||||
_WorldState createState() => _WorldState();
|
||||
}
|
||||
|
||||
class _WorldState extends State<World> with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
ParticleManage pm = ParticleManage();
|
||||
Random random = Random();
|
||||
late Timer timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
pm.size = Size(300, 200);
|
||||
|
||||
timer =Timer.periodic(Duration(seconds: 1), (timer) {
|
||||
if(pm.particles.length>20){
|
||||
timer.cancel();
|
||||
}
|
||||
pm.addParticle(Particle(
|
||||
color: randomRGB(),
|
||||
size: 5 + 4 * random.nextDouble(),
|
||||
vx: 3 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
vy: 3 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
ay: 0.1,
|
||||
x: 150,
|
||||
y: 100));
|
||||
});
|
||||
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
)..addListener(pm.tick)
|
||||
..repeat();
|
||||
}
|
||||
|
||||
Color randomRGB({int limitR = 0, int limitG = 0, int limitB = 0,}) {
|
||||
var r = limitR + random.nextInt(256 - limitR); //红值
|
||||
var g = limitG + random.nextInt(256 - limitG); //绿值
|
||||
var b = limitB + random.nextInt(256 - limitB); //蓝值
|
||||
return Color.fromARGB(255, r, g, b); //生成argb模式的颜色
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
timer.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
theWorld() {
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: theWorld,
|
||||
child: CustomPaint(
|
||||
size: pm.size,
|
||||
painter: WorldRender(manage: pm),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParticleManage with ChangeNotifier {
|
||||
List<Particle> particles = [];
|
||||
|
||||
Size size;
|
||||
|
||||
ParticleManage({this.size = Size.zero});
|
||||
|
||||
void setParticles(List<Particle> particles) {
|
||||
this.particles = particles;
|
||||
}
|
||||
|
||||
void addParticle(Particle particle) {
|
||||
particles.add(particle);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
particles.forEach(doUpdate);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void doUpdate(Particle p) {
|
||||
p.vy += p.ay; // y加速度变化
|
||||
p.vx += p.ax; // x加速度变化
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
if (p.x > size.width) {
|
||||
p.x = size.width;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.x < 0) {
|
||||
p.x = 0;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.y > size.height) {
|
||||
p.y = size.height;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
if (p.y < 0) {
|
||||
p.y = 0;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
particles.forEach((p) {
|
||||
p.x = 0;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class WorldRender extends CustomPainter {
|
||||
|
||||
final ParticleManage manage;
|
||||
|
||||
Paint fillPaint = Paint();
|
||||
|
||||
Paint stokePaint = Paint()
|
||||
..strokeWidth = 0.5
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
WorldRender({required this.manage}) : super(repaint: manage);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
canvas.drawRect(Offset.zero & size, stokePaint);
|
||||
manage.particles.forEach((particle) {
|
||||
drawParticle(canvas, particle);
|
||||
});
|
||||
}
|
||||
|
||||
void drawParticle(Canvas canvas, Particle particle) {
|
||||
fillPaint.color = particle.color;
|
||||
canvas.drawCircle(Offset(particle.x, particle.y), particle.size, fillPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant WorldRender oldDelegate) =>
|
||||
oldDelegate.manage != manage;
|
||||
}
|
||||
|
||||
|
||||
class Particle {
|
||||
/// x 位移.
|
||||
double x;
|
||||
|
||||
/// y 位移.
|
||||
double y;
|
||||
|
||||
/// 粒子水平速度.
|
||||
double vx;
|
||||
|
||||
// 粒子水平加速度
|
||||
double ax;
|
||||
|
||||
// 粒子竖直加速度
|
||||
double ay;
|
||||
|
||||
///粒子竖直速度.
|
||||
double vy;
|
||||
|
||||
|
||||
/// 粒子大小.
|
||||
double size;
|
||||
|
||||
/// 粒子颜色.
|
||||
Color color;
|
||||
|
||||
Particle({
|
||||
this.x = 0,
|
||||
this.y = 0,
|
||||
this.ax = 0,
|
||||
this.ay = 0,
|
||||
this.vx = 0,
|
||||
this.vy = 0,
|
||||
this.size = 0,
|
||||
this.color = Colors.black,
|
||||
});
|
||||
}
|
||||
207
packages/idraw/lib/p18/s04.dart
Normal file
207
packages/idraw/lib/p18/s04.dart
Normal file
@@ -0,0 +1,207 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
// import 'dart:ui' as ui;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020/11/5
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class Paper extends StatelessWidget {
|
||||
const Paper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
alignment: Alignment.center,
|
||||
child: const World(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class World extends StatefulWidget {
|
||||
const World({super.key});
|
||||
|
||||
@override
|
||||
_WorldState createState() => _WorldState();
|
||||
}
|
||||
|
||||
class _WorldState extends State<World> with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
ParticleManage pm = ParticleManage();
|
||||
Random random = Random();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
pm.size = Size(300, 200);
|
||||
|
||||
pm.addParticle(Particle(
|
||||
color: Colors.blue,
|
||||
size: 50,
|
||||
vx: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
vy: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
ay: 0.1,
|
||||
ax: 0.1,
|
||||
x: 150,
|
||||
y: 100));
|
||||
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
)
|
||||
..addListener(pm.tick)
|
||||
// ..repeat()
|
||||
;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
theWorld() {
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: theWorld,
|
||||
child: CustomPaint(
|
||||
size: pm.size,
|
||||
painter: WorldRender(manage: pm),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParticleManage with ChangeNotifier {
|
||||
List<Particle> particles = [];
|
||||
|
||||
Size size;
|
||||
|
||||
ParticleManage({this.size = Size.zero});
|
||||
|
||||
void setParticles(List<Particle> particles) {
|
||||
this.particles = particles;
|
||||
}
|
||||
|
||||
void addParticle(Particle particle) {
|
||||
particles.add(particle);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
particles.forEach(doUpdate);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void doUpdate(Particle p) {
|
||||
p.vy += p.ay; // y加速度变化
|
||||
p.vx += p.ax; // x加速度变化
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
if (p.x > size.width) {
|
||||
p.x = size.width;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.x < 0) {
|
||||
p.x = 0;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.y > size.height) {
|
||||
p.y = size.height;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
if (p.y < 0) {
|
||||
p.y = 0;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
particles.forEach((p) {
|
||||
p.x = 0;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class WorldRender extends CustomPainter {
|
||||
|
||||
final ParticleManage manage;
|
||||
|
||||
Paint fillPaint = Paint();
|
||||
|
||||
Paint stokePaint = Paint()
|
||||
..strokeWidth = 0.5
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
WorldRender({required this.manage}) : super(repaint: manage);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
canvas.drawRect(Offset.zero & size, stokePaint);
|
||||
manage.particles.forEach((particle) {
|
||||
drawParticle(canvas, particle);
|
||||
});
|
||||
}
|
||||
|
||||
void drawParticle(Canvas canvas, Particle particle) {
|
||||
fillPaint.color = particle.color;
|
||||
canvas.drawCircle(Offset(particle.x, particle.y), particle.size, fillPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant WorldRender oldDelegate) =>
|
||||
oldDelegate.manage != manage;
|
||||
}
|
||||
|
||||
|
||||
class Particle {
|
||||
/// x 位移.
|
||||
double x;
|
||||
|
||||
/// y 位移.
|
||||
double y;
|
||||
|
||||
/// 粒子水平速度.
|
||||
double vx;
|
||||
|
||||
// 粒子水平加速度
|
||||
double ax;
|
||||
|
||||
// 粒子竖直加速度
|
||||
double ay;
|
||||
|
||||
///粒子竖直速度.
|
||||
double vy;
|
||||
|
||||
|
||||
/// 粒子大小.
|
||||
double size;
|
||||
|
||||
/// 粒子颜色.
|
||||
Color color;
|
||||
|
||||
Particle({
|
||||
this.x = 0,
|
||||
this.y = 0,
|
||||
this.ax = 0,
|
||||
this.ay = 0,
|
||||
this.vx = 0,
|
||||
this.vy = 0,
|
||||
this.size = 0,
|
||||
this.color = Colors.black,
|
||||
});
|
||||
}
|
||||
244
packages/idraw/lib/p18/s05.dart
Normal file
244
packages/idraw/lib/p18/s05.dart
Normal file
@@ -0,0 +1,244 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
// import 'dart:ui' as ui;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:image/image.dart' as image;
|
||||
|
||||
/// create by 张风捷特烈 on 2020/11/5
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class Paper extends StatelessWidget {
|
||||
const Paper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
alignment: Alignment.center,
|
||||
child: const World(),
|
||||
);
|
||||
}
|
||||
}
|
||||
class World extends StatefulWidget {
|
||||
const World({super.key});
|
||||
|
||||
@override
|
||||
_WorldState createState() => _WorldState();
|
||||
}
|
||||
|
||||
class _WorldState extends State<World> with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
ParticleManage pm = ParticleManage();
|
||||
Random random = Random();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
pm.size = Size(400, 260);
|
||||
|
||||
initParticles();
|
||||
|
||||
// pm.addParticle(Particle(
|
||||
// color: Colors.blue,
|
||||
// size: 50,
|
||||
// vx: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
// vy: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
// ay: 0.1,
|
||||
// ax: 0.1,
|
||||
// x: 150,
|
||||
// y: 100));
|
||||
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
)..addListener(pm.tick)
|
||||
// ..repeat()
|
||||
;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
theWorld() {
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: theWorld,
|
||||
child: CustomPaint(
|
||||
size: pm.size,
|
||||
painter: WorldRender(manage: pm),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void initParticles() async {
|
||||
ByteData data = await rootBundle.load("assets/images/flutter.png");
|
||||
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
|
||||
image.Image? imageSrc = image.decodeImage(Uint8List.fromList(bytes));
|
||||
if(imageSrc==null) return;
|
||||
|
||||
double offsetX= (pm.size.width-imageSrc.width)/2;
|
||||
double offsetY= (pm.size.height-imageSrc.height)/2;
|
||||
|
||||
for (int i = 0; i < imageSrc.width; i++) {
|
||||
for (int j = 0; j < imageSrc.height; j++) {
|
||||
image.PixelUint8 pixel = imageSrc.getPixel(i, j) as image.PixelUint8;
|
||||
|
||||
if (pixel.toString() != '(255, 255, 255, 0)') {
|
||||
// print('-($i,$j)----${imageSrc.getPixel(i, j)}---------------');
|
||||
|
||||
Particle particle = Particle(
|
||||
x: i * 1.0+ offsetX,
|
||||
y: j * 1.0+ offsetY,
|
||||
vx: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
vy: 4 * random.nextDouble() * pow(-1, random.nextInt(20)),
|
||||
ay: 0.1,
|
||||
size: 0.5,
|
||||
color: Colors.blue); //产生粒子---每个粒子拥有随机的一些属性信息
|
||||
|
||||
pm.particles.add(particle);
|
||||
}
|
||||
}
|
||||
}
|
||||
setState(() {
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class ParticleManage with ChangeNotifier {
|
||||
List<Particle> particles = [];
|
||||
|
||||
Size size;
|
||||
|
||||
ParticleManage({this.size = Size.zero});
|
||||
|
||||
void setParticles(List<Particle> particles) {
|
||||
this.particles = particles;
|
||||
}
|
||||
|
||||
void addParticle(Particle particle) {
|
||||
particles.add(particle);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
particles.forEach(doUpdate);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void doUpdate(Particle p) {
|
||||
p.vy += p.ay; // y加速度变化
|
||||
p.vx += p.ax; // x加速度变化
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
if (p.x > size.width) {
|
||||
p.x = size.width;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.x < 0) {
|
||||
p.x = 0;
|
||||
p.vx = -p.vx;
|
||||
}
|
||||
if (p.y > size.height) {
|
||||
p.y = size.height;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
if (p.y < 0) {
|
||||
p.y = 0;
|
||||
p.vy = -p.vy;
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
particles.forEach((p) {
|
||||
p.x = 0;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class WorldRender extends CustomPainter {
|
||||
|
||||
final ParticleManage manage;
|
||||
|
||||
Paint fillPaint = Paint();
|
||||
|
||||
Paint stokePaint = Paint()
|
||||
..strokeWidth = 0.5
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
WorldRender({required this.manage}) : super(repaint: manage);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
// canvas.drawRect(Offset.zero&size, stokePaint);
|
||||
manage.particles.forEach((particle) {
|
||||
drawParticle(canvas, particle);
|
||||
});
|
||||
}
|
||||
|
||||
void drawParticle(Canvas canvas, Particle particle) {
|
||||
|
||||
fillPaint.color = particle.color;
|
||||
canvas.drawCircle(Offset(particle.x, particle.y), particle.size, fillPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant WorldRender oldDelegate) =>
|
||||
oldDelegate.manage != manage;
|
||||
}
|
||||
|
||||
|
||||
class Particle {
|
||||
/// x 位移.
|
||||
double x;
|
||||
|
||||
/// y 位移.
|
||||
double y;
|
||||
|
||||
/// 粒子水平速度.
|
||||
double vx;
|
||||
|
||||
// 粒子水平加速度
|
||||
double ax;
|
||||
|
||||
// 粒子竖直加速度
|
||||
double ay;
|
||||
|
||||
///粒子竖直速度.
|
||||
double vy;
|
||||
|
||||
|
||||
/// 粒子大小.
|
||||
double size;
|
||||
|
||||
/// 粒子颜色.
|
||||
Color color;
|
||||
|
||||
Particle({
|
||||
this.x = 0,
|
||||
this.y = 0,
|
||||
this.ax = 0,
|
||||
this.ay = 0,
|
||||
this.vx = 0,
|
||||
this.vy = 0,
|
||||
this.size = 0,
|
||||
this.color = Colors.black,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user