File size: 15,455 Bytes
4eb7346
 
 
 
fadd56c
f9752fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb7346
 
 
4fd8be6
4eb7346
 
4fd8be6
 
4eb7346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fd8be6
 
 
 
 
 
 
 
aa9287d
4fd8be6
 
db8a7b0
 
 
 
 
aa9287d
4eb7346
 
 
4fd8be6
4eb7346
4fd8be6
 
 
4eb7346
4fd8be6
 
 
 
aa9287d
4fd8be6
 
 
 
 
 
 
 
 
aa9287d
4fd8be6
 
aa9287d
4fd8be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa9287d
4fd8be6
aa9287d
4fd8be6
 
 
aa9287d
4fd8be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa9287d
4fd8be6
 
 
 
 
 
 
 
 
 
 
 
 
 
aa9287d
4fd8be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa9287d
4fd8be6
 
 
2608ee0
4fd8be6
 
 
4eb7346
81e52c9
4eb7346
 
 
4fd8be6
e233942
4eb7346
 
 
 
 
 
 
 
 
 
 
 
 
 
4fd8be6
4eb7346
e233942
4fd8be6
4eb7346
 
 
e233942
4fd8be6
81e52c9
4eb7346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a921fc6
4eb7346
 
 
 
 
 
 
 
 
 
4fd8be6
 
4eb7346
4fd8be6
4eb7346
 
4fd8be6
81e52c9
4fd8be6
 
 
 
 
 
 
 
 
81e52c9
4fd8be6
 
 
 
 
 
 
81e52c9
4fd8be6
 
 
 
 
 
 
 
 
 
 
 
 
4eb7346
 
 
 
aa9287d
4eb7346
 
 
4fd8be6
4eb7346
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import gradio as gr
import random

# Your predefined words list
SPECIAL_WORDS = [
    'luke', 'skywalker', 'vader', 'darth', 'leia', 'han', 'solo', 'chewbacca', 'obi-wan', 'kenobi',
    'yoda', 'anakin', 'padme', 'amidala', 'palpatine', 'emperor', 'mace', 'windu', 'qui-gon', 'jinn',
    'boba', 'fett', 'jango', 'rey', 'finn', 'poe', 'dameron', 'kylo', 'ren', 'snoke',
    'hux', 'phasma', 'ahsoka', 'tano', 'grievous', 'dooku', 'maul', 'jabba', 'hutt', 'lando',
    'calrissian', 'wedge', 'antilles', 'tarkin', 'thrawn', 'krennic', 'jyn', 'erso', 'cassian', 'andor',
    'jedi', 'sith', 'force', 'lightsaber', 'empire', 'rebellion', 'republic', 'resistance', 'first', 'order',
    'stormtrooper', 'clone', 'trooper', 'mandalorian', 'bounty', 'hunter', 'smuggler', 'pilot', 'princess', 'master',
    'apprentice', 'padawan', 'senator', 'chancellor', 'admiral', 'general', 'commander', 'captain', 'lord', 'knight',
    'millennium', 'falcon', 'x-wing', 'tie', 'fighter', 'star', 'destroyer', 'death', 'star', 'at-at',
    'at-st', 'speeder', 'bike', 'podracer', 'landspeeder', 'transport', 'cruiser', 'frigate', 'corvette', 'dreadnought',
    'tatooine', 'coruscant', 'endor', 'hoth', 'dagobah', 'naboo', 'alderaan', 'kamino', 'geonosis', 'mustafar',
    'yavin', 'jakku', 'takodana', 'starkiller', 'base', 'cantina', 'sarlacc', 'pit', 'cloud', 'city',
    'mos', 'eisley', 'jedi', 'temple', 'senate', 'cantina', 'carbon', 'freezing', 'hologram', 'droid',
    'r2-d2', 'c-3po', 'bb-8', 'protocol', 'astromech', 'battle', 'super', 'destroyer', 'medical',
    'blaster', 'thermal', 'detonator', 'holochess', 'hyperdrive', 'carbonite', 'kyber', 'crystal', 'holocron', 'meditation',
    'training', 'younglings', 'prophecy', 'chosen', 'one', 'balance', 'dark', 'side', 'light', 'council',
    'trials', 'vision', 'destiny', 'hope', 'rebellion', 'alliance', 'imperial', 'galactic', 'civil', 'war',
    'clone', 'wars', 'separatist', 'confederacy', 'trade', 'federation', 'blockade', 'invasion', 'phantom', 'menace'
]

# Global variables
original_designs = {}
selected_words = []

def generate_word_design(word, word_id, is_animated=False):
    """Generate styled design for a word."""
    fonts = [
        "'VT323', monospace",
        "'Josefin Sans', sans-serif",
        "'Rajdhani', sans-serif",
        "'Anton', sans-serif",
        "'Caveat', cursive",
        "'Patrick Hand', cursive",
        "'Nothing You Could Do', cursive",
        "'Reenie Beanie', cursive",
        "'Orbitron', sans-serif",
        "'Raleway', sans-serif",
        "'Open Sans Condensed', sans-serif",
        "'Indie Flower', cursive",
        "'Pacifico', cursive",
        "'Teko', sans-serif"
    ]
    
    if not is_animated:
        # Original state values
        font_sizes = ["18px", "19px", "20px"]
        font_tops = ["0px", "1px", "-1px"]
        letter_spacings = ["-1px", "0px", "1px", "2px"]
        text_shadows = ["0px 0px 1px #000", "0px 0px 2px #000", "1px 0px 0px #000", "0px 0px 0px #000"]
        skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
        word_color = "#000000"  # All letters in original word are black
    else:
        # Animated state values
        font_sizes = ["18px", "19px", "20px"]
        font_tops = ["0px", "1px", "-1px"]
        letter_spacings = ["-1px", "0px", "1px", "2px"]
        text_shadows = ["0px 0px 1px #000", "0px 0px 2px #000", "1px 0px 0px #000", "0px 0px 0px #000"]
        skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
        word_color = f"#{random.randint(0, 0xFFFFFF):06x}"  # ONE color for the entire word

    letters = list(word)
    styled_letters = []
    keyframes_css = ""
    
    if is_animated:
        # Get original styles to create transition keyframes
        original_data = original_designs.get(word_id, {})
        
    for i, letter in enumerate(letters):
        if not is_animated:
            # Store original values for later animation
            if word_id not in original_designs:
                original_designs[word_id] = {'letters': [], 'word_color': word_color}
            
            original_styles = {
                'font_family': random.choice(fonts),
                'font_size': random.choice(font_sizes),
                'letter_spacing': random.choice(letter_spacings),
                'text_shadow': random.choice(text_shadows),
                'skew_angle': random.choice(skew_angles),
                'margin_top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
                'top': random.choice(font_tops),
                'color': word_color  # Same color for all letters in this word
            }
            original_designs[word_id]['letters'].append(original_styles)
            original_designs[word_id]['word_color'] = word_color
            
            style = {
                'font-family': original_styles['font_family'],
                'line-height': '1.6',
                'font-size': original_styles['font_size'],
                'letter-spacing': original_styles['letter_spacing'],
                'text-shadow': original_styles['text_shadow'],
                'transform': f'skew({original_styles["skew_angle"]})',
                'margin-top': original_styles['margin_top'],
                'position': 'relative',
                'top': original_styles['top'],
                'color': original_styles['color'],
                'display': 'inline-block',
                'margin': '0 1px',
                'vertical-align': 'middle'
            }
        else:
            # Create animated version with keyframes
            original_letter_styles = original_data['letters'][i] if i < len(original_data.get('letters', [])) else {}
            original_word_color = original_data.get('word_color', '#000000')
            
            # New target values (but same color for all letters in this word)
            new_font_family = random.choice(fonts)
            new_font_size = random.choice(font_sizes)
            new_letter_spacing = random.choice(letter_spacings)
            new_text_shadow = f"{random.choice(text_shadows)} {word_color}"  # Use word_color for shadow
            new_skew_angle = random.choice(skew_angles)
            new_top = random.choice(font_tops)
            new_margin_top = random.choice(["-0.05cm", "0.00cm", "0.03cm", "0.05cm"])
            
            # Create unique animation name
            animation_name = f"move_{word_id}_{i}_{random.randint(1000, 9999)}"
            
            # Get original values for keyframes
            orig_font_size = original_letter_styles.get('font_size', '18px')
            orig_letter_spacing = original_letter_styles.get('letter_spacing', '0px')
            orig_color = original_letter_styles.get('color', '#000000')
            orig_text_shadow = original_letter_styles.get('text_shadow', '0px 0px 0px #000')
            orig_skew_angle = original_letter_styles.get('skew_angle', '0deg')
            orig_top = original_letter_styles.get('top', '0px')
            orig_margin_top = original_letter_styles.get('margin_top', '0.00cm')
            
            # Create keyframes that transition from original to new values
            keyframes_css += f"""
            @keyframes {animation_name} {{
                0% {{
                    font-size: {orig_font_size};
                    letter-spacing: {orig_letter_spacing};
                    color: {orig_color};
                    text-shadow: {orig_text_shadow};
                    transform: skew({orig_skew_angle}) scale(1);
                    top: {orig_top};
                    margin-top: {orig_margin_top};
                }}
                25% {{
                    font-size: {new_font_size};
                    letter-spacing: {new_letter_spacing};
                    color: {word_color};
                    text-shadow: {new_text_shadow};
                    transform: skew({new_skew_angle}) scale(1.2);
                    top: {new_top};
                    margin-top: {new_margin_top};
                }}
                50% {{
                    transform: skew({new_skew_angle}) scale(0.9);
                }}
                75% {{
                    transform: skew({new_skew_angle}) scale(1.1);
                }}
                100% {{
                    font-size: {new_font_size};
                    letter-spacing: {new_letter_spacing};
                    color: {word_color};
                    text-shadow: {new_text_shadow};
                    transform: skew({new_skew_angle}) scale(1);
                    top: {new_top};
                    margin-top: {new_margin_top};
                }}
            }}
            """
            
            style = {
                'font-family': new_font_family,  # Font changes instantly
                'line-height': '1.6',
                'font-size': new_font_size,
                'letter-spacing': new_letter_spacing,
                'text-shadow': new_text_shadow,
                'transform': f'skew({new_skew_angle})',
                'margin-top': new_margin_top,
                'position': 'relative',
                'top': new_top,
                'color': word_color,  # Same color for all letters in this word
                'display': 'inline-block',
                'margin': '0 1px',
                'vertical-align': 'middle',
                'animation': f'{animation_name} 2.5s ease-in-out forwards',
                'animation-delay': f'{i * 0.1}s'
            }

        style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
        styled_letter = f'<span class="letter-{word_id}-{i}" style="{style_str}">{letter}</span>'
        styled_letters.append(styled_letter)

    return f'''
    {f"<style>{keyframes_css}</style>" if keyframes_css else ""}
    <span class="word-container" id="word-{word_id}" style="display: inline-block; 
                                       margin: 10px; 
                                       padding: 8px 12px; 
                                       border: 2px solid #333; 
                                       border-radius: 8px;
                                       background-color: rgba(255,255,255,0.8);">
        <span style="display: inline-flex; 
                     align-items: baseline; 
                     vertical-align: middle;">
            {" ".join(styled_letters)}
        </span>
    </span>'''

def generate_random_words():
    """Generate 5 random words with initial styling."""
    global original_designs, selected_words
    
    # Reset data
    original_designs = {}
    selected_words = random.sample(SPECIAL_WORDS, 5)
    
    styled_words = []
    for i, word in enumerate(selected_words):
        word_design = generate_word_design(word, i, is_animated=False)
        styled_words.append(word_design)
    
    final_output = f"""
    <html>
    <head>
        <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@100&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Caveat&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Patrick+Hand&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Nothing+You+Could+Do&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Raleway:500" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Poiret+One&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Indie+Flower&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Teko&display=swap" rel="stylesheet">
        <style>
        body {{
            background-color: #f5f5f5;
            color: #000;
            font-size: 18px;
            line-height: 1.6;
            font-family: "Josefin Sans", sans-serif;
            padding: 20px;
        }}
        </style>
    </head>
    <body>
        <div style='max-width: 800px; margin: auto; text-align: center;'>
            <h2 style="margin-bottom: 30px;">Random Styled Words</h2>
            <div id="words-container" style="display: flex; flex-wrap: wrap; justify-content: center; align-items: center;">
                {" ".join(styled_words)}
            </div>
        </div>
    </body>
    </html>
    """
    
    return final_output

def trigger_movement(input_html):
    """Replace words with animated versions that transition from original to new styling."""
    global original_designs, selected_words
    
    if not original_designs or not selected_words:
        return input_html

    updated_html = input_html
    
    # Replace each word with its animated version
    for i, word in enumerate(selected_words):
        if i in original_designs:
            # Generate animated version that transitions from original values
            animated_design = generate_word_design(word, i, is_animated=True)
            
            # Find and replace the original word container
            old_pattern = f'<span class="word-container" id="word-{i}"'
            old_end = '</span></span>'
            
            # Find the full original word HTML
            start_idx = updated_html.find(old_pattern)
            if start_idx != -1:
                # Find the end of this word container
                temp_html = updated_html[start_idx:]
                span_count = 0
                end_idx = start_idx
                
                for j, char in enumerate(temp_html):
                    if temp_html[j:j+5] == '<span':
                        span_count += 1
                    elif temp_html[j:j+7] == '</span>':
                        span_count -= 1
                        if span_count == 0:
                            end_idx = start_idx + j + 7
                            break
                
                if end_idx > start_idx:
                    old_word_html = updated_html[start_idx:end_idx]
                    updated_html = updated_html.replace(old_word_html, animated_design, 1)

    return updated_html

# Create Gradio interface using Blocks
with gr.Blocks() as demo:
    gr.Markdown("# Random Word Styler\nEach word gets ONE color and transitions from original to new styling!")
    
    generate_button = gr.Button("Generate Random Words", variant="primary")
    output_html = gr.HTML()
    animate_button = gr.Button("Trigger Progressive Movement", variant="secondary")
    
    generate_button.click(generate_random_words, outputs=output_html)
    animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)

# Launch the app
demo.launch()