網膜ディスプレイ(例えば、window.devicePixelRatioアプローチを使用して、here、here、またhereを使用して)のキャンバスぼやけの問題に関する複数の提案を読んだが、私はできなかった私の特定の問題に提案された解決策を適用する。次のスクリプトは、まず不透明な画像データを含むキャンバスを作成し、その画像をSVG要素に書き出して再スケーリングします(まだぼやけています)。私は2016年後半にタッチバーとサファリを使用して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>
から取られたコンテキストをスケールアップするためwindow.devicePixelRatio:私は、次の組み合わせを使用しますあなたの特定の問題に対する解決策を提案しますか? –
よく、上記のスクリプトは、これらのソリューションを成功させないで実装します。 – spyrostheodoridis
RetinaはCSSピクセルを2物理ピクセルに設定しています。キャンバス解像度はCSSピクセルに設定されます。修正するには、キャンバス解像度をキャンバスサイズの2倍に設定します。 'const bounds = element.getBoundingClientRect();でサイズを取得すると、' canvas.width = bounds.width * 2'と 'canvas.height = bounds.height * 2'にキャンバスサイズを設定します。 – Blindman67