kin*_*jou 7 jquery svg raphael
我试图将用Raphael.js生成的svg转换成PNG图像.好吧,当svg没有模式和图像组件时,我将svg转换为图像.然后当我将这两个组件添加到SVG中时出现问题并且转换失败.完整的小提琴在 这里.即使我保存生成的svg并在浏览器中打开而不转换为图像,也不会渲染图像和模式.
代码段是:
var r = Raphael(document.getElementById("cus"), 390, 253);
r.setViewBox(-5, -2, 228, 306);
for (var outline in doorMatOutline) {
var path = r.path(doorMatOutline[outline].path);
//path.attr({fill: 'url('+patternuri+')'}); //adding pattern
}
//adding image
var img = r.image(imageuri, 5 ,10 ,100 ,100);
var svg = $('#cus').html().replace(/>\s+/g, ">").replace(/\s+</g, "<");
canvg('cvs', svg, {
ignoreMouse: true,
ignoreAnimation: true
});
var canvas = document.getElementById('cvs');
var img = canvas.toDataURL("image/png");
$("#resImg").attr('src', img);
$("#cus").hide();
Run Code Online (Sandbox Code Playgroud)
我已经在这里解决了这个问题http://jsfiddle.net/fktGL/1/,首先我必须更改 svg 属性
xmlns="http://www.w3.org/2000/svg"
到
xmlns:xlink="http://www.w3.org/1999/xlink"
Run Code Online (Sandbox Code Playgroud)
由于 svg 未在 W3C 验证服务上进行验证,这里有一个 stackoverflow 解释需要进行更改。
然后我必须添加一些超时以允许 SVG 正确渲染。我理解这是因为图像已在 SVG 和画布中绘制,但两者都需要一些时间来渲染图像。我的解决方案在速度较慢的设备上不能完美工作(增加超时会有所帮助),但我不知道另一种方法来解决这个问题(欢迎任何评论/改进)。
这是最后的狙击
var r = Raphael(document.getElementById("cus"), 390, 253);
r.setViewBox(-5, -2, 228, 306);
for (var outline in doorMatOutline) {
var path = r.path(doorMatOutline[outline].path);
path.attr({
fill: 'url(' + patternuri + ')'
});
}
$('#cus svg').removeAttr('xmlns');
// If not IE
if(/*@cc_on!@*/false){
$('#cus svg').attr('xmlns:xlink',"http://www.w3.org/1999/xlink");
}
// First timeout needed to render svg (I think)
setTimeout(function(){
var svg = $('#cus').html();
window.svg = svg;
canvg(document.getElementById('cvs'), svg);
// annother delay required after canvg
setTimeout(function(){
var canvas = document.getElementById('cvs'),
img = canvas.toDataURL("image/png");
$("#resImg").attr('src', img);
//$("#cus").hide();
console.log("ending... ");
},100)
},100);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2660 次 |
| 最近记录: |