Godot:CanvasItem
lastmod 2023-04-25created 2022-05-1910 edits
Base class of anything 2D.
GDScript Methods
| void | |
| void | |
| float | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| void | |
| RID | |
| RID | |
| Transform2D | |
| | |
| Transform2D | |
| Transform2D | |
| | |
| Transform2D | |
| Rect2 | |
| Transform2D | |
| World2D | |
| void | |
| bool | |
| bool | |
| bool | |
| bool | |
| | |
| | |
| void | |
| void | |
| void | |
| void | |
| void | |
draw_string
How to calculate the area of the text to be drawn
Use the Godot:Font#get_string_size function.
draw_polyline
| WARNING |
| Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on, then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing. |
Line Connection issue
According to the documentation the lines are connected, but they aren't... which causes a Line Connection issue.
A hexagon is drawn as follows.
float COS30 = (float)Math.Sqrt(3f) / 2f;
float SIN30 = 0.5f;
var r = radius;
var x = COS30 * radius;
var y = SIN30 * radius;
var cx = center.x;
var cy = center.y;
var points = new List<Vector2>();
points.Add(new Vector2(cx + 0, cy - r)); // 12시
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
points.Add(new Vector2(cx + x, cy + y)); // 4시30분
points.Add(new Vector2(cx + 0, cy + r)); // 6시
points.Add(new Vector2(cx - x, cy + y)); // 7시30분
points.Add(new Vector2(cx - x, cy - y)); // 10시30분
points.Add(new Vector2(cx + 0, cy - r)); // 12시
If the line width is greater than 1, the connected lines look unnatural, as in the following image. (You can see a notch where the lines meet at the 12 o'clock vertex.)
Godot_draw_polyline_lineWidth_issue_01.png
So if you add an extra line from the 12 o'clock vertex on to the 1:30 vertex,
points.Add(new Vector2(cx + 0, cy - r)); // 12시
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
points.Add(new Vector2(cx + x, cy + y)); // 4시30분
points.Add(new Vector2(cx + 0, cy + r)); // 6시
points.Add(new Vector2(cx - x, cy + y)); // 7시30분
points.Add(new Vector2(cx - x, cy - y)); // 10시30분
points.Add(new Vector2(cx + 0, cy - r)); // 12시
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
it looks like this:
Godot_draw_polyline_lineWidth_issue_02.png
At a glance it seems to be drawn well, but if you zoom in you can see something odd.
Godot_draw_polyline_lineWidth_issue_03.png
The same thing happens even if you start drawing from the 1:30 vertex.
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
points.Add(new Vector2(cx + x, cy + y)); // 4시30분
points.Add(new Vector2(cx + 0, cy + r)); // 6시
points.Add(new Vector2(cx - x, cy + y)); // 7시30분
points.Add(new Vector2(cx - x, cy - y)); // 10시30분
points.Add(new Vector2(cx + 0, cy - r)); // 12시
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
Godot_draw_polyline_lineWidth_issue_04.png
This seems to be an issue caused by representing line widths greater than 1 as connected "rectangle shapes".
Godot_draw_polyline_lineWidth_issue_06.png
So draw it by adding a point on a straight segment where the width does not change, i.e. at the 3 o'clock position:
Godot_draw_polyline_lineWidth_issue_05.png
var points = new List<Vector2>();
// points.Add(new Vector2(cx + 0, cy - r)); // 12시
// points.Add(new Vector2(cx + x, cy - y)); // 1시30분
points.Add(new Vector2(cx + x, cy)); // 3시
points.Add(new Vector2(cx + x, cy + y)); // 4시30분
points.Add(new Vector2(cx + 0, cy + r)); // 6시
points.Add(new Vector2(cx - x, cy + y)); // 7시30분
points.Add(new Vector2(cx - x, cy - y)); // 10시30분
points.Add(new Vector2(cx + 0, cy - r)); // 12시
points.Add(new Vector2(cx + x, cy - y)); // 1시30분
points.Add(new Vector2(cx + x, cy)); // 3시
See also
- Godot
- Godot:CustomDrawing
- Hexagon#Godot - Explains how to draw a cube with Godot's custom drawing.