Canvas is cool! - page 64

 
Vladimir Pastushak:

Hi all, I'd like to deepen my knowledge of Canvas

I don't even know where to start. Right now I'm imagining that Canvas is complicated logarithmic formulas and stuff like that.

Can you give me an example of how to make a button like below on canvas. I need some code to understand it.


Forum on trading, automated trading systems & strategy testing

Doing a crowdsourced project on Canvas

Nikolai Semko, 2017.05.24 02:20


Here's what came out.


The button can be displayed behind the background and in the foreground, set to click lock or not.

In the example file CanvasButton.mq5 it is possible to drag the button with the mouse. To do this, simply hold down Ctrl to move the blue button, and Shift to move the red button.

Because the GButton class is a descendant of the CCanvas class, the example demonstrates the ability to use functions of the CCanvas class on the CircleWu example (This is implemented in OnInit). So, it is possible to write text on the button using functions of the CCanvas class.

This class, of course, does not pretend to be clean. The graphics does not shine with perfection and 3D is very conventional; the shadows are not implemented, the execution speed is not optimized. And most likely there are a lot of rough edges and lack of various checks. So do not judge severely.

If you spend a little more time, of course it can be done:


I was just trying to... you could say - to set a direction.


Canvas.mqh file differs from original only in that m_pixels array is defined as public.


 

My compliments to Nikolay for his keen enthusiasm to experiment with Canvas. And I have studied the code examples with no less pleasure.

Alas of course, Canvas in its current implementation is purely CPU-bound algorithms for filling the bitmap array with graphical primitives. I.e. via loops with array element enumeration and that's about it... Meanwhile, it was 21 years ago...

The experiments are certainly good, but the implementation for practical purposes should be not only beautiful but also effective. For I can certainly ignore it sitting at some top-of-the-line 8-core PC, but as a rule the main mass of users have rather middle class PCs. And any full screen GUI with market data visualization, even if it is very convenient and beautiful, will not be popular, if it will load the CPU up to 75%. This is a fact. That's why, and not because they simply had nothing else to do - hardware acceleration has been brought into Canvas implementations in all major browsers.

Coming to the core - in MQL5 there should be some wrapper for Direct2D (and DirectWrite) - it would be a real bomb, opening the way to the efficient hardware accelerated implementation of many practical tasks in the field of GUI and data visualization. Of course, you can say that there is already DirectX. But only its Direct3D part is represented, and that is 3D projections, shaders and other attributes of 3D and it's still rather a narrow niche for practical use. Yes, of course you can by some adjusting projections and other manipulations and tessellations achieve rendering 2D-graphics and through Direct3D - but it means neither more nor less to create your own implementation of Direct2D over Direct3D. This wouldn't make any sense since Direct2D was implemented a long time ago.

So here's a thought...
 
That said, everything can be drawn quickly on the CPU as well.

The examples above prove it. Plus, this is an application domain where there are no strict requirements for the final beauty and sophistication of the images.
 
Codexample:

My compliments to Nikolay for his keen enthusiasm to experiment with Canvas. And I have studied the code examples with no less pleasure.

Unfortunately, Canvas in its current implementation is nothing but CPU-bound algorithms for filling bitmap array with graphical primitives. I.e. via loops with array elements search and that's about it... Meanwhile, it was 21 years ago...

The experiments are certainly good, but the implementation for practical purposes should be not only beautiful but also effective. For I can certainly ignore it sitting at some top-of-the-line 8-core PC, but as a rule the main mass of users have rather middle class PCs. And any full screen GUI with market data visualization, even if it is very convenient and beautiful, will not be popular, if it will load the CPU up to 75%. This is a fact. That's why - and not because they simply had nothing else to do - hardware acceleration has been brought into Canvas implementations in all major browsers.

Actually coming to the point - in MQL5 would be some wrapper for Direct2D (and DirectWrite) - it would be a real bomb, opening the way to the efficient hardware-accelerated implementation of many practical tasks in the field of GUI and data visualization. Of course, you can say that there is already DirectX. But only its Direct3D part is represented, and that is 3D projections, shaders and other attributes of 3D and it's still rather a narrow niche for practical use. Yes, of course you can by some adjusting projections and other manipulations and tessellations achieve rendering 2D-graphics and through Direct3D - but it means neither more nor less to create your own implementation of Direct2D over Direct3D. This wouldn't make any sense since Direct2D was implemented a long time ago.

Here's a thought...

Thanks for the kind words.
Actually MQ are good in that regard and have taken care of the graphics and compute tools using GPUs and not just CPUs.
I mean OpenCL and DirectX.
Here are some examples comparing implementations of the same graphical task (8 gravity centres) in three variants - CPU, OpenCL(GPU) and DirectX(GPU):

1.CPU -14 fps. (script Swirl2.mq5)

2. OpenCL - 162 frames per second (requires OpenCL drivers installed on your computer) (Swirl2_OCL.mq5 script)

3. DirectX - 37 frames per second (script Swirl2_GPU.mq5)


so it's not so melancholic.
If you need maximum performance, you can use graphics card as well.
I should note that these tests were done on a rather weak laptop with a weakIntel® HD Graphics 620 graphics card.
While using a video card gives a very decent performance boost, I agree with Renat that even using a CPU for graphics output covers over 99% of all graphics tasks in the MQL5 task area
.
As long as my frame is generated in less than 10 milliseconds, I'm not particularly jittery about the GPU (although the temptation is there sometimes, good thing that there is room for maneuvering).
And in 10 milliseconds I could output, for example, such a thing with shadows, antialiasing and transparency:


SZY codes for GPU were downloaded fromthis thread, thanks a lot@Rorschach and@Serhii Shevchuk for that.

Files:
Swirl2.mq5  5 kb
Swirl2_OCL.mq5  14 kb
 
Folks, has anyone done any resizing of graphic resources using Canvas interpolation?
 
Nikolay Kositsin:
Folks, has anyone done any resizing of graphic resources using interpolation on Canvas?

I gave an example of the codehere. But the code is far from perfect. It would be quite different now.

Forum on trading, automated trading systems and strategy testing

Canvas is cool!

Nikolai Semko, 2018.03.24 04:13

Raster scaling script demo

There is a rather large BMP file sitting in the Ex5 file as a resource.



 
Nikolai Semko:

I gave an example of the codehere. But the code is far from perfect. I would have done it quite differently now.


I'm curious how you would do it now. It's in my best interest :)

 
Nikolai Semko:

I gave an example of the codehere. But the code is far from perfect. I would do everything differently now.


It is indescribable beauty :)

 

OpenCL:https://www.mql5.com/ru/forum/162864

DirectX:Remnant 3D as standard

Вот что можно сделать с OpenCL прямо в терминале MetaTrader 5 без всяких DLL
Вот что можно сделать с OpenCL прямо в терминале MetaTrader 5 без всяких DLL
  • 2016.12.10
  • www.mql5.com
Это демонстрация не только расчетов на GPU в MQL5 коде, но и графических возможностей терминала: Полный исходный код в виде скрипта приложен...
 
Artyom Trishkin:

Just wondering how you'd do it now. Purely in my best interest :)

Very briefly, Artem, earlier I perceived a pixel as a square with calculation of its weighted participation in a pixel of new scale through the area, but now I would perceive it as a circle with calculation of its weighted participation through the distance to the centre of the pixel of new scale.
Reason: