How to upload image to Dart

I am trying to use Dart Language and HTML5 Canvas, but I have one problem. I do not know how to upload an image to Dart. I can get CanvasRenderingContext2D, and I can call fillText () and fillRect () with this, and everything works, but I'm trying to figure out how to load an image and draw using drawImage.

+7
source share
3 answers

Create and upload image

ImageElement image = new ImageElement(src: "my_image.png"); image.onLoad.listen((e) { // Draw once the image is loaded }); 

Draw the above image on canvas after loading it

 context.drawImage(image, destX, destY); 
+10
source

New image loading syntax:

 readFile() { ImageElement image = new ImageElement(src: "plant.png"); document.body.nodes.add(image); image.onLoad.listen(onData, onError: onError, onDone: onDone, cancelOnError: true); } onData(Event e) { print("success: "); } onError(Event e) { print("error: $e"); } onDone() { print("done"); } 
+9
source

I know this question is old, but maybe this can help someone else. There is another way to do this:

 void main() { ImageElement image = new ImageElement(src: "pic.png"); img.onLoad.listen(onData); img.onError.listen(onError); } void onData(Event e) { print("Load success"); } void onError(Event e) { print("Error: $e"); } 
+1
source

All Articles