将两个ImageData混合到一个带有Javascript偏移量的ImageData中

时间:2013-06-22 16:51:24

标签: javascript canvas putimagedata

我正在尝试将两个ImageData对象混合到一个对象中,以获得与此link

中显示的图片类似的结果

以下是具有两个ImageData的Javascript代码

var redImage = copy.getImageData((SCREEN_WIDTH - VIDEO_WIDTH)/2,(SCREEN_HEIGHT - VIDEO_HEIGHT)/2,VIDEO_WIDTH,VIDEO_HEIGHT);
var bluImage = copy.getImageData((SCREEN_WIDTH - VIDEO_WIDTH)/2,(SCREEN_HEIGHT - VIDEO_HEIGHT)/2,VIDEO_WIDTH,VIDEO_HEIGHT);
var redData = redImage.data;
var blueData = blueImage.data;

// Colorize red
for(var i = 0; i < redData.length; i+=4) {
    redData[i] -= (redData[i] - 255);
}
redImage.data = redData;

// Draw the pixels onto the visible canvas
disp.putImageData(redImage,(SCREEN_WIDTH - VIDEO_WIDTH)/2 - 25,(SCREEN_HEIGHT - VIDEO_HEIGHT)/2);

// Colorize cyan
for(var i = 1; i < blueData.length; i+=4) {
    blueData[i] -= (blueData[i] - 255);
    blueData[i+1] -= (blueData[i+1] - 255);
}
blueImage.data = blueData;

// Draw the pixels onto the visible canvas
disp.putImageData(blueImage,(SCREEN_WIDTH - VIDEO_WIDTH)/2 + 25,(SCREEN_HEIGHT - VIDEO_HEIGHT)/2);    

如何在将redData和blueData放到画布上之前合并/混合它?

3 个答案:

答案 0 :(得分:2)

可以用来混合两个图像的公式非常简单:

newPixel = imageMainPixel * mixFactor + imageSecPixel * (1 - mixFactor)

假设两个缓冲区长度相等的示例:

var mixFactor = 0.5; //main image is dominant

//we're using the red buffer as main buffer for this example
for(var i = 0; i < redData.length; i+=4) {
    redData[i]   = redData[i]   * mixFactor + blueData[i]   * (1 - mixFactor);
    redData[i+1] = redData[i+1] * mixFactor + blueData[i+1] * (1 - mixFactor);
    redData[i+2] = redData[i+2] * mixFactor + blueData[i+2] * (1 - mixFactor);
}

现在您的红色缓冲区包含混合图像。

要添加偏移,您只需使用偏移值重绘图像,例如:

var offset = 20; //pixels

copy.drawImage(originalImage, -offset, 0);  // <--
var redImage = copy.getImageData( /*...*/ );

copy.drawImage(originalImage, offset, 0);   // -->
var bluImage = copy.getImageData( /*...*/ );

答案 1 :(得分:1)

如果您不仅有ImageData个对象,还有源canvas元素,则可以使用此方法。

您可以通过调用toDataURL canvas方法获取base64编码的图像数据。然后,您可以从该数据创建Image元素,然后通过drawImage将该图像粘贴到目标画布。

示例代码:

function mergeImageData(callback, sources) {
    var canvas = document.createElement('canvas'),
        context,
        images = Array.prototype.slice.call(arguments, 1).map(function(canvas) {
                var img = new Image();
                img.onload = onLoad;
                img.src = canvas.toDataURL();
                return img;
            }
        ),
        imgCounter = 0,
        widths = [],
        heights = [];

    function onLoad() {
        widths.push(this.width);
        heights.push(this.height);

        if (++imgCounter == images.length) {
            merge();
        };
    };
    function merge() {
        canvas.width = Math.max.apply(null, widths);
        canvas.height = Math.max.apply(null, heights);
        context = canvas.getContext('2d');

        images.forEach(function(img) {
                context.drawImage(img, 0, 0, img.width, img.height);
            }
        );

        callback(context.getImageData(0, 0, canvas.width, canvas.height));
    };
};

答案 2 :(得分:0)

如何设置传输格式3d的功能 - 从格式完整并排到立体图,交替行,交替列,棋盘,原始并排和3d从3d?