我有一个问题,在jquery事件像素化之后,我想要一个图像。我不想使用pixelate.js或其他插件,它们对我的项目不好。
我想要的是,用css图像渲染自动将图像更改为同一幅图片的较小的“副本”:像素化,但没有第二次图像副本。CSS/javascript是可能的吗?
对不起,我的英语不好,谢谢!
发布于 2016-06-22 03:55:17
您可以使用HTML5 5的画布元素来完成这个任务。本质上,我们想要把我们的图像,画成一个小画布,然后拉伸帆布,以填补原来的图像大小。下面是一个例子:
$('.image-container').each(function() {
var canvas = $(this).children('canvas').get(0),
ctx = canvas.getContext('2d'),
image = $(this).children('img').get(0),
imageWidth = $(image).width(),
imageHeight = $(image).height(),
scaleFactor = 0.05;
canvas.width = scaleFactor * imageWidth;
canvas.height = scaleFactor * imageHeight;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
$(canvas).width(imageWidth);
$(canvas).height(imageWidth);
});
$('#toggle-pixelization').on('click', function() {
$('.image-container').children().toggleClass('hidden')
});.hidden {
display: none;
}
.image-container canvas {
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="image-container">
<img src="https://placekitten.com/150/150" />
<canvas class="hidden"></canvas>
</div>
<button id="toggle-pixelization">Toggle Pixelization</button>
发布于 2016-06-21 15:52:12
img {
height: 100%;
width: 100%;
}您可以将img o的大小设置为其父级的最大大小。因此,如果父文件调整大小,图像也应该相应地调整大小。
https://stackoverflow.com/questions/37949050
复制相似问题