Ooops
November 28, 2020, 01:37:38 AM

Author Topic: [bb] "Marching Ants" Selection Line by sswift [ 1+ years ago ]  (Read 570 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : "Marching Ants" Selection Line
Author : sswift
Posted : 1+ years ago

Description : This code draws an animated selection line like you might find in Photoshop.  To animate it, you need only to keep redrawing the line each frame.

The nice thing about a selection line like this is that it will be visible over any background, and the motion not only makes it more visible, but acts as an inidcator that the line is not part of the image it is drawn over.


Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------------------------------------------------------------
  2. ; This function draws a line of "marching ants" to the current buffer.
  3. ; This is the same kind of animated line drawn around a selection in Photoshop.
  4. ;
  5. ; There is no need to do anything to make the ants animate, other than continuously redrawing the line on the screen.
  6. ; -----------------------------------------------------------------------------------------------------------------------------------
  7. Function Line_MarchingAnts(X1#, Y1#, X2#, Y2#)
  8.        
  9.         Local X#, Y#
  10.         Local XDist#, YDist#
  11.         Local Mv#
  12.         Local StepX#, StepY#
  13.         Local AntSpeed, AntSize, AntCounter
  14.         Local RGB
  15.        
  16.         ; Setup
  17.  
  18.                 XDist# = X1# - X2#
  19.                 YDist# = Y1# - Y2#
  20.        
  21.                 If XDist# < 0 Then XDist# = -XDist#
  22.                 If YDist# < 0 Then YDist# = -YDist#
  23.                
  24.                 Select (YDist# > XDist#)
  25.                         Case True  Mv# = YDist#
  26.                         Case False Mv# = XDist#
  27.                 End Select
  28.                
  29.                 StepX# = XDist# / Mv#
  30.                 If (X1# > X2#) Then StepX# = -StepX#
  31.        
  32.                 StepY# = YDist# / Mv#
  33.                 If (Y1# > Y2#) Then StepY# = -StepY#
  34.  
  35.                 X# = X1#
  36.                 Y# = Y1#
  37.        
  38.         ; This is the clever bit that makes the ants move.
  39.                
  40.                 ; The starting value is determined by the current time.
  41.                 ; AntSpeed is the amount of time in milliseconds it takes for an ant to move one ant length.
  42.                 ; AntSize is the size of each ant, plus the space between them.
  43.                
  44.                 AntSpeed = 50
  45.                 AntSize  = 8
  46.                 AntCounter = Abs((MilliSecs()/AntSpeed) Mod AntSize)
  47.        
  48.         LockBuffer GraphicsBuffer()
  49.         For nc = 0 To Floor(mv#)
  50.  
  51.                 Select (Abs(AntCounter) Mod AntSize) < (AntSize/2)
  52.                         Case True  WritePixelFast X#, Y#, $00000000
  53.                         Case False WritePixelFast X#, Y#, $FFFFFFFF
  54.                 End Select
  55.        
  56.                 X# = X# + StepX#
  57.                 Y# = Y# + StepY#
  58.  
  59.                 AntCounter = AntCounter - 1
  60.  
  61.         Next
  62.         UnlockBuffer GraphicsBuffer()
  63.  
  64. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal