-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnitMain.pas
More file actions
225 lines (196 loc) · 5.44 KB
/
UnitMain.pas
File metadata and controls
225 lines (196 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
unit UnitMain;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.Ani, FMX.Effects, FMX.Filter.Effects, System.UIConsts;
type
TAnimatedPoint = record
Position: TPointF;
Angle: Single;
constructor Create(X, Y, Angle: Single);
end;
TFormMain = class(TForm)
PaintBox: TPaintBox;
TimerAnimate: TTimer;
procedure PaintBoxPaint(Sender: TObject; Canvas: TCanvas);
procedure FormCreate(Sender: TObject);
procedure TimerAnimateTimer(Sender: TObject);
private
Points: TArray<TAnimatedPoint>;
Bitmap: TBitmap;
procedure Update();
end;
const
PointCount = 7;
AnimationSpeed = 2.0;
BorderWidth = 10.0;
IterationCount = 4;
DarkValue = 4;
PathOpacity = 0.6;
//PathColor = TAlphaColorRec.Lightcyan;//TAlphaColorRec.Bisque;
var
FormMain: TFormMain;
implementation
{$R *.fmx}
{$POINTERMATH ON}
{$RANGECHECKS OFF}
{$OVERFLOWCHECKS OFF}
procedure DarkBitmap(Bitmap: TBitmap; Value: Byte);
var
Data: TBitmapData;
PixelLine: PAlphaColor;
X, Y: Integer;
Color: TAlphaColorRec;
begin
Assert(Bitmap.Map(TMapAccess.ReadWrite, Data));
try
if Data.PixelFormat in [TPixelFormat.BGRA, TPixelFormat.RGBA] then
begin
for Y := 0 to Data.Height - 1 do
begin
PixelLine := Data.GetScanline(Y);
for X := 0 to Data.Width - 1 do
begin
// read pixel
Color.Color := PixelLine[X];
// A
Color.A := 255;
// R
if Color.R > Value then
Color.R := Color.R - Value
else
Color.R := 0;
// G
if Color.G > Value then
Color.G := Color.G - Value
else
Color.G := 0;
// B
if Color.B > Value then
Color.B := Color.B - Value
else
Color.B := 0;
// write pixel
PixelLine[X] := Color.Color;
end;
end;
end;
finally
Bitmap.Unmap(Data);
end;
end;
{$POINTERMATH OFF}
{$RANGECHECKS ON}
{$OVERFLOWCHECKS ON}
function ChaikinPoints(Points: TArray<TPointF>): TArray<TPointF>;
var
I: Integer;
begin
Result := [];
for I := 0 to High(Points) do
begin
Result := Result + [TPointF.Create(
(Points[I].X * 0.75 + Points[(I + 1) mod Length(Points)].X * 0.25),
(Points[I].Y * 0.75 + Points[(I + 1) mod Length(Points)].Y * 0.25)
)];
Result := Result + [TPointF.Create(
(Points[I].X * 0.25 + Points[(I + 1) mod Length(Points)].X * 0.75),
(Points[I].Y * 0.25 + Points[(I + 1) mod Length(Points)].Y * 0.75)
)];
end;
end;
procedure TFormMain.FormCreate(Sender: TObject);
var
I: Integer;
begin
Bitmap := TBitmap.Create(800, 600);
Points := [];
for I := 0 to PointCount - 1 do
begin
Points := Points + [
TAnimatedPoint.Create(Bitmap.Width * Random(), Bitmap.Height * Random(), 2 * Pi * Random())
];
end;
end;
procedure TFormMain.Update();
var
I: Integer;
PathPoints: TArray<TPointF>;
Path: TPathData;
begin
// *** update ***
// animate points
for I := 0 to High(Points) do
begin
Points[I].Position.X := Points[I].Position.X + Cos(Points[I].Angle) * AnimationSpeed;
Points[I].Position.Y := Points[I].Position.Y + Sin(Points[I].Angle) * AnimationSpeed;
end;
// fix out of bounds
for I := 0 to High(Points) do
begin
// left
if Points[I].Position.X < BorderWidth then
Points[I].Angle := -Pi / 2 + Random * Pi;
// right
if Points[I].Position.X > Bitmap.Width - BorderWidth then
Points[I].Angle := Pi / 2 + Random * Pi;
// top
if Points[I].Position.Y < BorderWidth then
Points[I].Angle := Random * Pi;
// bottom
if Points[I].Position.Y > Bitmap.Height - BorderWidth then
Points[I].Angle := Pi + Random * Pi;
end;
// *** make path from point ***
// copy points
PathPoints := [];
for I := 0 to High(Points) do
PathPoints := PathPoints + [Points[I].Position];
// make path
for I := 0 to IterationCount - 1 do
PathPoints := ChaikinPoints(PathPoints);
// *** draw to buffer ***
// darker background
DarkBitmap(Bitmap, DarkValue);
// draw
Bitmap.Canvas.BeginScene();
try
// make path
Path := TPathData.Create();
try
// add points to path
Path.MoveTo(PathPoints[0]);
for I := 1 to High(PathPoints) do
Path.LineTo(PathPoints[I]);
Path.ClosePath();
// draw
Bitmap.Canvas.Stroke.Kind := TBrushKind.Solid;
Bitmap.Canvas.Stroke.Color := HSLtoRGB((TThread.GetTickCount64() div 30 mod 360) / 360, 1.0, 0.8);// PathColor;
Bitmap.Canvas.Stroke.Join := TStrokeJoin.Round;
Bitmap.Canvas.Stroke.Thickness := 7;
Bitmap.Canvas.DrawPath(Path, PathOpacity);
finally
Path.Free();
end;
finally
Bitmap.Canvas.EndScene();
end;
end;
procedure TFormMain.PaintBoxPaint(Sender: TObject; Canvas: TCanvas);
begin
PaintBox.Canvas.DrawBitmap(Bitmap, Bitmap.BoundsF, Bitmap.BoundsF, 1);
end;
procedure TFormMain.TimerAnimateTimer(Sender: TObject);
begin
Update();
Invalidate();
end;
{ TAniPoint }
constructor TAnimatedPoint.Create(X, Y, Angle: Single);
begin
Self.Position.X := X;
Self.Position.Y := Y;
Self.Angle := Angle;
end;
end.