Gif animated in canvas

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Addict
Addict
Posts: 821
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Gif animated in canvas

Post by minimy »

Hi! Now that I'm recovered from the hangover :lol: . After a long period without sharing code, I return to the attack!
I want to share this code to use animated gifs in a canvas.
Was teste with many gifs and all work here. Tell me if not work for you, and please shar if you impreve the code.
The system use timer taked from the gif, but if is not defined '0', then use this var defaultDelay=60 ms.

I hope be interesting for you.

Note: Can work in sprites, images and textures if you change the output. Can use transparent images if you use DrawAlphaImage.

Code: Select all

UseGIFImageDecoder()
Structure gif_data: img.i:frames.i:frame.i:width.i:height.i:timer.i:output.i: List spd.f():EndStructure: Global gif.gif_data
Procedure   gifLoad(canvas, file.s="mygif.gif", defaultDelay=60)
  If FileSize(file)>0
    If IsImage(gif\img):FreeImage(gif\img):EndIf: ClearList(gif\spd())
    gif\img= LoadImage(#PB_Any,file,0)
    gif\width=  ImageWidth(gif\img): gif\height= ImageHeight(gif\img): gif\frames= ImageFrameCount(gif\img): gif\output= canvas
    For p= 0 To gif\frames-1
      SetImageFrame(gif\img,p): AddElement(gif\spd())
      gif\spd()= GetImageFrameDelay(gif\img): If gif\spd()= 0: gif\spd()= defaultDelay: EndIf
    Next p
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure
Procedure gifPlayer()
  If gif\img
    If ElapsedMilliseconds() > gif\timer
      SetImageFrame(gif\img, gif\frame)
      StartDrawing(CanvasOutput(gif\output)): DrawImage(ImageID(gif\img),0,0): StopDrawing()
      gif\frame +1: If gif\frame >= gif\frames: gif\frame= 0: EndIf
      gif\timer= ElapsedMilliseconds() + gif\spd()
    EndIf
  EndIf
EndProcedure
OpenWindow(0,0,0,500,281,"Gif animated v1.0",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
canvas= CanvasGadget(#PB_Any,0,0,WindowWidth(0),WindowHeight(0))
ok= gifLoad(canvas, "D:\_MIO\OS Multi\GIF\giphy.gif"): Debug "File loaded= "+ ok
Repeat
  ev= WindowEvent()
  Select ev
    Case #PB_Event_Gadget
      eg=EventGadget():et=EventType()
      Select eg
        Case 0
      EndSelect
    Case #PB_Event_CloseWindow
      Break
  EndSelect
  gifPlayer(): Delay(1)
ForEver
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
minimy
Addict
Addict
Posts: 821
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Gif animated in canvas

Post by minimy »

If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
idle
Always Here
Always Here
Posts: 6154
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Gif animated in canvas

Post by idle »

thanks.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 631
Joined: Tue Jan 04, 2011 6:21 pm

Re: Gif animated in canvas

Post by SPH »

Ok on W11 and PB 6.30 b3 :wink:

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5609
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Gif animated in canvas

Post by Kwai chang caine »

Splendid and useful code, even if i hate GIF animated :mrgreen: :lol:
Thanks a lot for sharing this super idea and splendid code 8)
ImageThe happiness is a road...
Not a destination

PureBasic French Forum
User avatar
ChrisR
Addict
Addict
Posts: 1538
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Gif animated in canvas

Post by ChrisR »

Kwai chang caine wrote: Tue Jan 06, 2026 6:50 pm even if i hate GIF animated :mrgreen: :lol:
Not even a little GIF? I'm so disappointed :lol:
Thanks for sharing, minimy, that's cool 8)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5609
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Gif animated in canvas

Post by Kwai chang caine »

Not even a little GIF? I'm so disappointed
:wink: :lol:
ImageThe happiness is a road...
Not a destination

PureBasic French Forum
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 157
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

Re: Gif animated in canvas

Post by Jacobus »

Hi minimy,
I tested the example from the PB documentation, which is less complex than yours, with several types of GIFs and they all display correctly; the window adapts to the size of the GIF used. :)

Code: Select all

; Enable the GIF decoder
UseGIFImageDecoder()
Filename$ = Path$+"\giphy.gif"  ; Indicate the location on the disk
If LoadImage(0, Filename$)
  OpenWindow(0, 500, 300, DesktopUnscaledX(ImageWidth(0)), DesktopUnscaledY(ImageHeight(0)), "GIF viewer") 
  CanvasGadget(0, 0, 0, ImageWidth(0), ImageHeight(0))  
  AddWindowTimer(0, 0, 50) ; Adjust the timeout to see the speed difference (50, 100, 500)
  Repeat
    Event = WaitWindowEvent()   
    If Event = #PB_Event_Timer
      SetImageFrame(0, Frame) 
      If StartDrawing(CanvasOutput(0))
        DrawImage(ImageID(0), 0, 0)
        StopDrawing()
      EndIf      
      Frame+1
      If Frame >= ImageFrameCount(0) ; Cycle back to first frame, to play in loop.
        Frame = 0
      EndIf
    EndIf   
  Until Event = #PB_Event_CloseWindow
Else
  Debug "Impossible to load the file: " + Filename$
EndIf
PureBasicien tu es, PureBasicien tu resteras.
Post Reply