简体   繁体   English

将两个图像绘制到HTML5 Canvas

[英]Draw two images to HTML5 Canvas

Currently I am working on making a Chroma Key HTML5 Camera test app. 目前,我正在开发一个Chroma Key HTML5 Camera测试应用程序。

http://jsfiddle.net/XuScp/ http://jsfiddle.net/XuScp/

Currently it kind of works (It will select green in your background and remove it like any other Chroma Key, currently I took out the green color and replaced it with a kind of grey color as thats the background I currently have). 目前可以正常工作(它将在您的背景中选择绿色,并像其他任何色度键一样将其删除,当前我将绿色取下,并用一种​​灰色替代,即当前的背景)。

The issue I am having is drawing the background in the same canvas as the camera is in. Currently it draws to the canvas but once the camera is loaded the image disappears which I am guessing is being replaced by the camera. 我遇到的问题是在与相机所在的画布上绘制背景。当前它绘制到画布上,但是一旦加载相机,图像就会消失,我猜这是由相机所取代。

var bckg;
    bckg = new Image();
    bckg.src = 'https://raw.github.com/haywars/greenscreen/master/cambackgrounds/background1.jpg';
    function getImage(url){
        bckg= new Image();
        bckg.src = url;
    };

var processes = {
    timerCallback: function() {
        if (this.myVideo.paused || this.myVideo.ended) {
            return;
        }
        this.ctxInput.drawImage(this.myVideo, 0, 0, this.width, this.height);
        this.pixelScan();
        var self = this;
        setTimeout(function () {
            self.timerCallback();
        }, 0);
    },


    doLoad: function() {
        var video = document.querySelector('video');
        var canvas = document.querySelector('canvas');
        var ctx = canvas.getContext('2d');
        this.myVideo = document.getElementById("myVideo");
        this.cInput = document.getElementById("cInput");
        this.ctxInput = this.cInput.getContext("2d");
        this.cOutput = document.getElementById("cOutput");
        this.ctxOutput = this.cOutput.getContext("2d");
        var self = this;
        this.ctxOutput.drawImage(bckg,0,0);
        this.myVideo.addEventListener("playing", function() {
            self.width = self.myVideo.videoWidth;
            self.height = self.myVideo.videoHeight;
            self.timerCallback();
        }, false);
    },
    pixelScan: function() {
        var frame = this.ctxInput.getImageData(0, 0, this.width, this.height);
        for (var i = 0; i < frame.data.length; i++) {
            var r = frame.data[i];
            var g = frame.data[i+1];
            var b = frame.data[i+2];
            if (g > 0 && r > 50   && r < 165 && b < 60)
            frame.data[i + 3] = 0;
        }
        this.ctxOutput.putImageData(frame, 0, 0);

        //var img= new Image();
        //img.src = "images/cambackgrounds/background1.jpg";
        //this.ctxOutput.putImageData(img, 0, 0);
        return;
    }

}

As far as I can tell, you only draw the background ( this.ctxOutput.drawImage(bckg,0,0); ) once, and that is during the doLoad function. 据我所知,您只绘制背景( this.ctxOutput.drawImage(bckg,0,0); )一次,那是在doLoad函数期间。 Your loop does draw the altered video to the ctxOutput seemingly correctly, it just doesn't redraw the background upon the next loop. 您的循环看似正确地将更改后的视频绘制到ctxOutput ,只是在下一个循环时不重画背景。 That COULD be the reason. 那可能是原因。

ALSO, 也,

I would, instead of drawing the background for each loop, simply place the image behind the main canvas, using either an Image or Canvas element. 我将使用图像或Canvas元素将图像放置在主画布后面,而不是为每个循环绘制背景。 You are already making the green pixels transparent, so you should be able to see everything through the main canvas. 您已经使绿色像素透明,因此您应该能够通过主画布看到所有内容。 I might start with setting the z-index of each, and maybe setting their style.position s to 'absolute' to ensure one is exactly behind the other. 我可能会先设置每个z-index,然后将其style.position设置为'absolute'以确保其中一个恰好位于另一个之后。

This will take away any lag that would have resulted from drawing the background image to the same canvas each loop. 这将消除每个循环将背景图像绘制到同一画布上所产生的任何滞后。

I've come across multiple tutorials and benchmark tests that suggest layering with canvases. 我遇到过许多建议使用画布进行分层的教程和基准测试。 The background is usually static, so why constantly update it. 背景通常是静态的,所以为什么要不断更新它。 Just make it its own canvas. 只是使其自己的画布。 Layer one on top of the other, each containing certain types of elements (static, uniform motion, independent motion, etc.) to ensure maximum performance. 一层一层在另一层之上,每个层包含某些类型的元素(静态,匀速运动,独立运动等),以确保最佳性能。

I hope this was of some help! 希望对您有所帮助!

Sincerely, 真诚的

TheCrzyMan 疯狂的男人

@shayward - I agree with @TheCrzyMan, use hidden and display canvas tags to chromakey @shayward-我同意@TheCrzyMan,使用隐藏并显示画布标签来抠像

I am sure you have seen this mozilla tut on chroma key . 我确定您已经看到此Mozilla Tut在色度键上

Jeffrey Burtoft also has a step-by-step tut on this too. Jeffrey Burtoft也对此进行了逐步介绍。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM