How can I paste an image from the clipboard onto a Canvas element using Dart?

折月煮酒 提交于 2019-12-07 18:02:03

问题


I'm using Dart to develop a personal whiteboard Chrome app and it is sometimes useful to be able to quickly copy and paste an image (e.g. a slide from a presentation, a diagram or a handout) so that I can add notes over the image while teaching a class or giving a presentation.

How can I paste an image stored on the clipboard onto a canvas element in Dart?


回答1:


Actually, this answer to the same question for JS is almost directly applicable. A Dart translation might look something like:

import 'dart:html';

void main() {
  var can = new CanvasElement()
    ..width = 600
    ..height = 600
  ;

  var con = can.getContext('2d');

  document.onPaste.listen((e) {
    var blob = e.clipboardData.items[0].getAsFile();
    var reader = new FileReader();
    reader.onLoad.listen((e) {
      var img = new ImageElement()
        ..src = (e.target as FileReader).result;
      img.onLoad.listen((_) {
        con.drawImage(img, 0, 0);
      });
    });
    reader.readAsDataUrl(blob);
  });

  document.body.children.add(can);
}


来源:https://stackoverflow.com/questions/17916968/how-can-i-paste-an-image-from-the-clipboard-onto-a-canvas-element-using-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!