javascript - 使用具有透明 PNG 的 Canvas 进行像素化调整大小

标签 javascript image canvas pixel transparent

我想使用 Canvas 选项 imageSmoothingEnabled=false 实现像素化效果;所以图像在滚动时“不模糊”。

一切正常,直到使用透明图像即 PNG。投影缩放后的图像,并保留在背景中。

此外,直到用户滚动几个像素后,图像才会加载。

我发现 canvas.drawImage()函数拥有参数来设置偏移量。但是我还没有找到解决方案。

演示 https://jsfiddle.net/aLjfemru/

var ctx = canvas.getContext('2d'),
  img = new Image(),
  play = false;

/// turn off image smoothing - this will give the pixelated effect
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;


/// wait until image is actually available
img.onload = function(){
                image1.src="nf.png";

                context.drawImage(image1, 50, 50, 10, 10);
                };

img.src = 'https://upload.wikimedia.org/wikipedia/commons/b/bb/Gorgosaurus_BW_transparent.png';

/// MAIN function
function pixelate(v) {
  document.getElementById("v").innerHTML = "(v): " + v;

  /// if in play mode use that value, else use slider value
  var size = v * 0.01;

  var w = canvas.width * size;
  var h = canvas.height * size;

  /// draw original image to the scaled size
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.drawImage(img, 0, 0, w, h);
  ctx.drawImage(canvas, 0, 0, w, h, 0, 0, canvas.width, canvas.height);
}

function onScroll() {
  $(window).on('scroll', function() {
    var y = window.pageYOffset;
    if (y > 10) {
      y = Math.pow(y, 0.8);
      if (y >= 60) {
        y = 100;
      }
      pixelate(y);
    }
  });
}
onScroll();

最佳答案

一些快速的改变让它发生

使用第二个 Canvas 进行像素化

在进行渲染之前等待图像加载。

onscroll 在你滚动之前不会触发,所以当图像加载后调用渲染函数来显示图像。

canvas.width = innerWidth-20;
ctx = canvas.getContext("2d");
var ctxImage;
const img = new Image;
img.src = 'https://upload.wikimedia.org/wikipedia/commons/b/bb/Gorgosaurus_BW_transparent.png';
/// wait until image is actually available
img.onload = function(){
     // I dont knwo what this is for so removed the following two lines
    //image1.src="nf.png"; 
    //context.drawImage(image1, 50, 50, 10, 10);
    // Create a canvas to match the image
    var c = document.createElement("canvas");
    canvas.width = Math.min(canvas.width,(c.width = this.naturalWidth));
    canvas.height = c.height = this.naturalHeight;
    ctxImage = c.getContext("2d");
    // changing canvas size resets the state so need to set this again.
    ctx.imageSmoothingEnabled = false;
    onScroll();
    pixelate(100); // call first time
};

ctx.font = "32px arial";
ctx.textAlign = "center";
ctx.fillText("Loading please wait.",ctx.canvas.width /2, ctx.canvas.height / 4);
/// MAIN function
function pixelate(v) {
  document.getElementById("v").innerHTML = "(v): " + v;

  /// if in play mode use that value, else use slider value
  var size = Number(v) * 0.01;

  var w = img.width * size;
  var h = img.height * size;

 
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctxImage.clearRect(0, 0, ctxImage.canvas.width, ctxImage.canvas.height);
  ctxImage.drawImage(img, 0, 0, w, h);
  ctx.drawImage(ctxImage.canvas, 0, 0, w, h, 0, 0, canvas.width, canvas.height);
}

function onScroll() {
  addEventListener("scroll", function() {

    var y = window.pageYOffset;
    if (y > 10) {
      y = Math.pow(y, 0.65);
      if (y >= 100) {
        y = 100;
      }
      pixelate(y);
    }
  });
      
}
#fix {
      position: fixed;
    }

    html {
      height: 2000px;
    }
<div id="fix">
<p id="v" value="Animate">1</p><br />

<canvas id="canvas"></canvas>
</div>

关于javascript - 使用具有透明 PNG 的 Canvas 进行像素化调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459603/

相关文章:

javascript - HTML 如何阻止用户输入大于 2100-01-01 的日期?

ios - React-native iOS 不显示图像(pod 问题)

ios - PHP/IOS 6 使用 exif 从 iphone 上传时防止图像旋转

php - sql选择当前图像代码

math - 解释 - 通过控制点曲线的公式

javascript - 用鼠标移动 Canvas 形状

javascript - CSS:从左到右添加带有动画的边框 |右到左

javascript - 尝试通过 jQuery/replace() 删除括号内的 anchor 文本

javascript - 将 IE 8 Only JavaScript 添加到 Orchard CMS head 元素

canvas - 如何在 HTML5 Canvas 中剪辑 INSIDE 形状?