Lense effect


Active Member
Tutorial Author
Jan 2, 2020
A lense effect using mojo2.

Lense effect:
Import mojo2

Function Main:Int()
    New Game()
End Function

Class Point

    Field x:Float
    Field y:Float

    Method New(tX:Float,tY:Float)
        x = tX
        y = tY


Class Game Extends App

    Field myimage:Image
    Field canvas:Canvas
    Field k:Float = 0.0001
    Field cX:Int
    Field cY:Int
    Field p:Point[][]
    Const WIDTH:Int = 256
    Const HEIGHT:Int = 256
    Const GAP:Int = 2

    Method OnCreate:Int()
        SetSwapInterval 1
        SetUpdateRate 0
        SetDeviceWindow 640,480,4
        canvas=New Canvas
        p = New Point[WIDTH][]
        For Local i:Int = 0 Until WIDTH
            p[i] = New Point[HEIGHT]
            For Local j:Int = 0 Until HEIGHT
                p[i][j] = New Point(i * GAP,j * GAP)
        Return 0           
    End Method

    Method OnUpdate:Int()
        cX = MouseX()
        cY = MouseY()
        Return 0
    End Method

    Method OnRender:Int()
        canvas.Clear 0,0,0
        canvas.SetAlpha 1.0
        canvas.SetColor 1,1,1
        For Local i:Int = 0 Until WIDTH
            For Local j:Int = 0 Until HEIGHT
                Local pX:Float = getRadialX(p[i][j].x,p[i][j].y,cX,cY,k)
                Local pY:Float = getRadialY(p[i][j].x,p[i][j].y,cX,cY,k)       
           '    canvas.DrawPoint pX,pY
                canvas.DrawRect pX,pY,1,1,myimage,i,j,1,1
        Return 0       
    End Method

    Method getRadialX:Float(x:Float,y:Float,cx:Float,cy:Float,k:Float)
      Local res:Float = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      Return res;
    End Method

    Method getRadialY:Float(x:Float,y:Float,cx:Float,cy:Float,k:Float)
      Local res:Float = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      Return res
    End Method

End Class
Screenshot 2020-07-11 at 19.01.01.png
Last edited:


Well-Known Member
CX Code Contributor
Jun 21, 2017
I would suggest the removal of the image of the Mona Lisa png. That is unless you were physically there and took the picture yourself. Some museums tend to get rather annoyed distributing unauthorised copies. Even though the original picture it's self would be public domain, with the artist being long gone and copyright laws being introduced long after the original work was completed. Any reproduction would be copyright by the person or organisation that had commissioned any such reproduction.
Top Bottom