|
| 1 | +# p5.dart |
| 2 | + |
| 3 | +A Dart implementation of the [Processing API](https://processing.org/reference/) for [Flutter](https://flutter.io/). Processing is a software sketchbook and a language for learning how to code within the context of the visual arts. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +Add the p5 package to your Flutter app. For help using packages, see Flutter's online [documentation](https://flutter.io/using-packages/). |
| 8 | + |
| 9 | +You need to implement the widget containing the Processing sketch, and the sketch itself. |
| 10 | + |
| 11 | +A simple widget tree is the follwing: |
| 12 | + |
| 13 | +```dart |
| 14 | +import 'package:flutter/material.dart'; |
| 15 | +
|
| 16 | +import "package:p5/p5.dart"; |
| 17 | +import "sketch.dart"; |
| 18 | +
|
| 19 | +void main() => runApp(new MyApp()); |
| 20 | +
|
| 21 | +class MyHomePage extends StatefulWidget { |
| 22 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 23 | + final String title; |
| 24 | + @override |
| 25 | + _MyHomePageState createState() { |
| 26 | + return new _MyHomePageState(); |
| 27 | + } |
| 28 | +} |
| 29 | +
|
| 30 | +class MyApp extends StatelessWidget { |
| 31 | + // This widget is the root of your application. |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + return new MaterialApp( |
| 35 | + title: 'P5 Demo', |
| 36 | + theme: new ThemeData( |
| 37 | + // This is the theme of your application. |
| 38 | + primarySwatch: Colors.blue, |
| 39 | + ), |
| 40 | + home: new MyHomePage(title: 'P5 Demo Home Page'), |
| 41 | + ); |
| 42 | + } |
| 43 | +} |
| 44 | +
|
| 45 | +class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { |
| 46 | + MySketch sketch; |
| 47 | + PAnimator animator; |
| 48 | +
|
| 49 | + @override |
| 50 | + void initState() { |
| 51 | + super.initState(); |
| 52 | + sketch = new MySketch(); |
| 53 | + // Need an animator to call the draw() method in the sketch continuously, |
| 54 | + // otherwise it will be called only when touch events are detected. |
| 55 | + animator = new PAnimator(this); |
| 56 | + animator.addListener(() { |
| 57 | + setState(() { |
| 58 | + sketch.redraw(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + animator.run(); |
| 62 | + } |
| 63 | +
|
| 64 | + @override |
| 65 | + Widget build(BuildContext context) { |
| 66 | + return new Scaffold( |
| 67 | + appBar: new AppBar(title: new Text("P5 Draw!")), |
| 68 | + backgroundColor: const Color.fromRGBO(200, 200, 200, 1.0), |
| 69 | + body: new Center( |
| 70 | + child:new PWidget(sketch), |
| 71 | + ), |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +The MySketch class has to extend the base PPainter clas in the p5 package, and implement the setup() and draw() functions: |
| 79 | + |
| 80 | +```dart |
| 81 | +class MySketch extends PPainter { |
| 82 | + void setup() { |
| 83 | + size(300, 300); |
| 84 | + } |
| 85 | +
|
| 86 | + void draw() { |
| 87 | + background(color(255, 255, 255)); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Both pieces of code can be inside single dart file, or on separate files for better clarity. |
| 93 | + |
| 94 | +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: |
| 95 | + |
| 96 | +```dart |
| 97 | +class MySketch extends PPainter { |
| 98 | + var strokes = new List<List<PVector>>(); |
| 99 | +
|
| 100 | + void setup() { |
| 101 | + fullScreen(); |
| 102 | + } |
| 103 | +
|
| 104 | + void draw() { |
| 105 | + background(color(255, 255, 255)); |
| 106 | +
|
| 107 | + noFill(); |
| 108 | + strokeWeight(10); |
| 109 | + stroke(color(10, 40, 200, 60)); |
| 110 | + for (var stroke in strokes) { |
| 111 | + beginShape(); |
| 112 | + for (var p in stroke) { |
| 113 | + vertex(p.x, p.y); |
| 114 | + } |
| 115 | + endShape(); |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | + void mousePressed() { |
| 120 | + strokes.add([new PVector(mouseX, mouseY)]); |
| 121 | + } |
| 122 | +
|
| 123 | + void mouseDragged() { |
| 124 | + var stroke = strokes.last; |
| 125 | + stroke.add(new PVector(mouseX, mouseY)); |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +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: |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +For help getting started with Flutter, view our online [documentation](https://flutter.io/). |
| 135 | + |
| 136 | +For help on editing package code, view the [documentation](https://flutter.io/developing-packages/). |
0 commit comments