gpt4 book ai didi

javascript - 视网膜显示器 (MPB) 上的 Canvas 图像数据

转载 作者:行者123 更新时间:2023-11-30 11:31:42 26 4
gpt4 key购买 nike

我已经阅读了关于视网膜显示器 Canvas 模糊问题的多项建议(例如使用 window.devicePixelRatio 方法;herehere 以及 here),但我无法应用针对我的具体问题的建议解决方案。以下脚本首先使用一些随机图像数据(看起来很模糊)创建一个 Canvas ,然后将图像导出到 SVG 元素并重新缩放它(当然仍然模糊)。我正在使用 2016 年底的 MBP,带有触摸栏和 safari。关于如何避免模糊和实现清晰边缘的任何建议?请记住,初始图像数据应具有固定的宽度和高度。

<!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 获取视网膜像素比,屏幕外 Canvas 取自 here ,然后放大取自 here 的上下文

<!DOCTYPE html>
<meta charset="utf-8">

<script src="https://d3js.org/d3.v4.min.js"></script>

<body></body>

<script type="text/javascript">

const width = 20;
const height = 20;
const scale = 10; // the higher the number the crisper the custom image

var canvas = d3.select("body").append("canvas");
context = canvas.node().getContext("2d");

const ratio = window.devicePixelRatio || 1;
canvas.attr('width', width * ratio * scale)
.attr('height', height * ratio * scale)
.style('width', width * scale + 'px')
.style('height', height * scale + 'px');

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);
}
}

const offCtx = canvas.node().cloneNode().getContext('2d'); // create an off screen canvas
offCtx.putImageData(imageData, 0,0);
context.scale(ratio * scale, ratio * scale);
context.mozImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
context.drawImage(offCtx.canvas, 0,0);

//export image
var ImageD = canvas.node().toDataURL("img/png");
//load image
d3.select('body').append('svg').attr("height", 500).attr("width", 500).append("svg:image").datum(ImageD).attr("xlink:href", function(d) {return d})
.attr("height", 500).attr("width", 500);

</script>

关于javascript - 视网膜显示器 (MPB) 上的 Canvas 图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com