html - 两倍大小的像素

标签 html css image pixel

我刚刚为我的元素创作了小像素艺术 http://i.imgur.com/9FLj6Uk.png .我想在我的网站上使用它。如您所见,它是小像素艺术。我希望将一个像素绘制为 2*2 像素而不是 1*1 像素。我会使用 2*2 像素而不是一个像素来重新绘制图片,但这似乎是不好的解决方案。

我尝试在上面使用 CSS

img.pixel {
  width: 32px;
  height: 32px
}

但这不起作用,它在我的浏览器中显示出奇怪的阴影。我想看到硬像素。有谁知道这个问题的任何解决方案?

This is the problem when I use the CSS above 这是我使用上面的CSS时的问题

最佳答案

像素艺术在浏览器中很难,主要是因为缺乏对“像素化”或“清晰边缘”图像渲染的通用浏览器支持。应该是supported in CSS4 .

目前 CSS 堆栈看起来是这样的,虽然看起来 Chrome 30 和 Opera 16 已经打破了对 CSS 解决方案的支持

image-rendering:optimizeSpeed;
image-rendering:-moz-crisp-edges;
image-rendering:-o-crisp-edges;
image-rendering:optimize-contrast;
image-rendering:-webkit-optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;

参见 this answer来自@Phrogz,带有test case .另见 mozilla's documentation就此主题而言。为了现在的普遍支持,JS 解决方案可能必须暂时工作,例如在很棒的文章 drawing pixels is hard 上看到的:

var resize = function( img, scale ) {
// Takes an image and a scaling factor and returns the scaled image

// The original image is drawn into an offscreen canvas of the same size
// and copied, pixel by pixel into another offscreen canvas with the 
// new size.

var widthScaled = img.width * scale;
var heightScaled = img.height * scale;

var orig = document.createElement('canvas');
orig.width = img.width;
orig.height = img.height;
var origCtx = orig.getContext('2d');
origCtx.drawImage(img, 0, 0);
var origPixels = origCtx.getImageData(0, 0, img.width, img.height);

var scaled = document.createElement('canvas');
scaled.width = widthScaled;
scaled.height = heightScaled;
var scaledCtx = scaled.getContext('2d');
var scaledPixels = scaledCtx.getImageData( 0, 0, widthScaled, heightScaled );

for( var y = 0; y < heightScaled; y++ ) {
    for( var x = 0; x < widthScaled; x++ ) {
        var index = (Math.floor(y / scale) * img.width + Math.floor(x / scale)) * 4;
        var indexScaled = (y * widthScaled + x) * 4;
        scaledPixels.data[ indexScaled ] = origPixels.data[ index ];
        scaledPixels.data[ indexScaled+1 ] = origPixels.data[ index+1 ];
        scaledPixels.data[ indexScaled+2 ] = origPixels.data[ index+2 ];
        scaledPixels.data[ indexScaled+3 ] = origPixels.data[ index+3 ];
    }
}
scaledCtx.putImageData( scaledPixels, 0, 0 );
return scaled;

通读这篇文章,视网膜显示器和移动 safari 的存在可能会增加渲染正确尺寸像素艺术的额外复杂性。尽管有了 iOS7 的移动版 Safari,这可能会得到纠正。

关于html - 两倍大小的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318053/

相关文章:

javascript - 根据字符串对 HTML 列表进行排序

php - 在 PHP 中的 <img> 标签的开头和结尾附加 <figure> 标签

html - 使用 css 更改 html 文本颜色

html - 强制 ul 包含 float 列表项 : use &nbsp or overflow: auto?

javascript - 如何优化类似<picture>标签的background-image?

html - IE float 无填充错误

html - iOS 将不可见的 Unicode 字符显示为包含 “mvs” 的虚线框

html - 为什么 z-index 不影响 parent sibling ?

jquery - 如何使用 jQuery 动画更改背景图像?

image - 在开始和结束处暂停 GIF,并使用覆盖的文本来表示开始和结束