Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 9120ff0

Browse files
committed
first commit
0 parents  commit 9120ff0

11 files changed

+1118
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.atom/
3+
.dart_tool/
4+
.idea
5+
.packages
6+
.pub/
7+
packages

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [0.0.1] - June 26, 2018.
2+
3+
* First release with a few Processing API functions in PPainter.
4+
* PAnimator utility class to drive continuous frama animation.
5+
* Basic touch event handling
6+

LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

README.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
![P5 drawing sketch running in iPhone 6S and Nexus 5X](http://processing.andrescolubri.net/libraries/p5.dart/p5dart.jpg)
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/).

example/app.dart

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter/material.dart';
2+
3+
import "package:p5/p5.dart";
4+
import "sketch.dart";
5+
6+
void main() => runApp(new MyApp());
7+
8+
class MyHomePage extends StatefulWidget {
9+
MyHomePage({Key key, this.title}) : super(key: key);
10+
final String title;
11+
@override
12+
_MyHomePageState createState() {
13+
return new _MyHomePageState();
14+
}
15+
}
16+
17+
class MyApp extends StatelessWidget {
18+
// This widget is the root of your application.
19+
@override
20+
Widget build(BuildContext context) {
21+
return new MaterialApp(
22+
title: 'P5 Demo',
23+
theme: new ThemeData(
24+
// This is the theme of your application.
25+
primarySwatch: Colors.blue,
26+
),
27+
home: new MyHomePage(title: 'P5 Demo Home Page'),
28+
);
29+
}
30+
}
31+
32+
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
33+
MySketch sketch;
34+
PAnimator animator;
35+
36+
@override
37+
void initState() {
38+
super.initState();
39+
sketch = new MySketch();
40+
// Need an animator to call the draw() method in the sketch continuously,
41+
// otherwise it will be called only when touch events are detected.
42+
animator = new PAnimator(this);
43+
animator.addListener(() {
44+
setState(() {
45+
sketch.redraw();
46+
});
47+
});
48+
animator.run();
49+
}
50+
51+
@override
52+
Widget build(BuildContext context) {
53+
return new Scaffold(
54+
appBar: new AppBar(title: new Text("P5 Draw!")),
55+
backgroundColor: const Color.fromRGBO(200, 200, 200, 1.0),
56+
body: new Center(
57+
child:new PWidget(sketch),
58+
),
59+
);
60+
}
61+
}

example/sketch.dart

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "package:p5/p5.dart";
2+
3+
class MySketch extends PPainter {
4+
var strokes = new List<List<PVector>>();
5+
6+
void setup() {
7+
// size(300, 300);
8+
fullScreen();
9+
}
10+
11+
void draw() {
12+
background(color(255, 255, 255));
13+
14+
noFill();
15+
strokeWeight(10);
16+
stroke(color(10, 40, 200, 60));
17+
for (var stroke in strokes) {
18+
beginShape();
19+
for (var p in stroke) {
20+
vertex(p.x, p.y);
21+
}
22+
endShape();
23+
}
24+
}
25+
26+
void mousePressed() {
27+
strokes.add([new PVector(mouseX, mouseY)]);
28+
}
29+
30+
void mouseDragged() {
31+
var stroke = strokes.last;
32+
stroke.add(new PVector(mouseX, mouseY));
33+
}
34+
}

0 commit comments

Comments
 (0)