【问题标题】:how can we grayscale a pattern image used in canvas (javascript)我们如何对画布中使用的图案图像进行灰度化(javascript)
【发布时间】:2018-04-24 19:06:53
【问题描述】:

创建了一个圆形上下文并添加了图案图像以填充该圆圈。我怎样才能使该图案图像灰度化。 有什么想法可以帮到我。

代码:

    ctx.beginPath();
    var bg = new Image();    
    bg.src = image;  
    bg = function() {
        var pattern = ctx.createPattern(this, "no-repeat");
        ctx.fillStyle = pattern;
    };
    ctx.moveTo(canvasCenterHoriz, canvasCenterVert);
    ctx.arc(x, y, radius, startAngle, endAngle, direction);
    ctx.lineWidth = 0;
    ctx.fill();

【问题讨论】:

  • 那个 dup 答案已经过时并且不显示当前方法。使用ctx.filter = "saturate(0%)" 并绘制图像,它将是灰度的。或者绘制图像然后设置ctx.globalCompositeOperation = "saturation"ctx.fillStyle = "#888",然后用填充在图像上绘制以获得灰度。
  • 这个答案stackoverflow.com/a/39026987/3877726 显示了许多实时 FX 过滤器应用于画布上的视频(但也可用于静止图像)并且不需要安全的相同域图像,并且比建议的速度快很多倍重复答案的非常老派的方法。
  • 太棒了@Blindman67,这是一个更好的副本:stackoverflow.com/questions/40787895/…
  • 你打败了我,还有一个更好的答案:)

标签: javascript canvas grayscale


【解决方案1】:

这可能不是最好的方法,但它非常简单并且有效。

我在下面附上了一个工作示例。以下是它的工作原理:

图案图像是在不可见的画布中绘制的。加载图像后,将其绘制在画布上,并将白色覆盖和饱和度设置为全局合成操作。画布现在将包含您的图案的灰度版本。

然后将临时画布转换为图像,其源设置为画布数据 url。也许有更好的方法在两个画布之间发送图像数据,但我还没有找到。

图案完成后,您的原始弧线将使用新图案绘制。

let canvas = document.getElementById('grayscale-canvas');
let ctx = canvas.getContext('2d');

function makeGrayscaleBackground(image, onready) {
  var bg = new Image();
  bg.src = image;
  bg.onload = function() {
    // Create a canvas that's not attached to the DOM
    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', this.width);
    canvas.setAttribute('height', this.height);
    document.body.appendChild(canvas);
    let ctx = canvas.getContext('2d');

    // Draw the background image
    ctx.drawImage(this, 0, 0);

    // Then draw a white layer on top, with the saturation composite operation
    // which will remove the color from the underlying image
    ctx.globalCompositeOperation = "saturation";
    ctx.fillStyle = "#FFF";
    ctx.fillRect(0, 0, this.width, this.height);

    onready(canvas);
  };

}

function drawWithImage(image) {

  makeGrayscaleBackground(image, function(patternImage) {
    // Green background
    ctx.fillStyle = "#3f9";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Make a repeating pattern with image we created
    var pattern = ctx.createPattern(patternImage, "repeat");

    // Make an arc with the pattern
    let x = y = canvas.width/2;
    ctx.fillStyle = pattern;
    ctx.beginPath();
    ctx.arc(x, y, canvas.width/2-10, 0, 2*Math.PI);
    ctx.fill();
  })

}

// Example pattern image
// For security reasons, the image needs to be hosted on the same server as the script!
var bgImage = "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7";
drawWithImage(bgImage);
document.getElementById('bg').src = bgImage;
<canvas width="150" height="150" id="grayscale-canvas"></canvas><br>
Actual background image: <img id="bg"><br>
Modified background image:

【讨论】:

  • 您可以直接使用画布作为图案源。
  • 谢谢,如果没有人自愿,我明天更新sn-p。
  • @Kaiido 用您的建议更新了答案。干杯
猜你喜欢
  • 2019-04-21
  • 1970-01-01
  • 2013-04-15
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
相关资源
最近更新 更多