[英]Javascript SVG drawing on canvas failing at event phase 2
I'm trying to embed an SVG on a canvas but I get an error at event phase 2 when the SVG is served from my own domain. 我正在尝试在画布上嵌入SVG,但是当我从自己的域提供SVG时,我在第2阶段遇到错误。 The curious thing is if I the same image is hosted on a different server it works fine.
奇怪的是,如果我将相同的图像托管在不同的服务器上,它可以正常工作。 The image I'm testing with is the one here .
我正在测试的图像是这里的图像。 Even if I
wget
the image and place it in the image folder I get the same error. 即使我
wget
图像并将其放在图像文件夹中,我也会得到同样的错误。 However, it does work if I place it in the document with <object data="/images/tiger.svg" type="image/svg+xml"></object>
, but not if I set the source in javascript and draw on a canvas. 但是,如果我将它放在带有
<object data="/images/tiger.svg" type="image/svg+xml"></object>
的文档中,它确实有效,但如果我在javascript中设置源代码则不行画在画布上。
Here is the code I'm using which errors when the image is hosted at my domain: 这是我正在使用的代码,当我的域托管图像时出现错误:
var canvas = document.getElementById('my-canvas'),
ctx = canvas.getContext('2d'),
img = new Image();
img.onload = function() { ctx.drawImage(img, 0, 0); };
img.onerror = function(err) { console.log(err); };
img.src = '/images/tiger.svg'; // replacing this with 'http://phrogz.net/svg/tiger.svg' does work
Here is the error data from Firebug 这是来自Firebug的错误数据
NONE 0
defaultPrevented false
multipleActionsPrevented false
stopImmediatePropagation stopImmediatePropagation()
bubbles false
cancelable false
eventPhase 2
isTrusted true
timeStamp 1373387631408000
type "error"
The server that hosts the SVG has to send the right headers, and not every server is configured to do so. 承载SVG的服务器必须发送正确的标头,并不是每个服务器都配置为这样做。
There's a good guide here http://kaioa.com/node/45 这里有一个很好的指南http://kaioa.com/node/45
If you're using apache, you can add an htaccess file to your directory with this code: 如果您正在使用apache,则可以使用以下代码将htaccess文件添加到您的目录:
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
This sends the headers to serve SVG and the compressed version, SVGZ. 这将发送标头以提供SVG和压缩版本SVGZ。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.