no no , not like that, just i suppose to say that even an induvidual line should not be stright, it sohuld also be curve/bend in ripple effect , do slight change
import 'package:flutter/material.dart';
import 'dart:math';
class AmslerGridScreen extends StatefulWidget {
const AmslerGridScreen({super.key});
class _AmslerGridScreenState extends State {
// Variables for transformations
double _scale = 1.0;
late TransformationController _transformationController;
TapDownDetails? _doubleTapDetails;
// Variables for adjustable settings
int _gridLines = 20;
double _lineThickness = 1.0;
Color _gridColor = Colors.black;
Color _backgroundColor = Colors.white;
bool _showCenterDot = true;
// For contrast adjustment
double _contrast = 1.0;
// Reset other variables to default values
_gridLines = 20;
_lineThickness = 1.0;
_gridColor = Colors.black;
_backgroundColor = Colors.white;
_showCenterDot = true;
_contrast = 1.0;
_distortionAmount = 50.0; // default to 50 (no distortion)
_distortion2Amount = 50.0; // default to 50 (no ripple)
_darken = 1.0;
});
}
void _handleDoubleTap() {
if (_doubleTapDetails != null) {
final position = _doubleTapDetails!.localPosition;
// Zoom in or out on double tap
setState(() {
if (_scale == 1.0) {
_scale = 2.0;
} else {
_scale = 1.0;
}
_onScaleChanged(_scale);
});
}
}
class BackgroundPaint extends CustomPainter {
final double marginWidth;
final double marginHeight;
final int gridLines;
final double lineThickness;
final Color gridColor;
final Color backgroundColor;
final bool showCenterDot;
final double contrast;
final double distortionAmount;
final double distortion2Amount;
final double darken;
Certainly! I understand that you want each individual grid line to be curved or bent, creating a ripple effect across the grid, rather than just shifting straight lines. Currently, the grid lines are drawn using canvas.drawPoints with PointMode.polygon, which connects the points with straight lines. To achieve curved lines, we'll need to draw smooth curves through the distorted points.
Here's how you can modify your code to make the individual grid lines curved:
Use Paths to Draw Curved Lines:
Instead of drawing points or connecting them with straight lines, we'll create Path objects for each line (both horizontal and vertical). We'll then interpolate between the distorted points using Bezier curves to create smooth curves.
Implement a Smooth Interpolation Function:
To draw smooth curves through a set of points, we can use Catmull-Rom splines or a similar interpolation method. This allows us to calculate control points for Bezier curves that smoothly pass through all the distorted points.
Modify the Drawing Loop:
We'll update the loops that draw the vertical and horizontal lines to use the new interpolation method and draw paths instead of points.
Here's the modified BackgroundPaint class with these changes:
class BackgroundPaint extends CustomPainter {
// ... (your existing variables)
// Add this function to calculate Catmull-Rom spline control points
List<Offset> _getSplinePoints(List<Offset> points) {
// This function calculates intermediate points for a Catmull-Rom spline
List<Offset> splinePoints = [];
for (int i = 0; i < points.length - 1; i++) {
Offset p0 = i > 0 ? points[i - 1] : points[i];
Offset p1 = points[i];
Offset p2 = points[i + 1];
Offset p3 = i < points.length - 2 ? points[i + 2] : points[i + 1];
for (double t = 0; t < 1; t += 0.1) {
double tt = t * t;
double ttt = tt * t;
double q0 = -ttt + 2 * tt - t;
double q1 = 3 * ttt - 5 * tt + 2;
double q2 = -3 * ttt + 4 * tt + t;
double q3 = ttt - tt;
double x = 0.5 * (p0.dx * q0 + p1.dx * q1 + p2.dx * q2 + p3.dx * q3);
double y = 0.5 * (p0.dy * q0 + p1.dy * q1 + p2.dy * q2 + p3.dy * q3);
splinePoints.add(Offset(x, y));
}
}
return splinePoints;
}
@override
void paint(Canvas canvas, Size size) {
// ... (your existing code before drawing lines)
// Draw the distorted vertical lines with curved paths
for (int i = 0; i <= gridLines; i++) {
List<Offset> points = [];
for (int j = 0; j <= gridLines; j++) {
final point = Offset(i * gridSize, j * gridSize);
final distortedPoint = distortPoint(point);
points.add(distortedPoint);
}
List<Offset> splinePoints = _getSplinePoints(points);
Path path = Path();
path.moveTo(splinePoints.first.dx, splinePoints.first.dy);
for (var pt in splinePoints) {
path.lineTo(pt.dx, pt.dy);
}
canvas.drawPath(path, paint);
}
// Draw the distorted horizontal lines with curved paths
for (int j = 0; j <= gridLines; j++) {
List<Offset> points = [];
for (int i = 0; i <= gridLines; i++) {
final point = Offset(i * gridSize, j * gridSize);
final distortedPoint = distortPoint(point);
points.add(distortedPoint);
}
List<Offset> splinePoints = _getSplinePoints(points);
Path path = Path();
path.moveTo(splinePoints.first.dx, splinePoints.first.dy);
for (var pt in splinePoints) {
path.lineTo(pt.dx, pt.dy);
}
canvas.drawPath(path, paint);
}
// ... (your existing code after drawing lines)
}
// ... (rest of your code)
}
Explanation of the Changes:
_getSplinePoints Function:
This function calculates intermediate points using a Catmull-Rom spline, which is a type of spline that passes through all the control points.
It generates a set of points (splinePoints) that can be used to draw a smooth path.
Drawing the Vertical and Horizontal Lines:
For each line, we collect the distorted points and then generate the spline points using _getSplinePoints.
We create a Path, move to the first spline point, and then draw lines to each subsequent point in splinePoints.
Finally, we draw the path using canvas.drawPath.
Result:
The grid lines are now smooth curves that follow the distortion, creating a ripple effect where each individual line is curved.
Adjusting the Smoothness:
The parameter t += 0.1 in the for-loop inside _getSplinePoints controls the granularity of the spline. Smaller values result in more points and smoother curves but may impact performance. You can adjust it as needed.
Example of Adjusted Distortion Functions:
You mentioned wanting a ripple effect. To enhance or modify the ripple effect, you can adjust the rippleFrequency and rippleStrength variables in your distortPoint function. Here's how you might modify that part:
double rippleFrequency = 0.2; // Increased frequency for more ripples
double ripple = rippleStrength * sin(theta * rippleFrequency);
By using theta (the angle) instead of r, you can create concentric ripple patterns or other interesting effects.
Additional Tips:
Performance Considerations:
Drawing many curved paths can be computationally intensive. If you notice performance issues, consider optimizing the _getSplinePoints function or reducing the number of grid lines.
Experiment with Different Interpolation Methods:
You can try other spline interpolation methods or adjust the existing one to achieve the desired visual effect.
Visual Tweaks:
Adjust the distortion parameters and the interpolation to fine-tune the appearance of the grid to match your requirements.
Alternate Option Using Flutter's built-in Methods:
If you want to simplify the spline calculation, you can use Path.cubicTo with calculated control points. Here's an example of how you might do it:
List<Offset> controlPoints(List<Offset> points) {
List<Offset> controls = [];
for (int i = 1; i < points.length - 1; i++) {
Offset prev = points[i - 1];
Offset curr = points[i];
Offset next = points[i + 1];
double dx = (next.dx - prev.dx) / 2;
double dy = (next.dy - prev.dy) / 2;
controls.add(Offset(curr.dx + dx / 3, curr.dy + dy / 3));
controls.add(Offset(curr.dx - dx / 3, curr.dy - dy / 3));
}
return controls;
}
void drawSmoothLine(Canvas canvas, List<Offset> points) {
if (points.length < 2) {
return;
}
Path path = Path();
path.moveTo(points[0].dx, points[0].dy);
List<Offset> controls = controlPoints(points);
for (int i = 1; i < points.length - 1; i++) {
path.cubicTo(
controls[2 * (i - 1)].dx,
controls[2 * (i - 1)].dy,
controls[2 * (i - 1) + 1].dx,
controls[2 * (i - 1) + 1].dy,
points[i].dx,
points[i].dy,
);
}
// Draw line to the last point
path.lineTo(points.last.dx, points.last.dy);
canvas.drawPath(path, paint);
}
// Then, in your drawing loops:
drawSmoothLine(canvas, points);
This method involves calculating control points for cubic Bezier curves between your distorted points.
Conclusion:
By adjusting your drawing method to use paths and smooth interpolation, you can create grid lines that are curved according to your distortion functions, achieving the ripple effect you're looking for. Feel free to adjust the interpolation and distortion parameters to fine-tune the effect.
Let me know if you have any questions or need further assistance!