I would like to learn how to reproduce the famous sinus scroller text effect, but i can't make it to work.
Can someone explain the idea behind this ? I don't want the code (i want to find it myself), i just want to understand how to approach this.
I've created a table with a cell for each letter of my text, now i need to print and move each later to the left in a sinus pattern, but i can't figured out :
cls() table={1,2,3,4,5,6} x=127 function _update() end function _draw() for i=1,6 do print(table[i],127-i*10,cos(i*3/12)*20+60) end end |
With this i have a sinus with my text, but it's not moving in real time, and it's in reverse mode. At some point i need to erase each letter from their previous position ? How to erase only one printed letter ? Print the same letter at the same position with transparent color ? Maybe sprite are better for this because they are easiest to move ?
I can't explain this without some code, but this is just the formula:
cos(frq * x/128) * yscale + ky |
frq is the sinusoid frequency. yscale is the vertical span of the effect, ky is the vertical offset (64 = centered.)
You'll want to increment a frame counter (I use 't') during _update(), then you can do this:
x = i * 6 - t |
in the loop -- the 6 here affects the character spacing. You can get pretty crazy with this:
To handle erasing the characters, the common solution is just call cls() at the beginning of _draw() and re-render each frame from scratch each time.
You can also use simple formula to do basic sinus scroll:
y = baseline + (sin(x)*freq);
where baseline is where text would be drawn if it'd be flat ;). Then manipulate X to move text left/right.
My 2 cents version :
local text = "hello lexaloffle" local f = 0 function _load() end function _update() f+=5 end function _draw() cls() local y local c local x = 128/2 - (#text*4)/2 for c=1,#text do y = sin((x+f)/100) * 8 color(5) print(sub(text,c,c),x,(64-4)+y) y = sin((x+10+f)/100) * 8 color(7) print(sub(text,c,c),x,(64-4)+y) x = x+4 end end |
why do you divide the content of sin() by 128 or 100?
edit: got it, x goes from 0 to 128 on the screen and sin (x) repeat itself after 2pi, so you just tweak the number to get the oscillation you want.
[Please log in to post a comment]