我已经创建了一个html5画布并使用了 ctx.fillRect(X,Y,W,H);和其他一些JavaScript功能。如何在HTML5画布中显示多个外部svg图形
现在我想在该画布的多个位置添加一个svg图形。
我想使用svg的原因是图像的清晰度&质量。 我尝试在HTML5中使用drawImage方法,但是我想要绘制的图像在使用drawImage方法添加到画布时不会提供非常优质的输出。无论我使用多高分辨率图像,图像质量都很低。
但我认为使用SVG图形是解决方案。
这是我使用canvg.js在之前类似的问题被提及,但目前还不清楚试过我使用的代码,
<canvas id="myCanvas" style="border: 2px solid #000000; width: 1280px; height: 800px;"></canvas>
<img id="location" src="location.png" alt="The Scream" width="25.5" height="41.5">
<script >
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillRect(4,4, 12,10);
ctx.fillStyle=fontBlack;
ctx.fillText("1",8,10);
ctx.fillRect(5,16, 7, 16);
ctx.fillRect(5,33, 7, 28);
ctx.fillRect(5,62, 7, 50);
ctx.fillRect(5,113, 7, 15);
ctx.fillRect(5,129, 7, 15);
//I have a list of coordinates in a javascript object list called selectedShelfList, I'm getting the necessary coordinates from that.
function drawLocations(){
for (var i=0; i<selectedShelfList.length; i++)
{
var x_coordinate = selectedShelfList[i].shelf_x;
var y_coordinate = selectedShelfList[i].shelf_y;
var img=document.getElementById("location");
ctx.drawImage(img,x_coordinate,y_coordinate,8.5,13.8);
//Instead of drawImage method i want to use a code to show a svg graphic here
}
}
</script >
由于我的解决方案计算器上找到了这个问题, 。 例如:
var ctx = document.getElementById('test').getContext('2d');
ctx.drawSvg('<svg><rect x="0" y="0" width="100" height="200" fill="red" /></svg>', 0 , 0 , 500, 500);
我如何给建议立即进行删除在此命令外部SVG的网址是什么? 我可以使用''作为drawSvg的参数吗?例如:ctx.drawSvg('location.svg',0,0,500,500);
有什么办法可以在画布内显示svg图形吗? Canvg能做到这一点吗?如果是这样如何?
请帮我解决这个问题。 :)
我试过这个,但仍然svg绘制是非常低质量。其实你的方法产生较低的质量svg。 :( – direndd
适用于我!我编辑了我的答案,包括一个svg图像的缩放,并且质量保持不变。 – markE
是的,在这个例子中它显示了一个更清晰的图像,但事情是,我试图显示一个svg在这个例子中,你已经使用了ctx.drawImage(svg1,0,0,size,size); // Size = 100.在我的例子中,它是一个小图标,应该是ctx.drawImage(svg1,0,0,10,14);这使得图像看起来质量很低(pixalated):/ – direndd