Scroll text horizontally

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Scroll text horizontally

Post by collectordave »

I have been looking for a way to scroll text horizontally in something like a textgadget for some time.

I tried taking first character and putting at the end then setting the text but this seemed very jerky.

Remembered how scrolling a canvas in a container seemed ok so tried using a canvas in a container.

Needed two canvas gadgets to get a continuous scroll and threw this together.

My intention is to run the scrolling in a thread (high hopes).

It could be improved sorry for my scrappy code. If anyone can help with improvements please do.

Code: Select all

Global Container_0,Canvas_1,Canvas_2
Global ScrollFont.i


Procedure ScrollText(Text.s)

  Static iLoop.i
  
  Static C1Start.i,C2Start.i
  
  Define MyTextWidth.i

      If StartVectorDrawing(CanvasVectorOutput(Canvas_1))      

       VectorSourceColor(RGBA(236,236,236,255))
       
       AddPathBox(0,0,400,200)
       
       FillPath()      

      VectorFont(FontID(ScrollFont), 20)
      VectorSourceColor(RGBA(0, 0, 0, 255))     
      
        MyTextWidth = VectorTextWidth(Text)  

        MovePathCursor(0,0)
      
        DrawVectorText(Text) 
        
        StopVectorDrawing()

      EndIf
      
        ResizeGadget(Container_0,#PB_Ignore,#PB_Ignore,MyTextWidth,#PB_Ignore)
      
        ResizeGadget(Canvas_1,0,#PB_Ignore,MyTextWidth,#PB_Ignore)
        ResizeGadget(Canvas_2,MyTextWidth,#PB_Ignore,MyTextWidth,#PB_Ignore)           
        While WindowEvent():Wend     
      
     If StartVectorDrawing(CanvasVectorOutput(Canvas_2))      
        
       VectorSourceColor(RGBA(236,236,236,255))
       
       AddPathBox(0,0,400,200)
       
       FillPath()      

      VectorFont(FontID(ScrollFont), 20)
      VectorSourceColor(RGBA(0, 0, 0, 255))     
    
        MovePathCursor(0,0)
      
        DrawVectorText(Text) 
        
        StopVectorDrawing()
        
      EndIf   

  C1Start = MyTextWidth
  C2Start = MyTextWidth * 2

  iLoop = 0
  
  While iLoop < 5 * MyTextWidth

  ;Move The Canvas Gadgets  
  ResizeGadget(Canvas_1,C1Start,#PB_Ignore,#PB_Ignore,#PB_Ignore)
  ResizeGadget(Canvas_2,C2Start,#PB_Ignore,#PB_Ignore,#PB_Ignore)  
  iLoop = iLoop + 1

  ;Reduce Each Start Point By 1
  C1Start = C1Start -1
  C2Start = C2Start -1
  
  ;If Canvas 1 Off Display 
  If C1Start < 0 - MyTextWidth
    C1Start = C2Start + MyTextWidth
  EndIf
  
  ;If Canvas 2 Off Display 
  If C2Start < 0 - MyTextWidth
    C2Start = C1Start + MyTextWidth
  EndIf  

  
  While WindowEvent():Wend
  Delay(10)

Wend

EndProcedure

ScrollFont = LoadFont(#PB_Any, "Comic Sans MS", 20)

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  Container_0 = ContainerGadget(#PB_Any, 10, 60, 400, 20)
  Canvas_1 = CanvasGadget(#PB_Any, 0, 0, 460, 20)
  Canvas_2 = CanvasGadget(#PB_Any, 460, 0, 460, 20)
  CloseGadgetList()

  
 ScrollText("The quick brown fox jumps over the lazy dog     ")

    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf
Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Scroll text horizontally

Post by Mijikai »

Never use delay in the windows event loop.
Have one dedicated event handler.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Scroll text horizontally

Post by collectordave »

Hi

The delay() is not in the event loop but in the procedure.

It controls the speed of the scroll.

When I move the scrolling to a thread it will be replaced by a non blocking timer.

The code so far is just a proof of concept.

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Scroll text horizontally

Post by RASHAD »

The next snippet for Windows and you can use Mac version by wilbert-mk-soft
It's very simple but accurate at the same time

Code: Select all

Global scroll$,count
scroll$ = "....Scrolling Text...."
count = Len(scroll$)

Procedure Update()
  scroll$=Right(scroll$,count-1)+Left(scroll$,1)
  SetGadgetText(1,scroll$)
 EndProcedure
 
LoadFont(0,"Tahoma",24)
 OpenWindow(0,5,5,400,200,"Test Window",#PB_Window_ScreenCentered)
 StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    w = TextWidth(scroll$)
 StopDrawing()
 TextGadget(1,50,0,350,45,scroll$)
 SetGadgetColor(1,#PB_Gadget_BackColor,$BCFEFC)
 SetGadgetColor(1,#PB_Gadget_FrontColor,$0000FF)
 SetGadgetFont(1,FontID(0))
 ResizeGadget(1,WindowWidth(0)/2-w/2,0,w,40)

 
 AddWindowTimer(0,100,100)
 BindEvent(#PB_Event_Timer,@Update())
 
 Repeat
     Event = WaitWindowEvent()    
  

   Until Event = #PB_Event_CloseWindow
Egypt my love
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Scroll text horizontally

Post by collectordave »

Hi Rashad,

That is very similar to what I tried at first seems a little jerky plus it does not wrap around the text.

Nice to see it gathered together. Next time I search looking for Scroll Text both examples will be here.

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Scroll text horizontally

Post by Saki »

That is done quite differently in itself.

I have quickly made a small module, which solves the problem very simply and better.

viewtopic.php?f=12&t=77044&p=568444#p568444
地球上の平和
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Scroll text horizontally

Post by RASHAD »

For Windows
# 1:

Code: Select all

Global x
Import "kernel32.lib"
  CreateTimerQueue()
  DeleteTimerQueueTimer(hTimerQueue, hTimer, completionEvent)
  DeleteTimerQueueEx(hTimerQueue, completionEvent)
  ChangeTimerQueueTimer (hTimerQueue, hTimer, dueTime, period)
  CreateTimerQueueTimer(hTimer, hTimerQueue, timerCallback,param, dueTime, period, flags)
EndImport

Procedure scrollText(param, timer)
  x -1
  If x = -400
    x = 400
  EndIf 
  ResizeGadget(1,x,#PB_Ignore,#PB_Ignore,#PB_Ignore)
EndProcedure

OpenWindow(0, 0, 0, 400, 100, "Scroller", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0,0)
LoadFont(0, "Georgia"  ,  32)
ImageGadget(1,0,0,400,100,0)
CreateImage(0,400,100)
StartDrawing(ImageOutput(0))
DrawingFont(FontID(0))
DrawText(10,15,"....Scrolling Text....")
StopDrawing()
SetGadgetState(1,ImageID(0))

timerQueue = CreateTimerQueue()
CreateTimerQueueTimer(@timer , timerQueue, @scrollText(), 1, 0, 10, 0)

Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      DeleteTimerQueueEx(timerQueue, 0)
      Quit = 1
  EndSelect
Until Quit = 1
# 2:

Code: Select all

Global x

Procedure scrollText(param, timer)
  x -1
  If x = -400
    x = 400
  EndIf 
  ResizeGadget(1,x,#PB_Ignore,#PB_Ignore,#PB_Ignore)
EndProcedure

OpenWindow(0, 0, 0, 400, 100, "Scroller", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0,0)
LoadFont(0, "Georgia"  ,  32)
ImageGadget(1,0,0,400,100,0)
CreateImage(0,400,100)
StartDrawing(ImageOutput(0))
DrawingFont(FontID(0))
DrawText(10,15,"....Scrolling Text....")
StopDrawing()
SetGadgetState(1,ImageID(0))

cycle = 20
TEvent = timeSetEvent_(cycle,0,@ScrollText(),0,#TIME_PERIODIC)

Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      timeKillEvent_(TEvent)
      Quit = 1
  EndSelect
Until Quit = 1

Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Scroll text horizontally

Post by IdeasVacuum »

.... Wouldn't it be a easier to use a WebGadget and an off-the-shelf javascript text scroller?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Scroll text horizontally

Post by Saki »

IdeasVacuum wrote:.... Wouldn't it be a easier to use a WebGadget and an off-the-shelf javascript text scroller?
No, because the BF Tools are simply better, simpler and more flexible. :wink:

This is a gif, it laughs normally soft like butter.
Image

There are countless simple ways to create a cool output.
Last edited by Saki on Sat Apr 10, 2021 12:33 am, edited 1 time in total.
地球上の平和
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Scroll text horizontally

Post by JHPJHP »

Hi IdeasVacuum,
IdeasVacuum wrote:... Wouldn't it be a easier to use a WebGadget and an off-the-shelf javascript text scroller?
Yes, of course. But, I did a quick test with a pure CSS example, and while it ran smooth from MS Edge, it was stuttering from IE like all the previous PureBasic examples; see Windows Services & Other Stuff\Other_Stuff\GadgetStuff\WebGadget\MarqueeText...
Saki/walbus wrote:No, because the BF Tools are simply better, simpler and more flexible. :wink:
I hope you were being ironic...

JavaScript Developers
In Q3 2020, according to Developer Economics, the number of software developers using JavaScript was 12.4 million. This means that 53% of all developers in the world were using JS at some point.
JavaScript Popularity
Programming language popularity: JavaScript leads – 5 million new developers since 2017

JavaScript remains by far the most popular programming language with developers
Last edited by JHPJHP on Sun Apr 18, 2021 5:40 am, edited 8 times in total.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Scroll text horizontally

Post by Saki »

@JHPJHP
In Germany they say: "Don't look a gift horse in the mouth". :wink:

Make simple a better Scroll tool as mine with JavaScript and post it here.
Or a better text tool as the FontMaster_BF.
地球上の平和
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Scroll text horizontally

Post by JHPJHP »

Saki/walbus wrote:@JHPJHP
In Germany they say: "Don't look a gift horse in the mouth". :wink:

Make simple a better Scroll tool as mine with JavaScript and post it here.
Or a better text tool as the FontMaster_BF.
Yet again, the point is lost on you.
Last edited by JHPJHP on Fri Apr 09, 2021 10:51 pm, edited 6 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Scroll text horizontally

Post by RASHAD »

For Windows
Very simple a few lines of code
No limits to your imaginations
It's an image after all (text - image - effects)
Beside it doesn't stop while moving

Code: Select all

Global x

Procedure scrollText(param, timer)
  x -1
  If x = -380
    x = 380
  EndIf
  ResizeGadget(1,x,#PB_Ignore,#PB_Ignore,#PB_Ignore)
EndProcedure

OpenWindow(0, 0, 0, 400, 130, "Scroller", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
LoadFont(0, "Georgia"  ,  32)
ContainerGadget(0,10,10,380,80,#PB_Container_Flat)
SetGadgetColor(0,#PB_Gadget_BackColor,$88E6FD)
ImageGadget(1,0,12,380,80,0)
CloseGadgetList()
CreateImage(0,376,76,24,$88E6FD)
StartDrawing(ImageOutput(0))
DrawingFont(FontID(0))
DrawingMode(#PB_2DDrawing_Transparent )
pos = DrawText(0,6,".... ",$4DFE3A)
pos = DrawText(pos,6,"Scrolling ",$FE785D)
pos = DrawText(pos,6,"Text ",$5D80FE)
DrawText(pos,6,"....",$16E200)
StopDrawing()
SetGadgetState(1,ImageID(0))

ButtonGadget(2,10,98,40,24,"ON",#PB_Button_Toggle)

cycle = 20

Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      timeKillEvent_(TEvent)
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
          If GetGadgetState(2) = 1
          SetGadgetText(2,"OFF")
          TEvent = timeSetEvent_(cycle,0,@ScrollText(),0,#TIME_PERIODIC)
        Else
          SetGadgetText(2,"ON")
          timeKillEvent_(TEvent)
          x = 0
          ResizeGadget(1,x,#PB_Ignore,#PB_Ignore,#PB_Ignore)
        EndIf          
      EndSelect
  EndSelect
Until Quit = 1
Edit : Modified
You can replace timeSetEvent_() with BindEvent(#pb_event_timer,?)
Egypt my love
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Scroll text horizontally

Post by JHPJHP »

Saki/walbus wrote:@JHPJHP
In Germany they say: "Don't look a gift horse in the mouth". :wink:

Make simple a better Scroll tool as mine with JavaScript and post it here.
Or a better text tool as the FontMaster_BF.
You seemed to have missed the point again. There are literally millions of web programmers, how can you compare your BF shambles?

-------------------------------------------

This example demonstrates fairly smooth scrolling and applies a unique ease-in/out effect.
- Windows Services & Other Stuff\Other_Stuff\GadgetStuff\WebGadget\MarqueeText...
Last edited by JHPJHP on Sun Apr 18, 2021 5:42 am, edited 2 times in total.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Scroll text horizontally

Post by Saki »

JHPJHP wrote:
Saki/walbus wrote:@JHPJHP
@JHPJHP - dear
What you know, amazing, but thank you for that, an honour.

And now also you think that's particularly great ?
Besides, it's a big pile of rubbish code for a single simple line of text !
地球上の平和
Post Reply