【发布时间】:2016-07-13 14:58:08
【问题描述】:
我正在尝试在画布上渲染 SVG 图像,一次绘制一个图像以填充给定的行,下面是相同的代码 sn-p:
function createSVGUrl(svg) {
var svgBlob = new Blob([svg], {type: 'image/svg+xml;charset=utf-8'});
return DOMURL.createObjectURL(svgBlob);
};
/**
* Renders svg tile on the given context.
* @param {CanvasRenderingContext2D} ctx
* @param {SVGElement} svg The svg tile.
* @param {{x: number, y:number}} pos The position to draw the svg tile on.
* @throws Error
*/
function renderSVGTile(ctx, svg, pos) {
var img = new Image();
var url = createSVGUrl(svg);
img.onload = function() {
try {
ctx.drawImage(img, pos.x, pos.y);
ctx.imageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
DOMURL.revokeObjectURL(url);
} catch (e) {
throw new Error('Could not render image' + e);
}
};
img.src = url;
};
问题是我可以看到我不想要的部分填充的行,有没有办法一次填充整行?
【问题讨论】:
-
等待所有图像都已预加载,然后绘制它。顺便说一句,调用drawImage之前应该设置imageSmoothingEnabled,避免循环设置。
-
@Kaiido 不完全是我尝试了建议的解决方案,但在我的情况下它不起作用,可能是由于每行的图像数量非常多。理论上它应该可以工作,但它没有,here 你可以看到这个例子。
-
o_o 为什么要通过 svg ?你知道canvas有一些绘图方法吗?你的代码会更简单。
-
@Kaiido 因为我们开发人员遇到的同样问题是的,这是要求。
标签: javascript html canvas svg web