Hi,
If I may add my few cents to it…you can use javascript to draw an image in the context and then draw any shape on the image from python functions e.g:
Note: I am not using custom html, hence finding elements by tag name etc.
In your html code
<script>
function myCanvas() {
var c = document.getElementsByTagName("canvas")[0];//find canvas
var ctx = c.getContext("2d");
var img = document.getElementsByTagName("img")[2];//find image loaded from file loader
var imageObj = new Image();
imageObj.onload = function(){
ctx.drawImage(img,0,0);
};
imageObj.src = img.src;
}
My Form1 function:
def file_loader_1_change(self, file, **event_args):
"""This method is called when a new file is loaded into this FileLoader"""
small_img = anvil.image.generate_thumbnail(file, 640) #reducing image size
self.image_1_show(small_img,**event_args)
width, height = anvil.image.get_dimensions(small_img)
#setting canvas size to my small_img size
self.canvas_1.width = width
self.canvas_1.height = height
Image is now displayed:
def image_1_show(self,file,**event_args):
"""This method is called when the Image is shown on the screen"""
self.image_1.source = file
Call js when Image is clicked:
def image_1_mouse_down(self, x, y, button, **event_args):
"""This method is called when a mouse button is pressed on this component"""
self.call_js('myCanvas')
Now, any kind of canvas drawing method ,like:
def canvas_1_mouse_down(self, x, y, button, **event_args):
"""This method is called when a mouse button is pressed on this component"""
self.canvas_1.stroke_rect(x,y,50,50)
pass
You’ll need to convert coord system on images to match x and y values,
Hope this is of any help,
Archie