Snippet Mojo2 multiple canvases w/ transparent backgrounds

olive

Member
3rd Party Module Dev
Tutorial Author
Patreon Bronze
Joined
Jul 17, 2017
Messages
77
Quick example of how to layer multiple canvases in mojo2 without having big opaque rectangles on each one.

You can have a whole bunch of layers per canvas + do fancy stuff with coordinating their positioning, scale, etc., but this is the simplest case I could make to demonstrate the process. If people are interested, I could try to generalize the module I've put together to do that stuff.

Code:
Strict

Import mojo2


Class Test Extends App

   Field canvas1:Canvas
   Field layer1:DrawList
  
   Field canvas2:Canvas
   Field layer2:DrawList
  
   Field circle_x:Float
   Field circle_y:Float
  
   Method OnCreate%()
       SetUpdateRate 60
      
       canvas1 = New Canvas
       layer1 = New DrawList
      
       canvas2 = New Canvas
       layer2 = New DrawList
      
       Return 0
   End
  

   Method OnUpdate%()
       circle_x = MouseX()
       circle_y = MouseY()
      
       Return 0
   End

   Method OnRender%()
       canvas1.Clear()
       layer1.Reset()
  
       canvas2.Clear()
       layer2.Reset()

      
       layer1.SetColor(.75,0,0)
       For Local i% = 0 to 10
           layer1.DrawRect(i*100, 0, 50, DeviceHeight())
       Next
      
       layer2.SetColor(0,0,.75)
       layer2.DrawCircle(circle_x, circle_y, 50)
      
      
       canvas1.RenderDrawList(layer1)
       canvas1.Flush()
      
       canvas2.RenderDrawList(layer2)
       canvas2.Flush()
       Return 0
   End

End

Function Main%()
   New Test
   Return 0
End

(note: I'm still back on Monkey X & haven't made the jump to Cerberus yet, so I dunno if there'd be any difference there)
 
Great pice of code Wick. That should work without changes.
 
Back
Top Bottom