bezier.dart is a simple open-source Dart library for handling 2D Bézier curve math.
The library was developed, documented, and published by Aaron Barrett and Isaac Barrett. It is based heavily on the work of Pomax, including his excellent Primer on Bézier Curves and his original JavaScript library, Bezier.js.
We're trying to design bezier.dart to be both platform independent and context independent. You can run the library anywhere you can run Dart: in a web browser, in a Flutter application, server side, and beyond.
For live examples of the library's API, see the project page at dartographer.com/bezier.
t
along a curvet
parameter valuet
parameter valuesdependencies:
bezier: any
import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
void main() {
// bezier.dart supports both quadratic curves...
final quadraticCurve = QuadraticBezier([
Vector2(-40.0, -40.0),
Vector2(30.0, 10.0),
Vector2(55.0, 25.0)
]);
// ...and cubic curves!
final cubicCurve = CubicBezier([
Vector2(10.0, 10.0),
Vector2(70.0, 95.0),
Vector2(25.0, 20.0),
Vector2(15.0, 80.0)
]);
}
t
of 0.75
.import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
void main() {
final curve = QuadraticBezier([
Vector2(10.0, 10.0),
Vector2(70.0, 95.0),
Vector2(15.0, 80.0)
]);
final computedPoint = curve.pointAt(0.75);
}
t
parameter values of 0.2
and 0.6
.import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
void main() {
final curve = CubicBezier([
Vector2(10.0, 10.0),
Vector2(70.0, 95.0),
Vector2(25.0, 20.0),
Vector2(15.0, 80.0)
]);
final subcurve = curve.subcurveBetween(0.2, 0.6);
}
t
values between a curve and a line segment.import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
void main() {
final curve = QuadraticBezier([
Vector2(10.0, 500.0),
Vector2(50.0, 0.0),
Vector2(90.0, 500.0)
]);
final lineStart = Vector2(0.0, 400.0);
final lineEnd = Vector2(100.0, 410.0);
final intersections = curve.intersectionsWithLineSegment(lineStart, lineEnd);
}
12.0
.import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";
void main() {
final curve = CubicBezier([
Vector2(10.0, 10.0),
Vector2(15.0, 95.0),
Vector2(20.0, 95.0),
Vector2(25.0, 10.0)
]);
final subcurves = curve.offsetCurve(12.0);
}
We've made our best effort to conform to the recommendations outlined in the Effective Dart guide. Accordingly, this library is formatted using dartfmt.
As fervent believers in the value of clean code, we are constantly seeking to improve the library and make it easier to work with. Please alert us to any issues you notice, no matter how trivial. We wholeheartedly welcome criticism and friendly debate! :nerd_face:
To run the test cases from the terminal, run the following command from the bezier.dart root directory.
pub run test
Most IDEs now provide interfaces for running tests, which are generally easier to work with. In most cases you can simply right click on a test file or directory in the project tree view and select the menu option to run the selected tests.
Please file feature requests and bugs using the GitHub issues tab.