Thanks zak.
Always nice code I can put into official examples.
Hi
drawing using canvas is a pleasure, since it is simple, and the refreshing screen done without the user to concern about it, believe me some languages do the refrersh by continious executing of the drawing code, and if you want to do the refresh yourself using api, god help you.
i have posted before about ploting sine curve slowly, and it is done using tbgl , and tbgl+ui, but using canvas alone is simpler: it is like plotting using quick basic but more advanced. you can simply convert all vb6 and quickbasic plotting codes to thinbasic.
the following code will plot the sine curve
Canvas_SetPixel(x*30+300, y*80+300) for plotting pixels,
Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) ) for plotting with thickness defined by a previous "Canvas_Width(10)" .
i have used the canvas new feature like:
a = Canvas_WaitKey(27) to wait pressing ESC to exit, the 27 is the asc of ESC you can use 32 to exit by pressing space bar
to plot without animation just delete the first "Canvas_Redraw" and keep the second one
Uses "UI" Uses "math" ' Global variables '----------------------------------------------------------- Dim ScreenWidth As Long = 800 Dim ScreenHeight As Long = 600 Dim hWin As DWord '---Handle of the canvas window Dim a As Long Dim x,y,x1,y1 As Double '----------------------------------------------------------- ' Main program '----------------------------------------------------------- hWin = Canvas_Window("test Canvas sine curve: press ESC to exit", 1, 1, ScreenWidth, ScreenHeight ) Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer '---Init canvas Canvas_Clear(%BLACK) Canvas_Width(10) For x=-9 To 15 Step 0.05 y = Sin(x) Canvas_Color(Rgb(0, 255, 0)) 'plotting the thin sine curve points: Canvas_SetPixel(x*30+300, y*80+300) x1 = x*30+300: y1 = y*80+150 'plotting the thick sine curve points: Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) ) Canvas_Redraw Next x Canvas_Redraw a = Canvas_WaitKey(27) Canvas_Window End
Thanks zak.
Always nice code I can put into official examples.
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
hello
i was not aware of the Canvas_Scale. Petr confirmed its existence and thanks for him for suggesting the feature "floating point values would be accepted" here http://community.thinbasic.com/index.php?topic=2425.0
so i repost the above example using this feature.
x in sin(x) in thinbasic in radians.
Canvas_Scale making your own world, it is better than mapping manualy your world to real canvas, it is done here automatically. you just guess the values produced by your functions or data, then design the Canvas_Scale(x1,y1,x2,y2) to suit that data.
PS: added #MINVERSION 1.8.6.0 as suggested by Petr#MINVERSION 1.8.6.0 Uses "UI" Uses "math" ' Global variables '----------------------------------------------------------- Dim ScreenWidth As Long = 800 Dim ScreenHeight As Long = 600 Dim hWin As DWord '---Handle of the canvas window Dim a As Long Dim x,y,x1,y1,width As Double width = 30 '----------------------------------------------------------- ' Main program '----------------------------------------------------------- hWin = Canvas_Window("Canvas sine curve with Canvas_Scale: press ESC to exit", 1, 1, ScreenWidth, ScreenHeight ) Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer Canvas_Scale(-4*Pi, -3, 4*Pi, 3) '---Init canvas Canvas_Clear(%BLACK) For x=-4*Pi To 4*Pi Step 0.05 y = Sin(x) Canvas_Color(Rgb(0, 255, 0)) 'plotting the thin sine curve points: Canvas_SetPixel(x, y) 'plotting the thick sine curve points: x1 = x: y1 = y - 1.5 Canvas_Width(width) Sleep(2) Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) ) width = width - 0.06 Canvas_Redraw Next x Canvas_Redraw a = Canvas_WaitKey(27) Canvas_Window End
thanks for this nice example zak
but I have noticed an error, see my picture, with this line
a = canvas_waitkey(27)
best regards, frank
you can't always get what you want, but if you try sometimes you might find, you get what you need
hi frank
it is running okay on my system winxp thinbasic 1.8.6,
i don't know why this error, make sure you have installed the latest thinbasic since this feature canvas_waitkey(...) introduced recently
regards
Frank,
it is new feature in the latest Beta to have params for Canvas_Waitkey.
Zak, when surfing on the latest release wave, you might consider adding on top of your script:
[code=thinbasic]
#MINVERSION 1.8.6.0
[/code]
This way ThinBASIC checks first, if proper version is installed.
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
zak, petr, haven't updated to new beta, that was my little problem.
example is working fine here. servus, frank
you can't always get what you want, but if you try sometimes you might find, you get what you need
Bookmarks