Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class WaveClipper extends CustomClipper<Path> {
final double percentage;
final double animation;
WaveClipper(this.percentage, this.animation);
@override
Path getClip(Size size) {
var progress = (percentage > 100.0 ? 100.0 : percentage) / 100.0;
progress = 1.0 - progress;
final double wavesHeight = size.height * 0.1;
var path = new Path();
if (progress == 1.0) {
return path;
} else if (progress == 0.0) {
path.lineTo(0.0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, 0.0);
path.lineTo(0.0, 0.0);
return path;
}
List<Offset> wavePoints = [];
for (int i = -2; i <= size.width.toInt() + 2; i++) {
var extraHeight = wavesHeight * 0.5;
extraHeight *= i / (size.width / 2 - size.width);
var dx = i.toDouble();
var dy = sin((animation * 360 - i) % 360 * Vector.degrees2Radians) * 5 +
progress * size.height -
extraHeight;
if (!dx.isNaN && !dy.isNaN) {
wavePoints.add(Offset(dx, dy));
}
}
path.addPolygon(wavePoints, false);
// finish the line
path.lineTo(size.width, size.height);
path.lineTo(0.0, size.height);
return path;
}
@override
bool shouldReclip(WaveClipper old) =>
percentage != old.percentage || animation != old.animation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.