This is the property that allows us to jump to other page sections previously tagged. Then, we assign the data to the document Hashproperty. The trickhere is that we retrieve that information in its URL format and, thus, encoded as Base64data that we can assign to other document properties without losing its original meaning.
Once we get the reference to our Canvas instance, the following line of code retrieves the picture content itself and assigns it to the imgvariable. For that, we use the ControlIDproperty on our WebCanvas instance, adding the “canvas” appended by Xojo to all these kind of objects when generating the source code for the page. Js.Append "=img" // We assign that data to the document Hash property, so we can retrieve it from the Xojo sideĮxecuteJavaScript execute // Finally, we execute the JavaScript codeĪs you can see, the first line of the JavaScript code is in charge of retrieving the cCanvasreference from the DOM (document object model) of the Web Page. Js.Append "var img = canvas.toDataURL(""image/png base64 "") " // We access to the image data, encoded as a base64 string Js.Append "var canvas = document.getElementById('" + cCanvas.ControlID + "_canvas') " // We get the item reference based on its ID Once the WebButton has been added to the WebPage from the Library, put the following code in its ActionEvent Handler: Dim js() As String In fact, this will be the button in charge of calling the process to save the image as a picture file on the user disk. This is the process we will execute from the ActionEvent Handler of a WebButtonadded to the Webfrom the Library. The first interesting step is retrieving the underlying image represented by the WebCanvas and that is something we can get using a fragment of JavaScriptcode executed from the Xojo side. For example, we can draw a picture added to our project (with the filename “landscape”), using this line of code in the Paint event of our cCanvasinstance: g.DrawPicture landscape, 0, 0, g.Width, g.Height Retrieving the WebCanvas image data Obviously, you can draw anything you want in the WebCanvas or even painta picture on it. Use the following code to draw a black colored rectangle: g.FillRect 0, 0, g.Width, g.Height With the WebCanvas selected in the Navigator, change its name to cCanvasusing the Inspector and add the PaintEvent Handler to it using the Insert > Event Handleroption from the menu.
In order to follow this tutorial, create a new Webproject and add a new WebCanvascontrol from the Library to the default Web Page added to the project: Web. However, by using a little JavaScript you can easily add this capability to Xojo. Sometimes is can be useful to be able to save the graphics drawn in the WebCanvas to an image file, but unfortunately there is no built-in Xojo method to do this. It takes advantage of the HTML5 Canvas making it fast and powerful. The WebCanvascontrol is used for drawing graphics in web apps.