我已經創建了一個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