-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.a.svgranddraw.d
171 lines (159 loc) · 5.81 KB
/
ai.a.svgranddraw.d
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
// I used the information from the web search results to help me write the code
import std.stdio;
import std.random;
import std.conv;
import std.string;
// A function that generates a random color in hexadecimal format
string randomColor() {
return "#" ~ toHexString(uniform(0, 16777215));
}
// A function that generates a random SVG element with a given tag and attributes
string randomSVGElement(string tag, string[string] attrs) {
string element = "<" ~ tag;
foreach (key, value; attrs) {
element ~= " " ~ key ~ "=\"" ~ value ~ "\"";
}
element ~= "/>";
return element;
}
// A function that generates a random SVG animation element with a given type and attributes
string randomSVGAnimationElement(string type, string[string] attrs) {
string element = "<animateTransform attributeName=\"transform\" type=\"" ~ type ~ "\"";
foreach (key, value; attrs) {
element ~= " " ~ key ~ "=\"" ~ value ~ "\"";
}
element ~= "/>";
return element;
}
// A function that generates a random SVG shape with a given tag and size
string randomSVGShape(string tag, int size) {
string shape = "";
string color = randomColor();
switch (tag) {
case "circle":
shape = randomSVGElement(tag, [
"cx": to!string(uniform(0, size)),
"cy": to!string(uniform(0, size)),
"r": to!string(uniform(0, size / 2)),
"fill": color
]);
break;
case "rect":
shape = randomSVGElement(tag, [
"x": to!string(uniform(0, size)),
"y": to!string(uniform(0, size)),
"width": to!string(uniform(0, size)),
"height": to!string(uniform(0, size)),
"fill": color
]);
break;
case "ellipse":
shape = randomSVGElement(tag, [
"cx": to!string(uniform(0, size)),
"cy": to!string(uniform(0, size)),
"rx": to!string(uniform(0, size / 2)),
"ry": to!string(uniform(0, size / 2)),
"fill": color
]);
break;
case "polygon":
int n = uniform(3, 10); // number of points
string points = "";
for (int i = 0; i < n; i++) {
points ~= to!string(uniform(0, size)) ~ "," ~ to!string(uniform(0, size)) ~ " ";
}
shape = randomSVGElement(tag, [
"points": points,
"fill": color
]);
break;
default:
break;
}
return shape;
}
// A function that generates a random SVG animation with a given type and duration
string randomSVGAnimation(string type, int duration) {
string animation = "";
switch (type) {
case "rotate":
animation = randomSVGAnimationElement(type, [
"from": to!string(uniform(0, 360)),
"to": to!string(uniform(0, 360)),
"dur": to!string(duration) ~ "s",
"repeatCount": "indefinite"
]);
break;
case "scale":
animation = randomSVGAnimationElement(type, [
"from": to!string(uniform(0.1, 2.0)),
"to": to!string(uniform(0.1, 2.0)),
"dur": to!string(duration) ~ "s",
"repeatCount": "indefinite"
]);
break;
case "translate":
animation = randomSVGAnimationElement(type, [
"from": to!string(uniform(-100, 100)) ~ " " ~ to!string(uniform(-100, 100)),
"to": to!string(uniform(-100, 100)) ~ " " ~ to!string(uniform(-100, 100)),
"dur": to!string(duration) ~ "s",
"repeatCount": "indefinite"
]);
break;
case "skewX":
animation = randomSVGAnimationElement(type, [
"from": to!string(uniform(-45, 45)),
"to": to!string(uniform(-45, 45)),
"dur": to!string(duration) ~ "s",
"repeatCount": "indefinite"
]);
break;
case "skewY":
animation = randomSVGAnimationElement(type, [
"from": to!string(uniform(-45, 45)),
"to": to!string(uniform(-45, 45)),
"dur": to!string(duration) ~ "s",
"repeatCount": "indefinite"
]);
break;
default:
break;
}
return animation;
}
// A function that generates a random SVG with a given size and number of shapes
string randomSVG(int size, int numShapes) {
string svg = "<svg width=\"" ~ to!string(size) ~ "\" height=\"" ~ to!string(size) ~ "\" xmlns=\"http://www.w3.org/2000/svg\">";
string[] tags = ["circle", "rect", "ellipse", "polygon"];
for (int i = 0; i < numShapes; i++) {
string tag = tags[uniform(0, tags.length)];
string shape = randomSVGShape(tag, size);
string animation = randomSVGAnimation(tag, uniform(1, 10));
svg ~= shape ~ animation;
}
svg ~= "</svg>";
return svg;
}
// A function that generates a random HTML with a given SVG
string randomHTML(string svg) {
string html = "<!DOCTYPE html><html><head><style>body { margin: 0; }</style></head><body>";
html ~= svg;
html ~= "</body></html>";
return html;
}
// A function that writes a string to a file
void writeToFile(string filename, string content) {
auto file = File(filename, "w");
file.write(content);
file.close();
}
void main() {
// Generate a random SVG with a size of 500 and 10 shapes
string svg = randomSVG(500, 10);
// Generate a random HTML with the SVG
string html = randomHTML(svg);
// Write the HTML to a file
writeToFile("randomSVG.html", html);
// Print a message
writeln("The program has generated a randomSVG.html file.");
}