This is first image which i want to create
This is the second image which i achieved using that code
**I have to create an animation like line moving from left to right in zigzag motion with round curves .This custom-paint code generates these lines but these are straight zig-zag lines like , i want to make these lines with some round curve in them how can i do that
I have to create an animation like line moving from left to right in zigzag motion with round curves .This custom-paint code generates these lines but these are straight zig-zag lines like , i want to make these lines with some round curve in them how can i do that
I have to create an animation like line moving from left to right in zigzag motion with round curves .**
class DigitalWavePainter extends CustomPainter {
final double animationValue;
final double widthOfCanvas;
final double heightOfCanvas;
DigitalWavePainter(
this.animationValue, this.widthOfCanvas, this.heightOfCanvas);
@override
void paint(Canvas canvas, Size size) {
final width = widthOfCanvas;
final height = heightOfCanvas;
final path = Path();
final path2=Path();
final numSegments = 200; // Increase the number of line segments
final segmentWidth = width / numSegments;
final amplitude = height + 10;
final upperBoundary = height * 0.01;
final lowerBoundary = height; // Adjust the lower boundary value
final waveProgress = animationValue * 2;
final waveProgressCircle = animationValue;
double dotY = 0;
double dotX = -50;
path.moveTo(width, (math.sin(0) * amplitude) + height / 2); // Start at the initial point of the wave
path2.moveTo(-50, (math.sin(0) * amplitude) + height / 2);
for (int i = 1; i <= numSegments; i++) {
final x = i * segmentWidth ;
final animationTime = (x / width * math.pi) + waveProgress * math.pi;
final y = (math.sin(animationTime) * amplitude) + height / 12;
dotY = y;
if (y < upperBoundary) {
final newY = upperBoundary;
dotY = newY;
dotX = x;
path2.lineTo(
x,newY
);
} else if (y > lowerBoundary) {
final newY = lowerBoundary;
dotY = newY;
dotX = x;
path2.quadraticBezierTo(x - segmentWidth, newY, x, newY);
print("lower boundary");
} else {
dotX = x;
dotY = y;
path2.lineTo(x, y);
}
}
for (int i = 1; i <= numSegments / 2; i++) {
final x = width - i * segmentWidth;
final animationTime = (x / width * math.pi) + waveProgress * math.pi;
final y = (math.sin(animationTime) * amplitude) + height / 12;
dotY = (math.sin(animationTime) * amplitude) + height / 12;
if (y < upperBoundary) {
final newY = upperBoundary;
dotY = newY ;
path.quadraticBezierTo(x + segmentWidth, newY, x, newY);
} else if (y > lowerBoundary) {
final newY = lowerBoundary;
dotY = newY;
path.quadraticBezierTo(x + segmentWidth, newY, x, newY);
} else {
dotY = y;
path.lineTo(x, y);
}
}
const dotRadius = 20.0; // Set the radius of the dot
final paint = Paint()
..color = Colors.blue
..strokeWidth = 16.0
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
final circlePaint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
final boundaryPaint = Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = 5.0;
canvas.drawPath(path, paint);
canvas.drawPath(path2,paint);
// Draw the dot as a filled circle at the current position
canvas.drawCircle(
Offset(width / 2, dotY + animationValue),
dotRadius,
circlePaint,
);
// Draw the boundary circle
canvas.drawCircle(Offset(width / 2, dotY), dotRadius, boundaryPaint);
}
@override
bool shouldRepaint(DigitalWavePainter oldDelegate) {
return oldDelegate.animationValue != animationValue;
}
}
2
Answers