画布:在不模糊的情况下调整图像大小

杜海姆

我试图在画布中表示一个图像,并希望画布是其父容器宽度的一定百分比。问题是我在将图像添加到画布时以某种方式模糊了图像,我不想模糊图像。

我认为添加ctx.imageSmoothingEnabled = false会禁用模糊,但它似乎没有帮助。

有谁知道如何防止下面的图像在这个画布中模糊?任何想法将不胜感激!

var img = new Image();
img.src = 'https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg';

img.onload = function() { 
  var canvas = document.querySelector('canvas');
  canvas.width = hidden.clientWidth;
  canvas.height = hidden.clientHeight;
  var ctx = canvas.getContext('2d');
  ctx.webkitImageSmoothingEnabled = false;
  ctx.mozImageSmoothingEnabled = false;
  ctx.imageSmoothingEnabled = false;
  ctx.drawImage(hidden,
    0, 0, this.naturalWidth, this.naturalHeight,
    0, 0, hidden.clientWidth, hidden.clientHeight)

}
#container {
  width: 800px;
  background: #efefef;
}

#hidden {
  position: absolute;
  top: -1000%;
}

#hidden,
#canvas {
  width: 60%;
}
<div id='container'>
  <img src='https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg' id='hidden'>
  <canvas></canvas>
</div>

盲人67

画布分辨率和显示尺寸不一样。

查看图像,我看到它的大小为 444 像素 x 800 像素,这是非常低的分辨率。它的显示方式在很大程度上取决于显示设备。

您在问题中将画布显示宽度设置为容器大小的 60%,并将画布分辨率设置为图像 (444by800)。

很可能是显示宽度和画布分辨率不匹配。

画布显示尺寸

  • canvas.style.widthcanvas.style.height表示图像将填充的页面区域,与画布分辨率无关。

画布分辨率

  • canvas.widthcanvas.height属性以像素为单位表示画布的分辨率,用于存储图像内容的实际像素数

浏览器正在添加模糊。

假设 60%(对于#hidden {width : 60%})是 1920 像素宽的高清显示器,画布将被拉伸,60% * 1920 = 1152px比画布大 2.6 倍。

画布内容没有模糊,您已经正确绘制了图像。浏览器将画布以大于画布分辨率的尺寸渲染到页面上,从而增加了模糊效果。

简单的修复

要阻止浏览器模糊画布的内容,请使用 CSS 属性image-rendering: pixelated;,如下例所示。

var img = new Image();
img.src = 'https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg';

img.onload = function() { 
  canvas.width = this.naturalWidth
  canvas.height = this.naturalHeight
  var ctx = canvas.getContext('2d');
  // ctx.imageSmoothingEnabled = false;  // not needed as you are not streaching the image
  ctx.drawImage(this, 0, 0);

}
.big {
  width:100%;
  image-rendering: pixelated;
}
<canvas class="big" id="canvas"></canvas>

简单锐化两种色调黑白。

图像的分辨率非常低,因此在大多数情况下,您无法提高质量。

但由于它是黑白两色(不是黑色、灰色和白色),您可以将其拉伸到更大的画布上并增加对比度以消除渲染到画布时引入的一些模糊。

它是通过使用屏幕和颜色燃烧复合操作将画布多次渲染在自身之上来完成的。对比度增加以减少模糊但保持较少像素化的外观。

突出差异 使用此方法仅锐化了图像的左半部分。

var img = new Image();
img.src = 'https://gist.githubusercontent.com/duhaime/019f2ccb98391adbf460a10463059683/raw/c4c2bc1fa923d905aba0ef88e05c0760823d2cca/000630070000650.jpg';

img.onload = function() { 
  const bounds = canvas.getBoundingClientRect(); //Get the canvas display sise
  
  canvas.width = bounds.width;
  canvas.height = this.naturalHeight * (bounds.width / this.naturalWidth);
  var ctx = canvas.getContext('2d');
  // draw to canvas 
  ctx.drawImage(this, 0, 0,canvas.width,canvas.height);
  // draw again using screen to up white areas
  ctx.globalCompositeOperation = "screen";
  ctx.drawImage(canvas, 0, 0,canvas.width/ 2,canvas.height,0, 0,canvas.width/ 2,canvas.height);
  // draw again to pukl grays down some 
  ctx.globalCompositeOperation = "color-burn";
  ctx.drawImage(canvas, 0, 0,canvas.width/ 2,canvas.height,0, 0,canvas.width/ 2,canvas.height);
  // draw again using screen to up white areas
  ctx.globalCompositeOperation = "screen";
  ctx.globalAlpha = 0.5;
  ctx.drawImage(canvas, 0, 0,canvas.width/ 2,canvas.height,0, 0,canvas.width/ 2,canvas.height);
  // draw again to pukl grays down some 
  ctx.globalAlpha = 1;
  ctx.globalCompositeOperation = "color-burn";
  ctx.drawImage(canvas, 0, 0,canvas.width/ 2,canvas.height,0, 0,canvas.width/ 2,canvas.height);
  
}
.big {
  width:100%;
}
<canvas class="big" id="canvas"></canvas>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在不调整div大小的情况下调整图像大小

如何在没有像素模糊的情况下调整html5画布的大小

ImageMagick:在不缩放图像的情况下调整图像大小

在不损失质量的情况下调整图像大小MVC 4

如何在不损失画质的情况下调整图像大小

如何在不裁剪或降低质量的情况下调整图像大小?

在特定情况下调整图像大小

在不更改分区大小的情况下调整dd转储的大小

如何在不加载图像的情况下调整图像大小?

如何在不在 Tkinter 中拉伸图像的情况下调整图像大小?

Three.js – 如何在不调整渲染器或缩放内容的情况下调整画布大小?

如何在不丢失任何数据的情况下调整分区大小(使我的Ubuntu分区更大)?

Linux:在不更新GPT的情况下调整磁盘大小:gdisk表示无法使用新扇区

在不破坏系统的情况下调整ext4分区的大小

在不获取滚动条的情况下调整远程桌面窗口的大小

如何在不丢失现有数据的情况下调整std :: vector的大小?

在Word中,如何在不更改表宽度的情况下调整列的大小?

是否可以在不创建继承QGraphicsItem的类的情况下调整QGraphicsItem的大小?

有没有办法在不绘制多余形状的情况下调整 JFrame 的大小?

CSS 网格,如何在不弄乱整个页面的情况下调整元素的大小

使用 CSS 在不知道尺寸的情况下调整屏幕上的图像大小并居中

在没有JavaScript的情况下调整HTML中图像的大小

在不使用图像的情况下调整背景色的大小

在不使用LVM的情况下调整分区大小

在不影响内容的情况下调整窗口大小

(Python--numpy)如何在不循环的情况下调整numpy数组的大小并对其进行切片?

是否可以在不卸载和重新安装Linux(或丢失数据)的情况下调整根分区的大小?

如何在不模糊文本的情况下模糊图像?

在不调整文本框大小的情况下调整Fabric Rect的大小