PixelChipCode

Trippy Text Effect GameMaker

Oct 20th, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.04 KB | Source Code | 0 0
  1. /*
  2. By Oliver @pixelchipcode
  3. This code creates a dynamic, colorful trailing text effect that follows the mouse pointer in GameMaker. The main text stays in the center of the mouse position, and the trailing shadows follow behind with a shrinking and color-cycling effect.
  4.  
  5. Features:
  6. - Shadow trail effect: Each instance of the text follows the mouse, progressively shrinking and cycling through a rainbow of colors.
  7. - Custom line height: Allows control over the spacing between lines of text for a multi-line setup.
  8. - Smooth movement: The trail follows smoothly behind the mouse, interpolating positions using a lerp function to ensure fluid motion.
  9. - Blending modes: The trail uses additive blending (bm_add) for vibrant color effects, while the main text is drawn using normal blending.
  10.  
  11. Main Components:
  12. - **shadow_colors**: An array defining the colors that cycle through the text trail.
  13. - **text**: The text to be displayed, including line breaks using '\n'.
  14. - **mouse_trail**: An array that stores the previous positions of the mouse to create the trailing effect.
  15. - **shrink_speed**: Controls how quickly the trailing text shrinks as it moves further back in the trail.
  16. - **line_height**: Allows control over the spacing between lines in multi-line text.
  17.  
  18. How it works:
  19. 1. The mouse's current position is tracked and stored in the `mouse_trail` array, which keeps a history of past positions.
  20. 2. The text trail is drawn at each past mouse position, shrinking progressively and cycling through the defined colors.
  21. 3. The main text is drawn at the current mouse position on top of the trail.
  22.  
  23. Customize:
  24. - Modify `trail_length` to adjust the number of trailing instances.
  25. - Adjust `shrink_speed` to control how quickly the trailing text shrinks.
  26. - Change `line_height` to fine-tune the spacing between lines of text.
  27. */
  28.  
  29. //create
  30.  
  31. // Initialize the shadow colors in the Create event
  32. shadow_colors = [
  33.     c_red,
  34.     c_orange,
  35.     c_yellow,
  36.     c_lime,
  37.     c_aqua,
  38.     c_blue,
  39.     c_purple
  40. ];
  41.  
  42. // Set the font
  43. draw_set_font(Font1);
  44. draw_set_halign(fa_center)
  45.  
  46. // Define the text
  47. text = "Nothing\nis real";
  48.  
  49.  
  50. // Time variable for animation
  51. time_offset = 0;
  52.  
  53. // Control the speed and distance of the effect
  54. shrink_speed = 0.96;  // Controls how much each text shrinks (closer to 1 = slower shrink)
  55. distance_offset = 8;  // Controls the distance between layers
  56.  
  57. // Mouse trail positions, store past mouse positions
  58. trail_length = 10;  // Number of past positions to store (length of the tail)
  59. mouse_trail = [];  // Initialize an empty array to store positions
  60.  
  61. // Initialize all positions to the current mouse position to start aligned
  62. for (var i = 0; i < trail_length; i++) {
  63.     array_push(mouse_trail, [mouse_x, mouse_y]);
  64. }
  65.  
  66. // Store the last position of the mouse for checking movement
  67. last_mouse_x = mouse_x;
  68. last_mouse_y = mouse_y;
  69.  
  70. //step
  71. // Add the current mouse position to the trail but keep positions smooth
  72. for (var i = trail_length - 1; i > 0; i--) {
  73.     // Smooth transition of each position towards the one in front of it
  74.     mouse_trail[i][0] = lerp(mouse_trail[i][0], mouse_trail[i - 1][0], 0.2);
  75.     mouse_trail[i][1] = lerp(mouse_trail[i][1], mouse_trail[i - 1][1], 0.2);
  76. }
  77.  
  78. // Set the first trail position to the current mouse position
  79. mouse_trail[0][0] = mouse_x;
  80. mouse_trail[0][1] = mouse_y;
  81.  
  82. // Increment the time_offset to animate the snake-like effect (for color cycling)
  83. time_offset += 0.05;
  84.  
  85. //draw
  86.  
  87.  
  88. gpu_set_blendmode(bm_add);
  89.  
  90. // Define the custom line height for the text
  91. line_height = 110;  // Adjust as needed
  92.  
  93. // Loop through the trail and draw text at each past position
  94. for (var i = 0; i < trail_length; i++) {
  95.     // Get the current color index but reverse the cycling so colors flow away from the main font
  96.     var color_index = (trail_length - i + time_offset * 10) mod array_length(shadow_colors);
  97.  
  98.     // Set the current color
  99.     draw_set_color(shadow_colors[color_index]);
  100.  
  101.     // Calculate scale for shrinking effect
  102.     var scale = power(shrink_speed, i);  // Shrinks progressively
  103.  
  104.     // Get the position from the trail
  105.     var x_pos = mouse_trail[i][0];
  106.     var y_pos = mouse_trail[i][1];
  107.  
  108.     // Split the text into lines using \n as the delimiter
  109.     var lines = string_split(text, "\n");
  110.  
  111.     // Loop through each line and draw it with custom line spacing
  112.     for (var j = 0; j < array_length(lines); j++) {
  113.         draw_text_transformed(
  114.             x_pos,
  115.             y_pos + (j * line_height),  // Adjust Y position for each line
  116.             lines[j],  // Draw each line separately
  117.             scale,
  118.             scale,
  119.             j // No rotation needed
  120.         );
  121.     }
  122. }
  123.  
  124. // Draw the main text on top (default color, purple as per your example)
  125. gpu_set_blendmode(bm_normal);
  126. draw_set_color(c_white);  
  127.  
  128. // Split the main text into lines for the final drawing
  129. var lines = string_split(text, "\n");
  130.  
  131. // Loop through the lines and draw them with custom spacing
  132. for (var j = 0; j < array_length(lines); j++) {
  133.     draw_text(mouse_x, mouse_y + (j * line_height), lines[j]);
  134. }
  135.  
Tags: gml gameMaker
Advertisement
Add Comment
Please, Sign In to add comment