I'm trying to fill a curved rectangle, but it is overflowing from the curved part, have used paths to create this shape. Any suggestions for how to achieve this.
Or is there any way to create a rectangle with 4 offset values, whose one side is curved like this?
I have offset values of two straight line (x1,y1), (x2,y2), (x3,y3) and (x4,y4). Any references?
final path = Path();
path.moveTo(87.75, 10.06);
path.lineTo(263.25, 80.06);
path.lineTo(263.25, 359.0);
path.lineTo(87.75, 359.0);
canvasWrapper.drawPath(path, _barAreaPaint..color = Colors.red);
Try drawing an arc
Rect rect = Rect.fromLTWH(0.0, 0.0, size.width, size.height);
path.arcTo(
rect,
0.0,
pi / 2,//adjust radians accordingly
false);
}
Related
I am using canvas to draw circles in flutter, is there any easy way to draw circles filled with lines?
PS:I know I can calculate each line's points then drawing lines in circle. But is there any simple method that can be implemented without calculation?
This is what I want to achieve:
Finally, I found an easy and ingenious solution without calculation.
That is creating a linear gradient filled in the circle.
give two colors for the start and end of the gradient.
give transparent to the start color.
give the actual line color to the end color.
Paint paint = new Paint();
paint.color =Colors.red;
paint.style = PaintingStyle.fill;
paint.shader = ui.Gradient.linear(
start,
//the line interval is 20
Offset(start.dx + 20, start.dy),
[
//give transparent to start color, give actual line color to the end color
Colors. transparent,
Colors.red
],
//The interval [0.0,0.9] is transparent, and the interval [0.9,1.0] is red. So it looks like drawing red lines
[0.90, 1.0],
//just repeat the gradient
TileMode.repeated,
null);
canvas.drawCircle(
Offset(centerX, centerY),
radius,
paint);
I want to draw a line perpendicular to an arc at a particular point. I'm using path.arcTo to draw the arc. I have to say, I already managed to do something similar, by drawing the arc, and then increase the radius and tell flutter to draw another arc, but with an invalid sweepAngle, in that way flutter draws the perpendicular line I want. However, this at the beginning worked, but later on gave me some bugs. I'm sharing with you the initial code that I'm using. I'd like to know if there is a specific method or way to achieve what the first image shows, after drawing the arc:
Offset center = Offset(screenSize.width / 2, screenSize.height / 2);
double degreesToRadians(double degrees) => degrees * (math.pi / 180);
path.arcTo(
Rect.fromCircle(center: center, radius: 150),
degreesToRadians(180),
degreesToRadians(135),
true);
I'm working directly with degrees. Therefore the function degreesToRadians.
My current result is just this:
Thanks in advance.
So, what I did was to declare final outerRadiusPoint (the point where I wanted to take my line to go to) by establishing a sizeToIncrease for the radius, obtaining cosine of startingAngle and sine of startingAngle, adding center.dx and center.dy respectively and multiplying each by the size we want our new radius to be.
After that, we draw our arc with path.arcTo. Then we obtain the offset of the end of the arc with PathMetrics and PathMetric. With that we are now able to move the path to that position and start drawing again to the Offset of outerRadiusPoint.
final outerRadiusPoint = new Point(
(sizeForRadius + sizeToIncrease) * cos(degreesToRadians(startingAngle)) + center.dx,
(sizeForRadius + sizeToIncrease) * sin(degreesToRadians(startingAngle)) + center.dy);
path.arcTo(
Rect.fromCircle(center: center, radius: sizeForRadius),
degreesToRadians(initialPoint),
degreesToRadians(buttonArcLength),
true);
PathMetrics pms = path.computeMetrics();
PathMetric pm = pms.last;
Offset pmoffset = pm.getTangentForOffset(pm.length).position;
path.lineTo(outerRadiusPoint.x, outerRadiusPoint.y);
path.moveTo(pmoffset.dx, pmoffset.dy);
Please tell me, is it possible to locate list of widgets along the curve? Like on picture, Let's say teeth are widgets.
If you don't want to use Stack, then you might try out the CustomPaint widget and drawing in flutter with paths.
There's a tutorial to painting: https://medium.com/flutter-community/paths-in-flutter-a-visual-guide-6c906464dcd0
However, painting is a limited thing and if you have an actual widgets that need to be fully responsive and act as a widgets, you need to use Stack with Positioned. That's the only way to go.
Also, in this question about animating along the curve in flutter you can find some code examples that use both: painting and stack and even animate that. Possibly the most useful code from there is calculating the position of an object that you want to place along some curve:
Offset calculate(path, along) {
PathMetrics pathMetrics = path.computeMetrics();
PathMetric pathMetric = pathMetrics.elementAt(0);
along = pathMetric.length * along;
Tangent pos = pathMetric.getTangentForOffset(along);
return pos.position;
}
In above example you need to have some Path which is a representation of some curve. For example you can get one like this:
Path getPath(){
Size size = Size(300,300);
Path path = Path();
path.moveTo(0, size.height / 2);
path.quadraticBezierTo(size.width / 2, size.height, size.width, size.height / 2);
return path;
}
then you give this path and some along, which is number between 0 and 1, which represents beginning and ending of this curve and you give it to the calculate, which return and Offset, which you can use in your Positioned inside your Stack:
Positioned(
top: calculate(path, along).dy,
left: calculate(path, along).dx,
child: ...
When you only want some shapes to be drawn on a screen then you can use simply painting with CustomPaint widget, but this objects won't be widgets
I have two UIViews and I need to draw a rectangle (or) get the frame of the smallest rectangle outside both the UIViews. How can I get this?
You can use:
CGRect smallestRectangle = CGRectUnion(view1.frame, view2.frame);
According to the docs, this function
Returns the smallest rectangle that contains the two source rectangles.
here are some steps that should work to find the rectangle you want
find the left most origin.x... = new origin.x
find the top most origin.y... = new origin.y
find the largest (origin.x + size.width)... = new size.width
find the largest (origin.y + size.height)... = new size.height
Simply trying to rotate a rectangle around it's origin, or its upper left corner like so:
Am using the following:
panGestureRecognizer.view.transform = CGAffineTransformRotate(panGestureRecognizer.view.transform, (M_PI * angle) / 180);
But the rectangle is sort of rotating in a big loop. Is there some sort of translation I need to do to get this to work?
You just need to set the anchor point: https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/anchorPoint
panGestureRecognizer.view.layer.anchorPoint = CGPointMake(0.0, 0.0);
Further Reading: For more advanced stuff you could try some of the tips detailed here for matrix transformations: https://stackoverflow.com/a/8536553/563381