How to draw an image in a Canvas ? #2431
-
Hello, For my application, I need to create a custom widget that won't exists out of the box on any platform. For that reason, I intend to use Canvas to manually draw the content of the widget. I'm starting to experiment with it, but I'm already facing an issue. Reading the documentation closely, I don't see an API to draw an image in the Canvas. Basically, I have SVG or PNG files that I want to get drawn in the Canvas at specific position. Here a screenshot of the expected result: I need to draw the exclamation, the check mark, the chevron, etc. I wish to reuse the existing PNG or SVG file as I don't want to manually draw them using programing. Is it possible ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You haven't missed anything - canvas doesn't support bitmap images. Canvas is a purely vector surface. It's likely possible to implement support for bitmaps on canvas, but the feature isn't there at present. However, more generally, my immediate reaction is that a canvas is almost certainly the wrong foundation on which to build this. It's a bare-bones drawing surface, not something intended for constructing complex UI widgets. It may superficially have the features that seem like a match for building a widget, like drawing text and handling mouse clicks - but the resulting widget won't look very native widget, as you need to take control of the entire drawing process, and the entire process of targeting clicks - and that will be almost impossible to get right, especially in a cross-platform way. My suggestion would be to relax your expectations on the exact appearance of your app, and work with the tools that Toga provides. For example - in this screenshot, you have what looks like a "start backup now" button. The "low friction" approach in Toga is to make this a button, not try and develop a highly customized look and feel to avoid using a button. Where you've got a text label, followed by an icon, with a possible call to action - use a Label, an Image, and a Button with an icon. Yes, this will look different to your screenshot. However, it will be a lot less work, and it will be visually consistent with the widgets on each platform. |
Beta Was this translation helpful? Give feedback.
-
Hello @freakboy3742 Thanks for helping again. My understanding is it won't be possible to create something identical to my screenshot and I need to adjust the design to accommodate current Toga limitation. Using Canvas is not a solution to workaround these limitation. |
Beta Was this translation helpful? Give feedback.
You haven't missed anything - canvas doesn't support bitmap images. Canvas is a purely vector surface. It's likely possible to implement support for bitmaps on canvas, but the feature isn't there at present.
However, more generally, my immediate reaction is that a canvas is almost certainly the wrong foundation on which to build this. It's a bare-bones drawing surface, not something intended for constructing complex UI widgets. It may superficially have the features that seem like a match for building a widget, like drawing text and handling mouse clicks - but the resulting widget won't look very native widget, as you need to take control of the entire drawing process, and the entire proc…