Recreating the separating texts in Forspoken
11/7/2024
Mihail Todorov
In Forspoken, dealing damage to enemies triggers a unique separating text effect that adds flair to combat feedback. This stylistic touch enhances the game’s dynamic feel. In this article, you’ll learn how to replicate this visually striking effect for your own game, bringing a touch of magic to your combat UI.
Overview
In this tutorial we’ll be showing you how to recreate the separating effect that happens on the damage texts in the game Forspoken .
To see the sample live you can find the whole project in the ${Gameface package}/Samples/uiresources/UITutorials/SeparatingTexts
directory.
Getting started
To get started we’ll create two files: index.html
and index.js
and link them together.
To make this example look more like a game, we’ll also include an image for the background and use the DCC ASH Font for the texts.
Styling our sample
First of we’ll begin by styling our components. We’ll start by adding the font and the image to the background in a style
tag in our index.html
:
Then we’ll style our words. Since we want each letter to move in a random direction, we’ll need to create a separate element for each one. This is why we’ll style both the word and the letters.
Each .separating-word
we’ll be positioned absolutely on the screen and it’s text will always be in uppercase.
As for each .moving-letter
here is where the magic will happen. We’ll use CSS Transforms
to create the animations and to do that each letter will need to have a basic styling. In our case that would be to have it’s initial position set, initial opacity and color.
Making the words appear and separate
Adding the words to our page
To add the words to our page we’ll go to our index.js
file and make a createSeparatingWord
function.
To this function we need to pass which is the word, what color it would be and where it should appear on the screen.
The first step would be to create our word element. For this we’ll create a new element, add the separating-word
class to it and set it’s position according to the x
and y
coordinates.
Then for each letter we’ll create a new element and set it inside our word element. As we want the letters on the left side to have a text shadow on their left and those on their right, we’ll get the middle of the word and the shadow offset as well. The shadow offset will ensure that the further away the letter is from the middle one, the bigger the shadow is.
Finally we append the word element to the body.
Animating the letters
Next we need to animate the letters and make them move and disappear.
To do that we can set the transform of each letter so it offsets slightly. Here again we’ll be using the middle of the word to make sure that letters to the right and left go in their corresponding direction.
And since we already have a function for creating the word, we can add the necessary styles in there.
As you can see, here we are wrapping our changes in 2 requestAnimationFrame
callbacks. The reason is that, if we change it immediately Gameface won’t have the time to paint the initial element and then transition to the next style, but it would rather jump to the end result immediately. And the reason that we need two callbacks is that due to the way Gameface works, it’s paint is always 1 frame behind.
Removing the letters
Finally we need to remove the letters after they have disappeared.
Since we need to actually wait for them to disappear, we need to add a transitionend
event to the word element. And as we already have the element in our function, let’s also do it there.
Linking it our game
To link it to our game, we simply need to do the following
Which will show our word when the game sends the damageWord
event.
Mocking it in our sample
But as our sample doesn’t have a game behind it yet, we would need to mock this on the frontend.
To do this, we’ll simply set a timer to show a random word at a random place on the screen and we’ll also add an event listener so we can show the random word on click.
In Conclusion
The separating text effect from Forspoken is more than just a visual treat—it’s a way to immerse players in the flow of combat. By mastering this technique, you can add an eye-catching and impactful element to your game that players will remember.