我已閱讀了關於視網膜顯示器上畫布模糊問題的多個建議(例如,使用window.devicePixelRatio方法; here,here和here),但我還沒有將建議的解決方案應用於我的特定問題。下面的腳本首先創建一個包含一些隨機圖像數據(顯示模糊)的畫布,然後將圖像導出到SVG元素並重新調整它(當然仍然是模糊的)。 2016年末,我使用觸摸屏和Safari瀏覽器使用MBP。有關如何避免模糊並實現清晰邊緣的任何建議?請記住,初始imageData應該有固定的寬度和高度。視網膜顯示(MPB)上的畫布圖像數據
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<body></body>
<script type="text/javascript">
var width = 100;
var height = 100;
var canvas = d3.select("body").append("canvas");
context = canvas.node().getContext("2d"),
canvas
.attr("width", width)
.attr("height", height)
.style("width", width + "px")
.style("height", height + "px")
//this is the part that should normally take care of blurriness
if (window.devicePixelRatio > 1) {
var devicePixelRatio = window.devicePixelRatio || 1;
var backingStoreRatio = context.webkitBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
var ratio = devicePixelRatio/backingStoreRatio;
canvas
.attr('width', width * ratio)
.attr('height', height * ratio)
.style('width', width + 'px')
.style('height', height + 'px');
context.scale(ratio, ratio);
}
var imageData = context.createImageData(width, height);
for (var i = 0, l = 0; i<height; ++i) {
for (j = 0; j<width; ++j, l += 4) {
imageData.data[l+0] = Math.round(Math.random() * 255);
imageData.data[l+1] = Math.round(Math.random() * 255);
imageData.data[l+2] = Math.round(Math.random() * 255);
imageData.data[l+3] = Math.round(Math.random() * 255);
}
}
context.putImageData(imageData, 0, 0);
var ImageD = canvas.node().toDataURL("img/png");
var svg = d3.select('body').append('svg').attr('width', width*5).attr('height', height*5);
svg.append("svg:image").datum(ImageD).attr("xlink:href", function(d) {return d})
.attr("height", height*5).attr("width", width*5)
</script>
採取的背景下,爲什麼你沒有能夠應用針對您的具體問題提出解決方案 –
好吧,上面的腳本實現這些解決方案並沒有成功 – spyrostheodoridis
視網膜有CSS像素設置爲2個物理像素。畫布分辨率設置爲CSS像素。要修復,請將畫布分辨率設置爲畫布大小的2倍。通過'const bounds = element.getBoundingClientRect();獲取大小;'然後將畫布大小設置爲'canvas.width = bounds.width * 2'和'canvas.height = bounds.height * 2' – Blindman67