JavaScript – 是否有相当于画布的toDataURL方法SVG?

我正在尝试将svg图像加载到画布中进行像素操作
我需要一个方法,如toDataURL或get ImageData for svg

在Chrome / Safari上,我可以尝试通过图像和画布

var img = new Image()
img.onload = function(){
  ctx.drawImage(img,0) //this correctly draws the svg image to the canvas! however...
  var dataURL = canvas.toDataURL(); //Security_ERR: DOM Exception 18
  var data = ctx.getimageData(0,img.width,img.height).data //also Security_ERR: DOM Exception 18
  }
  img.src = "image.svg" //that is an svg file. (same domain as html file :))

但是我得到安全错误.
有什么办法吗

这是一个现场演示的问题http://clstff.appspot.com/gist/462846(你可以查看源)

解决方法

来自: http://www.svgopen.org/2009/papers/12-Using_Canvas_in_SVG/#d4e105

The reason why you cannot use an SVG image element as source for the drawImage method is simple,but painful: the current Canvas specification does not (yet) allow to reference SVGImageElement as source for drawImage and can only cope with HTMLImageElement,HTMLCanvasElement and HTMLVideoelement. This short-coming will hopefully be addressed during the process of defining “SVG in HTML5” behavior and Could be extended to allow SVGSVGElement as well. The xhtml:img element in listing 3 uses visibility:hidden as we do not want it to interfere with its visible copy on the Canvas.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...