Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Here is a Typing Effect in javascript.

I could only implement a very simple one:

DynamicModule[{s = ""},
 Dynamic[Refresh[
   If[Length[s] > 10, s = "", s = s <> "x"],
   TrackedSymbols :> {}, UpdateInterval -> 0.5]]]

How can I do that exactly the same?

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

Perhaps

typingEffect[str_String, secPerLetter_: 0.3] := 
 Dynamic[StringTake[str, 
   Clock[{0, StringLength@str, 1}, StringLength@str secPerLetter]]]

(thanks @SjoerdC.deVries)

So this gives an animation

typingEffect["hello my dear"]

Other effects can be implemented with the same idea, such as

typingEffect2[str_String] := 
 Row@MapIndexed[
   Style[#, 
      FontColor->Dynamic@ColorData["Rainbow"][
       Mod[(First@#2 - 0.5)/StringLength@str + Clock[], 1]]] &, 
   Characters@str]
share|improve this answer
    
as would my 3 years old son say: "but why?" ;-) –  chris Nov 19 '12 at 17:48
    
@chris, I wouldn't know exactly what to explain to your 3 year old son :P. What part does he not understand? –  Rojo Nov 19 '12 at 17:50
    
well that's the thing: it doesn't matter what I explain to him... He always comes up with that question! –  chris Nov 19 '12 at 17:51
    
@chris, don't worry, he'll soon grow up like all of us and forget he never got his answer –  Rojo Nov 19 '12 at 17:52
    
@Rojo, typing StringLength@str/.slen_:> and then slen twice takes just as many characters as simply typing StringLength@str twice, but is perhaps more confusing. –  VF1 Nov 19 '12 at 18:22
show 6 more comments

Implementation with ScheduledTask. When the Type button is pushed, a scheduled task is started that increases the display length of the temporary string temp. It can be paused (Stop) and continued (Type again).

text = StringTake[ExampleData[{"Text", "LoremIpsum"}], 120]

temp = ""; task = None; n = 0;
Row@{Button["Type", If[task =!= None, RemoveScheduledTask@task]; 
   task = RunScheduledTask[
     If[n > StringLength@text, RemoveScheduledTask@task; n = 0, 
      temp = StringTake[text, n++]], 0.01]],
  Button["Stop", If[task =!= None, RemoveScheduledTask@task]]}
Dynamic@temp

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.