0

質問があります。いくつかのjqueryイベントのピクセル化の後に必要な画像があります。pixelate.js やその他のプラグインは使用したくありません。プロジェクトには適していません。

私が望むのは、CSSイメージレンダリングを使用して、イメージを同じ画像のより小さな「コピー」に自動的に変更することです。ピクセル化されていますが、イメージの2番目のコピーはありません。CSS/javascriptで可能ですか?

私の悪い英語でごめんなさい、そしてありがとう!

4

2 に答える 2

0

HTML5 の canvas 要素でこれを行うことができます。基本的に、画像を取得して小さなキャンバスに描画し、キャンバスを引き伸ばして元の画像サイズに合わせます。次に例を示します。

$('.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-22T03:55:17.920 に答える