install

dependencies:
p5: ^0.0.5

main.dart

import 'package:flutter/material.dart';

import "package:p5/p5.dart";
import "sketch.dart"; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
} class HomePage extends StatefulWidget {
@override
_HomePageState createState() {
return _HomePageState();
}
} class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
MySketch sketch;
PAnimator animator; @override
void initState() {
super.initState();
sketch = new MySketch();
// 需要动画师连续调用草图中的draw()方法,
// 否则只有在检测到触摸事件时才会调用它。
animator = new PAnimator(this);
animator.addListener(() {
setState(() {
sketch.redraw();
});
});
animator.run();
} @override
Widget build(BuildContext context) {
return Scaffold(body: Center(child: PWidget(sketch)));
}
}

sketch.dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import "package:p5/p5.dart"; Blob blob;
List<Blob> blobs = [];
double zoom = 1;
double yoff = 0; /// 从一个范围内映射一个数字去另一个范围。
double map(num v, num start1, num stop1, num start2, num stop2) {
return (v - start1) / (stop1 - start1) * (stop2 - start2) + start2;
} var perlin;
const TWO_PI = math.pi * 2;
const PERLIN_YWRAPB = 4;
const PERLIN_YWRAP = 1 << PERLIN_YWRAPB;
const PERLIN_ZWRAPB = 8;
const PERLIN_ZWRAP = 1 << PERLIN_ZWRAPB;
const PERLIN_SIZE = 4095; var perlinOctaves = 4; // default to medium smooth
var perlinAmpFalloff = 0.5; // 50% reduction/octave double scaledCosine(double i) {
return 0.5 * (1.0 - math.cos(i * math.pi));
} /// 返回所定义坐标的柏林噪声值。柏林噪声是个用来生成比 random() 所能生成更自然及更谐波的随机数字系列。在 1980 年代有 Ken Perlin 所发明,柏林噪声至今常被用在图形应用程序中生成程序纹理、自然运动、形状、地形等等。
double noise(double x, double y, [double z]) {
y = y ?? 0;
z = z ?? 0; if (perlin == null) {
perlin = List(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
// perlin[i] = Math.random();
perlin[i] = math.Random().nextDouble();
}
} if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
if (z < 0) {
z = -z;
} var xi = x.floor(), yi = y.floor(), zi = z.floor();
var xf = x - xi;
var yf = y - yi;
var zf = z - zi;
var rxf, ryf; double r = 0;
var ampl = 0.5; var n1, n2, n3; for (var o = 0; o < perlinOctaves; o++) {
var of = xi + (yi << PERLIN_YWRAPB) + (zi << PERLIN_ZWRAPB); rxf = scaledCosine(xf);
ryf = scaledCosine(yf); n1 = perlin[of & PERLIN_SIZE];
n1 += rxf * (perlin[(of + 1) & PERLIN_SIZE] - n1);
n2 = perlin[(of + PERLIN_YWRAP) & PERLIN_SIZE];
n2 += rxf * (perlin[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n2);
n1 += ryf * (n2 - n1); of += PERLIN_ZWRAP;
n2 = perlin[of & PERLIN_SIZE];
n2 += rxf * (perlin[(of + 1) & PERLIN_SIZE] - n2);
n3 = perlin[(of + PERLIN_YWRAP) & PERLIN_SIZE];
n3 += rxf * (perlin[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n3);
n2 += ryf * (n3 - n2); n1 += scaledCosine(zf) * (n2 - n1); r += n1 * ampl;
ampl *= perlinAmpFalloff;
xi <<= 1;
xf *= 2;
yi <<= 1;
yf *= 2;
zi <<= 1;
zf *= 2; if (xf >= 1.0) {
xi++;
xf--;
}
if (yf >= 1.0) {
yi++;
yf--;
}
if (zf >= 1.0) {
zi++;
zf--;
}
}
return r;
} /// 计算一个介于两个数字之间所定义的插值量位置的数字。amt 参数为两个值之间的插值量,0.0 为第一个值,0.1 为非常接近第一个值,0.5 为两者之间等等。lerp 函数可用来沿着直线制作动画及绘制虚线。
double lerp(double start, double stop, double amt) {
return amt * (stop - start) + start;
} class MySketch extends PPainter {
void setup() {
// fullScreen();
size(300, 300);
blob = Blob(0, 0, 50);
} void draw() {
background(Colors.black);
translate(width / 2, height / 2);
double newzoom = 128 / blob.r;
zoom = lerp(zoom, newzoom, 0.1);
scale(zoom, zoom);
blob.show(this);
}
} class Blob {
double x, y, r;
Blob(this.x, this.y, this.r); show(PPainter p) {
const double depth = 35;
p.fill(p.color(0, 255, 0));
p.noStroke();
p.push();
p.beginShape();
double xoff = 0;
for (double a = 0; a < TWO_PI; a += 0.001) {
double offset = map(noise(xoff, yoff), 0, 1, -depth, depth);
double _r = r + offset;
double x = _r * math.cos(a);
double y = _r * math.sin(a);
p.vertex(x, y); xoff += 0.09;
}
p.endShape();
p.pop();
yoff += 0.02;
}
}

Flutter 使用p5的更多相关文章

  1. 7、Flutter banner_view 轮播图的使用

    1.前言 实现轮播图,效果如下: 2.实现 将采用 banner_view 实现:资源库地址 2.1.yaml 引入依赖 在 pubspec.yaml 声明需要引用的库,执行命令 flutter pa ...

  2. Flutter 多引擎支持 PlatformView 以及线程合并解决方案

    作者:字节移动技术-李皓骅 摘要 本文介绍了 Flutter 多引擎下,使用 PlatformView 场景时不能绕开的一个线程合并问题,以及它最终的解决方案.最终 Pull Request 已经 m ...

  3. Flutter 初尝:从 Java 无缝过渡

    准备阶段 下载 Flutter SDK 新建 Flutter 文件夹,克隆 Flutter SDK: git clone -b beta https://github.com/flutter/flut ...

  4. 编写第一个Flutter App(翻译)

    博客搬迁至http://blog.wangjiegulu.com RSS订阅:http://blog.wangjiegulu.com/feed.xml 以下代码 Github 地址:https://g ...

  5. 【译】Java、Kotlin、RN、Flutter 开发出来的 App 大小,你了解过吗?

    现在开发 App 的方式非常多,原生.ReactNative.Flutter 都是不错的选择.那你有没有关注过,使用不同的方式,编译生成的 Apk ,大小是否会有什么影响呢?本文就以一个最简单的 He ...

  6. 我花了 8 小时,"掌握"了一下 Flutter | Flutter 中文站上线

    Hi,大家好,我是承香墨影! 距离 Google 在 2018 世界移动大会上发布 Flutter 的 Beta 版本,Flutter 是 Google 用以帮助开发者在 Android 和 iOS ...

  7. flutter初体验

    flutter初体验 和flutter斗争了两个周末,基本弄清楚了这个玩意的布局和一些常用组件了. 在flutter里面,所有东西都是组件Widget.我们像拼接积木一样拼接Widget,拼接的关键词 ...

  8. Flutter 实现原理及在马蜂窝的跨平台开发实践

    一直以来,跨平台开发都是困扰移动客户端开发的难题. 在马蜂窝旅游 App 很多业务场景里,我们尝试过一些主流的跨平台开发解决方案, 比如 WebView 和 React Native,来提升开发效率和 ...

  9. Flutter 即学即用系列博客——05 StatelessWidget vs StatefulWidget

    前言 上一篇我们对 Flutter UI 有了一个基本的了解. 这一篇我们通过自定义 Widget 来了解下如何写一个 Widget? 然而 Widget 有两个,StatelessWidget 和 ...

随机推荐

  1. P5518 [MtOI2019]幽灵乐团 / 莫比乌斯反演基础练习题

    瞎扯 建议在阅读题解之前欣赏这首由普莉兹姆利巴姐妹带来的的合奏. Q:你参加省选吗?不是说好了考完 NOIP 就退吗. A:对啊. Q:那你学这玩意干啥? A:对啊,我学这玩意干啥? 写这题的动机? ...

  2. nginx常用功能和配置

    nginx常用功能和配置 1.nginx常用功能和配置 1.1 限流 1.2 压力测试工具--Ab 1.2.1安装 1.2.2 测试 1.2.3 返回值 1.3 limit_conn_zone 1.4 ...

  3. SpringMVC传递JSON数据

    文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...

  4. 报表生成工具ireport

    最近又开始学习新的玩意儿了,扒拉扒拉网上的资源,先捣鼓个思维导图.

  5. java导出xls

    package com.spring.mvc.xls; import java.io.File;import java.io.FileInputStream;import java.text.Deci ...

  6. Java反射应用--2

    Java反射开窍第一篇 明天再写

  7. HDU-6704 K-th occurrence(后缀数组+主席树)

    题意 给一个长度为n的字符串,Q次询问,每次询问\((l,r,k)\) , 回答子串\(s_ls_{l+1}\cdots s_r\) 第\(k\) 次出现的位置,若不存在输出-1.\(n\le 1e5 ...

  8. 2019 Multi-University Training Contest 7 Kejin Player(期望)

    题意:给定在当前等级升级所需要的花费 每次升级可能会失败并且掉级 然后q次询问从l到r级花费的期望 思路:对于单次升级的期望 我们可以列出方程: 所以我们可以统计一下前缀和 每次询问O1回答 #inc ...

  9. 2015 German Collegiate Programming Contest (GCPC 15) + POI 10-T3(12/13)

    $$2015\ German\ Collegiate\ Programming\ Contest\ (GCPC 15) + POI 10-T3$$ \(A.\ Journey\ to\ Greece\ ...

  10. poj 2566 Bound Found 尺取法

    一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...