Skip to content

Commit daba8f6

Browse files
doc & new api
1 parent c58ebb3 commit daba8f6

File tree

14 files changed

+169
-110
lines changed

14 files changed

+169
-110
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [0.1.1] - 2020-May-5.
2+
3+
* `FillConfig no has a builder`
4+
* `Rough.draw` is now `drawRough`, a extension method of ``Draw
5+
* Basic external api documentation
6+
17
## [0.1.0+2] - 2020-May-5.
28

39
* Basic documentation

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,37 @@ To draw a figure you have to:
2424
2. Create a `Filler` to be used when drawing objects (you have to provide a configuration for the filling and a `DrawConfig` for the filling path).
2525
3. Create a `Generator` object using the created `DrawConfig` and `Filler`. This will define a drawing/filling style.
2626
4. Invoke the drawing method from the `Generator` to create a `Drawable`.
27-
5. Paint the `Drawable` in the canvas using the `Rough.draw` method.
27+
5. Paint the `Drawable` in the canvas using the `drawRough` method extension for `Canvas`.
2828

29-
Here an example on how to draw a rectangle:
29+
Here an example on how to draw a circle:
3030

3131
```dart
32-
//Create a `DrawConfig` object.
33-
DrawConfig myDrawConfig = DrawConfig.build(
34-
roughness: 3,
35-
curveStepCount: 14,
36-
maxRandomnessOffset: 3,
37-
);
38-
39-
//Create a `Filler` (we reuse the drawConfig in this case).
40-
Filler myFiller = ZigZagFiller(
41-
FillerConfig(
42-
hachureGap: 8,
43-
hachureAngle: -20,
44-
drawConfig: myDrawConfig,
45-
),
46-
);
47-
48-
//3Create a `Generator` with the created `DrawConfig` and `Filler`
49-
Generator generator = Generator(
50-
myDrawConfig,
51-
myFiller,
52-
);
53-
54-
//4. Build a circle `Drawable`.
55-
Drawable figure = generator.circle(200, 200, 320);
56-
57-
//5. Paint the `Drawable` in the canvas.
58-
Rough().draw(canvas, figure, pathPaint, fillPaint);
32+
//Create a `DrawConfig` object.
33+
DrawConfig myDrawConfig = DrawConfig.build(
34+
roughness: 3,
35+
curveStepCount: 14,
36+
maxRandomnessOffset: 3,
37+
);
38+
39+
//Create a `Filler` with a configuration (we reuse the drawConfig in this case).
40+
FillerConfig myFillerConfig = FillerConfig(
41+
hachureGap: 8,
42+
hachureAngle: -20,
43+
drawConfig: myDrawConfig,
44+
);
45+
Filler myFiller = ZigZagFiller(myFillerConfig);
46+
47+
//Create a `Generator` with the created `DrawConfig` and `Filler`
48+
Generator generator = Generator(
49+
myDrawConfig,
50+
myFiller,
51+
);
52+
53+
//4. Build a circle `Drawable`.
54+
Drawable figure = generator.circle(200, 200, 320);
55+
56+
//5. Paint the `Drawable` in the canvas.
57+
Canvas.drawRough(figure, pathPaint, fillPaint);
5958
```
6059

6160
And this is the result:

example/lib/pages/flutter_logo.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class FlutterLogoPage extends StatelessWidget {
2424
}
2525

2626
class FlutterLogoPainter extends InteractivePainter {
27-
final Rough rough = Rough();
28-
2927
Paint stroke = Paint()
3028
..strokeWidth = 2
3129
..isAntiAlias = true
@@ -43,12 +41,11 @@ class FlutterLogoPainter extends InteractivePainter {
4341

4442
@override
4543
void paintRough(canvas, size) {
46-
FillerConfig fillerConfig = FillerConfig(
44+
FillerConfig fillerConfig = FillerConfig.build(
4745
hachureAngle: 90,
4846
drawConfig: drawConfig,
4947
fillWeight: 10,
5048
hachureGap: 5,
51-
// zigzagOffset: 10,
5249
);
5350

5451
Generator gen = Generator(drawConfig, HachureFiller(fillerConfig));
@@ -62,11 +59,11 @@ class FlutterLogoPainter extends InteractivePainter {
6259

6360
canvas
6461
..translate(translateX, translateY)
65-
..scale(scale);
66-
rough.draw(canvas, gen.polygon([PointD(37, 128), PointD(9, 101), PointD(100, 10), PointD(156, 10)]), stroke, fillPaint);
67-
canvas.translate(-4, -4);
68-
rough.draw(canvas, gen.polygon([PointD(156, 94), PointD(100, 94), PointD(50, 141), PointD(79, 170)]), stroke, fillPaint);
69-
canvas.translate(6, 6);
70-
rough.draw(canvas, gen.polygon([PointD(79, 170), PointD(100, 191), PointD(156, 191), PointD(107, 142)]), stroke, fillPaint);
62+
..scale(scale)
63+
..drawRough(gen.polygon([PointD(37, 128), PointD(9, 101), PointD(100, 10), PointD(156, 10)]), stroke, fillPaint)
64+
..translate(-4, -4)
65+
..drawRough(gen.polygon([PointD(156, 94), PointD(100, 94), PointD(50, 141), PointD(79, 170)]), stroke, fillPaint)
66+
..translate(6, 6)
67+
..drawRough(gen.polygon([PointD(79, 170), PointD(100, 191), PointD(156, 191), PointD(107, 142)]), stroke, fillPaint);
7168
}
7269
}

example/lib/pages/interactive_arc.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class ArcPainter extends InteractivePainter {
4141

4242
@override
4343
void paintRough(Canvas canvas, Size size) {
44-
Generator generator = Generator(drawConfig, HachureFiller(const FillerConfig().copyWith(drawConfig: drawConfig)));
44+
Generator generator = Generator(drawConfig, HachureFiller(FillerConfig.build().copyWith(drawConfig: drawConfig)));
4545
double s = min(size.width, size.height);
4646
Drawable figure = generator.arc(size.width / 2, size.height / 2, s * 0.8, s * 0.8, pi * 0.2, pi * 1.8, true);
47-
Rough().draw(canvas, figure, pathPaint, fillPaint);
47+
canvas.drawRough(figure, pathPaint, fillPaint);
4848
}
4949
}

example/lib/pages/interactive_circle.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class CirclePainter extends InteractivePainter {
4141

4242
@override
4343
void paintRough(Canvas canvas, Size size) {
44-
Generator generator = Generator(drawConfig, NoFiller(const FillerConfig().copyWith(drawConfig: drawConfig)));
44+
Generator generator = Generator(drawConfig, NoFiller(FillerConfig.build().copyWith(drawConfig: drawConfig)));
4545
double s = min(size.width, size.height);
4646
Drawable figure = generator.circle(size.width / 2, size.height / 2, s * 0.8);
47-
Rough().draw(canvas, figure, pathPaint, fillPaint);
47+
canvas.drawRough(figure, pathPaint, fillPaint);
4848
}
4949
}

example/lib/pages/interactive_rectangle.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class Rectangle extends InteractivePainter {
3939

4040
@override
4141
void paintRough(Canvas canvas, Size size) {
42-
Generator generator = Generator(drawConfig, NoFiller(const FillerConfig().copyWith(drawConfig: drawConfig)));
42+
Generator generator = Generator(drawConfig, NoFiller(FillerConfig.build().copyWith(drawConfig: drawConfig)));
4343

4444
Drawable figure = generator.rectangle(size.width * 0.1, size.height * 0.2, size.width * 0.8, size.height * 0.6);
45-
Rough().draw(canvas, figure, pathPaint, fillPaint);
45+
canvas.drawRough(figure, pathPaint, fillPaint);
4646
}
4747
}

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ packages:
122122
path: ".."
123123
relative: true
124124
source: path
125-
version: "0.1.0+1"
125+
version: "0.1.0+2"
126126
sky_engine:
127127
dependency: transitive
128128
description: flutter
@@ -192,4 +192,4 @@ packages:
192192
source: hosted
193193
version: "3.5.0"
194194
sdks:
195-
dart: ">=2.4.0 <3.0.0"
195+
dart: ">=2.6.0 <3.0.0"

lib/src/config.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import 'dart:math';
22

3+
// Describe how a particular shape is drawn.
34
class DrawConfig {
4-
final double maxRandomnessOffset; //line + solidFiller
5-
final double roughness; //everything
6-
final double bowing; //line
7-
final double curveFitting; //ellipse
8-
final double curveTightness; //curve
9-
final double curveStepCount; //generateEllipseParams
5+
final double maxRandomnessOffset;
6+
final double roughness;
7+
final double bowing;
8+
final double curveFitting;
9+
final double curveTightness;
10+
final double curveStepCount;
1011
final int seed;
1112
final Randomizer randomizer;
1213

@@ -31,6 +32,13 @@ class DrawConfig {
3132
this.randomizer,
3233
});
3334

35+
/// Generates a [DrawConfig]
36+
/// * [roughness] Numerical value indicating how rough the drawing is. A rectangle with the roughness of 0 would be a perfect rectangle. Default value is 1. There is no upper limit to this value, but a value over 10 is mostly useless.
37+
/// * [bowing] Numerical value indicating how curvy the lines are when drawing a sketch. A value of 0 will cause straight lines. Default value is 1.
38+
/// * [seed] The seed for creating random values used in shape generation. This is useful for creating the exact shape when re-generating with the same parameters. Default value is 1.
39+
/// * [curveStepCount] When drawing ellipses, circles, and arcs, Rough approximates [curveStepCount] number of points to estimate the shape. Default value is 9.
40+
/// * [curveTightness]
41+
/// * [curveFitting] When drawing ellipses, circles, and arcs, it means how close should the rendered dimensions be when compared to the specified one. Default value is 0.95.
3442
static DrawConfig build({
3543
double maxRandomnessOffset,
3644
double roughness,

lib/src/filler.dart

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,43 @@ class FillerConfig {
2424
final double dashGap;
2525
final double zigzagOffset;
2626

27-
const FillerConfig({
27+
const FillerConfig._({
2828
DrawConfig drawConfig,
29-
this.fillWeight = 1,
30-
this.hachureAngle = -41,
31-
this.hachureGap = 15,
32-
this.dashOffset = 15,
33-
this.dashGap = 2,
34-
this.zigzagOffset = 5,
29+
this.fillWeight,
30+
this.hachureAngle,
31+
this.hachureGap,
32+
this.dashOffset,
33+
this.dashGap,
34+
this.zigzagOffset,
3535
}) : _drawConfig = drawConfig;
3636

37+
/// * [fillWeigh] When using dots styles to fill the shape, this value represents the diameter of the dot.
38+
/// * [hachureAngle] Numerical value (in degrees) that defines the angle of the hachure lines. Default value is -41 degrees.
39+
/// * [hachureGap] Numerical value that defines the average gap, in pixels, between two hachure lines. Default value is 15.
40+
/// * [dashOffset] When filling a shape using the [DashedFiller], this property indicates the nominal length of dash (in pixels). If not set, it defaults to the hachureGap value.
41+
/// * [dashGap] When filling a shape using the [DashedFiller], this property indicates the nominal gap between dashes (in pixels). If not set, it defaults to the hachureGap value.
42+
/// * [zigzagOffset] When filling a shape using the [ZigZagLineFiller], this property indicates the nominal width of the zig-zag triangle in each line. If not set, it defaults to the hachureGap value.
43+
static FillerConfig build({
44+
DrawConfig drawConfig,
45+
double fillWeight = 1,
46+
double hachureAngle = -41,
47+
double hachureGap = 15,
48+
double dashOffset = 15,
49+
double dashGap = 2,
50+
double zigzagOffset = 5,
51+
}) =>
52+
FillerConfig._(
53+
drawConfig: drawConfig,
54+
fillWeight: fillWeight,
55+
hachureAngle: hachureAngle,
56+
hachureGap: hachureGap,
57+
dashOffset: dashOffset,
58+
dashGap: dashGap,
59+
zigzagOffset: zigzagOffset,
60+
);
61+
62+
static FillerConfig defaultConfig = FillerConfig.build(drawConfig: DrawConfig.defaultValues);
63+
3764
DrawConfig get drawConfig => _drawConfig;
3865

3966
FillerConfig copyWith({
@@ -45,7 +72,7 @@ class FillerConfig {
4572
double dashGap,
4673
double zigzagOffset,
4774
}) =>
48-
FillerConfig(
75+
FillerConfig._(
4976
drawConfig: drawConfig ?? _drawConfig,
5077
fillWeight: fillWeight ?? this.fillWeight,
5178
hachureAngle: hachureAngle ?? this.hachureAngle,
@@ -59,7 +86,7 @@ class FillerConfig {
5986
abstract class Filler {
6087
final FillerConfig config;
6188

62-
Filler(this.config);
89+
Filler(this.config) : assert(config != null, 'FillerConfig could not be null');
6390

6491
OpSet fill(List<PointD> points);
6592

@@ -104,9 +131,9 @@ abstract class Filler {
104131
}
105132
List<Edge> removed = edges.sublist(0, ix + 1);
106133
edges.removeRange(0, ix + 1);
107-
removed.forEach((edge) {
134+
for (Edge edge in removed) {
108135
activeEdges.add(ActiveEdge(y, edge));
109-
});
136+
}
110137
}
111138
activeEdges = activeEdges.where((ae) => ae.edge.yMax > y).toList();
112139

@@ -268,7 +295,7 @@ abstract class Filler {
268295
}
269296

270297
class NoFiller extends Filler {
271-
NoFiller([FillerConfig config = const FillerConfig()]) : super(config);
298+
NoFiller([FillerConfig config]) : super(config);
272299

273300
@override
274301
OpSet fill(List<PointD> points) {
@@ -277,7 +304,7 @@ class NoFiller extends Filler {
277304
}
278305

279306
class HachureFiller extends Filler {
280-
HachureFiller([FillerConfig config = const FillerConfig()]) : super(config);
307+
HachureFiller([FillerConfig config]) : super(config);
281308

282309
@override
283310
OpSet fill(List<PointD> points) {
@@ -286,7 +313,7 @@ class HachureFiller extends Filler {
286313
}
287314

288315
class ZigZagFiller extends Filler {
289-
ZigZagFiller([FillerConfig config = const FillerConfig()]) : super(config);
316+
ZigZagFiller([FillerConfig config]) : super(config);
290317

291318
@override
292319
OpSet fill(List<PointD> points) {
@@ -295,7 +322,7 @@ class ZigZagFiller extends Filler {
295322
}
296323

297324
class HatchFiller extends Filler {
298-
HatchFiller([FillerConfig config = const FillerConfig()]) : super(config);
325+
HatchFiller([FillerConfig config]) : super(config);
299326

300327
@override
301328
OpSet fill(List<PointD> points) {
@@ -307,7 +334,7 @@ class HatchFiller extends Filler {
307334
}
308335

309336
class DashedFiller extends Filler {
310-
DashedFiller([FillerConfig config = const FillerConfig()]) : super(config);
337+
DashedFiller([FillerConfig config]) : super(config);
311338

312339
@override
313340
OpSet fill(List<PointD> points) {
@@ -348,7 +375,7 @@ class DashedFiller extends Filler {
348375
}
349376

350377
class DotFiller extends Filler {
351-
DotFiller([FillerConfig config = const FillerConfig()]) : super(config);
378+
DotFiller([FillerConfig config]) : super(config);
352379

353380
@override
354381
OpSet fill(List<PointD> points) {
@@ -385,7 +412,7 @@ class DotFiller extends Filler {
385412
}
386413

387414
class SolidFiller extends Filler {
388-
SolidFiller([FillerConfig config = const FillerConfig()]) : super(config);
415+
SolidFiller([FillerConfig config]) : super(config);
389416

390417
@override
391418
OpSet fill(List<PointD> points) {

0 commit comments

Comments
 (0)