如何让用户下载使用 RaphaelJs 生成的图像?我希望它以 HTML 格式显示在 HTML 上,<img>
以便他们可以右键单击图像以将其保存到桌面。
问问题
5970 次
1 回答
13
这是将 RaphelJs 转换为图像所需的简单示例
需要的库是:
- RaphaelJs - http://raphaeljs.com/
- Raphel.Export - https://github.com/ElbertF/Raphael.Export
- canvg - http://code.google.com/p/canvg/
-
<script type="text/javascript" src="raphaeljs.js"></script>
<script type="text/javascript" src="raphael.export.js"></script>
<script type="text/javascript" src="canvg.js"></script>
<script>
window.onload = function(){
//Start with a simple raphaelJs drawing
var paper = new Raphael(document.getElementById('raphaelDiv'), 200, 200);
var circle = paper.circle(50, 40, 10);
circle.attr("fill", "#f00");
circle.attr("stroke", "#fff");
var circle2 = paper.circle(70, 60, 50);
//Use raphael.export to fetch the SVG from the paper
var svg = paper.toSVG();
//Use canvg to draw the SVG onto the empty canvas
canvg(document.getElementById('myCanvas'), svg);
//I had to use setTimeout to wait for the canvas to fully load before setting the img.src,
//will probably not be needed for a simple drawing like this but i ended up with a blank image
setTimeout(function() {
//fetch the dataURL from the canvas and set it as src on the image
var dataURL = document.getElementById('myCanvas').toDataURL("image/png");
document.getElementById('myImg').src = dataURL;
}, 100);
}
</script>
<!--The containers below are set to be invisible, we need them for the
conversion but we dont need to se them-->
<div id="raphaelDiv" style="display:none;"></div>
<canvas id="myCanvas" style="display:none;"></canvas>
<img id="myImg" src="">
Wola,你有你的形象!我希望这对某人有用!
于 2013-01-05T19:18:09.360 回答