本文介绍了将剪贴板图像粘贴到画布上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个画布,我需要用户能够粘贴图像。
我希望这是跨浏览器。我只想使用html / javascript。我也愿意使用flash对象。
I have a canvas that i need the users to be able to paste an image onto.I would like this to be cross browser. I would like only to use html/javascript. I would also be willing to use a flash object.
推荐答案
这在Chrome中可以正常工作,已经能够弄清楚如何让它在Firefox中工作。你可以使用这个jQuery插件来检测剪贴板粘贴。我假设你知道如何从剪贴板中获取数据后绘制图像。
This works fine in Chrome, though as of yet I haven't been able to figure out how to get it to work in Firefox. You can use this jQuery plugin to detect clipboard pastes. I'll assume you know how to draw the image once you have the data from the clipboard.
# 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
这篇关于将剪贴板图像粘贴到画布上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!