【问题标题】:Paste clipboard image to canvas将剪贴板图像粘贴到画布
【发布时间】:2011-07-24 16:06:25
【问题描述】:

我有一个画布,我需要用户能够将图像粘贴到该画布上。 我希望这是跨浏览器。我只想使用 html/javascript。我也愿意使用 Flash 对象。

【问题讨论】:

  • 大多数现代浏览器甚至不允许您从剪贴板读取文本,除非有特定的配置更改/权限对话框。此外,图像仍然是客户端的,我猜你需要它在服务器端?
  • 我实际上只需要图像是客户端。

标签: javascript canvas clipboard


【解决方案1】:

这在 Chrome 中运行良好,但到目前为止我还没有弄清楚如何让它在 Firefox 中运行。您可以使用这个 jQuery 插件来检测剪贴板粘贴。一旦你从剪贴板获得数据,我假设你知道如何绘制图像。

# jquery.paste_image_reader.coffee
(($) ->
  $.event.fix = ((originalFix) ->
    (event) ->
      event = originalFix.apply(this, arguments)

      if event.type.indexOf('copy') == 0 || event.type.indexOf('paste') == 0
        event.clipboardData = event.originalEvent.clipboardData

      return event

  )($.event.fix)

  defaults =
    callback: $.noop
    matchType: /image.*/

  $.fn.pasteImageReader = (options) ->
    if typeof options == "function"
      options =
        callback: options

    options = $.extend({}, defaults, options)

    this.each () ->
      element = this
      $this = $(this)

      $this.bind 'paste', (event) ->
        found = false
        clipboardData = event.clipboardData

        Array::forEach.call clipboardData.types, (type, i) ->
          return if found
          return unless type.match(options.matchType)

          file = clipboardData.items[i].getAsFile()

          reader = new FileReader()

          reader.onload = (evt) ->
            options.callback.call(element, file, evt)

          reader.readAsDataURL(file)

          found = true

)(jQuery)

使用方法:

$("html").pasteImageReader
  callback: (file, event) ->
    # Draw the image with the data from
    # event.target.result

【讨论】:

    【解决方案2】:

    据我所知,仅使用 JavaScript 和 HTML 是无法做到这一点的。但是,this blog post 描述了使用 Java 小程序实现此目的

    【讨论】:

      【解决方案3】:

      使用 HTML5 画布可以轻松得多。您应该能够使用画布的“drop”事件或窗口的“paste”事件来完成此操作。下面的代码 sn-ps 改编自我用来完成此任务的 typescript 类。

      this.canvas.addEventListener("drop", onDrop);
      window.addEventListener("paste", onPaste);
      
      function onDrop(event: DragEvent): void {
        event.preventDefault();
        const file = event.dataTransfer.files[0];
        this.createRasterFromFile(file);
      }
      
      function onPaste(event: ClipboardEvent): void {
        event.preventDefault();
        event.stopPropagation();
        const file = event.clipboardData.items[0].getAsFile();
        this.createRasterFromFile(file);
      }
      
      function createRasterFromFile(file: File): void {
        if (file && (file.type == 'image/png' || file.type == 'image/jpeg')) {
          const reader = new FileReader();
          reader.onload = function () {
            // Image data stored in reader.result
          }.bind(this);
          reader.readAsDataURL(file);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-06-14
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 2013-12-06
        • 2010-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多