搜索插件、代码仓与 APP 分类

插件详情v0.1.0

p5

这是一个 Dart 实现的 Processing API,可用于在 Flutter 应用程序中编程交互式图形。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
23
喜欢数
31
Pub 分数
130

安装插件

flutter pub add p5

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

p5 包提供了 Processing API 的 Dart 实现,使开发者能够在 Flutter 应用中创建交互式图形和可视化效果。受流行的 Processing 语言启发,它提供了一种简单直观的语法来绘制形状、处理动画并响应用户输入。这使其非常适合创意编程、数据可视化和教育项目。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,确保广泛的兼容性。它与 Flutter 的 widget 系统无缝集成,可在应用中实现丰富动态的视觉效果。

适合场景

  • 互动艺术装置
  • 数据可视化仪表板
  • 教育类编码工具
  • 创意原型设计
  • 动画UI元素

主要特点

  • 类似处理的语法
  • 实时动画支持
  • 跨平台渲染
  • 鼠标和触摸事件处理
  • 内置的形状和颜色函数

适合对象

  • 创意编码者
  • 教授编程的教育工作者
  • 使用代码的视觉艺术家
  • Flutter 开发者探索图形
  • 初学者学习交互设计

使用建议

  • 性能在低端设备上可能有所不同
  • 与原始 Processing 相比,文档有限
  • 不适用于复杂的游戏开发
  • 需要了解基本的图形概念
  • 由于渲染引擎,可能导致应用体积增大

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

p5.dart

A Dart implementation of the Processing API for Flutter. Processing is a software sketchbook and a language for learning how to code within the context of the visual arts.

Note: this is an experimental library that only implements a few functions from the Processing API. Class names and structure will likely change in the future. And feel free to fork and play around with the code :-)

Getting Started

Add the p5 package to your Flutter app. For help using packages, see Flutter's online documentation.

You need to implement the widget containing the Processing sketch, and the sketch itself.

A simple widget tree is the follwing:

import 'package:flutter/material.dart';

import "package:p5/p5.dart";
import "sketch.dart";

void main() => runApp(new MyApp());

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() {
    return new _MyHomePageState();
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'P5 Demo',
      theme: new ThemeData(
        // This is the theme of your application.
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'P5 Demo Home Page'),
    );
  }
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  MySketch sketch;
  PAnimator animator;

  @override
  void initState() {
    super.initState();
    sketch = new MySketch();
    // Need an animator to call the draw() method in the sketch continuously,
    // otherwise it will be called only when touch events are detected.
    animator = new PAnimator(this);
    animator.addListener(() {
      setState(() {
        sketch.redraw();
      });
    });
    animator.run();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text("P5 Draw!")),
      backgroundColor: const Color.fromRGBO(200, 200, 200, 1.0),
      body: new Center(
        child:new PWidget(sketch),
      ),
    );
  }
}

The MySketch class has to extend the base PPainter class in the p5 package, and implement the setup() and draw() functions, just like in Processing sketch:

class MySketch extends PPainter {
  void setup() {
    size(300, 300);
  }

  void draw() {
    background(color(255, 255, 255));
  }
}

Both pieces of code can be inside single dart file, or on separate files for better clarity.

A simple drawing sketch can be implemented by handling the mouse dragging, and storing the pointer positions in a list of PVector object holding the (x, y) coordinates, which are then used to draw lines:

class MySketch extends PPainter {
  var strokes = new List<List<PVector>>();

  void setup() {
    fullScreen();
  }

  void draw() {
    background(color(255, 255, 255));

    noFill();
    strokeWeight(10);
    stroke(color(10, 40, 200, 60));
    for (var stroke in strokes) {
      beginShape();
      for (var p in stroke) {
        vertex(p.x, p.y);
      }
      endShape();
    }
  }

  void mousePressed() {
    strokes.add([new PVector(mouseX, mouseY)]);
  }

  void mouseDragged() {
    var stroke = strokes.last;
    stroke.add(new PVector(mouseX, mouseY));
  }
}

Notice that the size of the drawing area can be made to fill the entire app by using fullScreen(). The app should give similar results for both iOS and Android.

For help getting started with Flutter, view our online documentation.

For help on editing package code, view the documentation.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。