从一个HTML5画布抓取并绘制到另一个

时间:2013-02-04 07:46:18

标签: javascript image-processing html5-canvas

我正在尝试从一个canvas元素中获取像素信息,对其进行操作,然后将其绘制到另一个canvas元素。当我拿到并绘制到同一个画布时,我已经能够完成这项工作。它也按特定顺序工作,但不是我需要的顺序。

我们的想法是让隐藏的画布保存图像信息,我可以从中拉取像素,操纵,然后绘制到主画布。当我从myCanvas绘制到myCanvas2时,这是有效的,但不是相反。我从中获取数据的图像不需要是画布,事实上我更喜欢它不是因为我想要尽可能少的资源。

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<canvas id="myCanvas2" width="800" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var image = new Image();
image.src = 'http://timsterrible.net/gaming/pixelsweeper/images/bariumStrip.png';

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(10,10,50,50);

var canRaw = document.getElementById("myCanvas2");
var canvas = canRaw.getContext("2d");

image.onload = canvas.drawImage(image, 0, 0);

function copy()
{
var imgData=ctx.getImageData(32,0,32,32);
canvas.putImageData(imgData,10,70);
}
</script>

<button onclick="copy()">Copy</button>

</body>
</html>

我的目标是在混合时将一种化学品泄漏到另一种化学品中。现在它们非常坚固,你可以在play around here时看到。

感谢您提供任何帮助以解决这一令人困惑的烦恼。

http://timsterrible.net/gaming/pixelsweeper/stack/notmixing.png

1 个答案:

答案 0 :(得分:0)

希望以下内容可以帮助您解决问题。 将函数getPixel作为方法添加到用于包含从中提取数据的所需图像的画布。 将函数putPixel作为方法添加到用于包含最终组合图像的画布。

我添加了对功能的快速测试,以查看两个图像summer.jpgwinter.jpg的中心部分是否正确合并。

以下代码的限制是画布和图片都具有相同的宽度和高度,但可以轻松更改。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>pixel swap</title>
<style type="text/css">
    div, canvas, img {
        position: absolute;
    }

    img {
        top:0px;
        left:0px;
        visibility:hidden;
    }

    #imagedata {
        left:10px;
        top:10px;
        visibility:hidden;
    }
    #maincanvas {
        left:10px;
        top:10px;
        border:1px solid black;
    }

</style>
<script type="text/javascript">
    // abbreviation function for getElementById
    function $(id) {
      return document.getElementById(id);
    }

    //height and width for all canvases and images
    var W=100;
    var H=100;

    //variables for canvases
    var cid, cmain;


    /*getPixel gets an individual pixel from the image, img
     */
    function getPixel(x,y,img) {
        this.ctx.drawImage(img,0,0);
        var imdata=this.ctx.getImageData(0,0,this.width,this.height);
        var col=4*(y*this.width+x);
        var pix=new Pixel();
        pix.red=imdata.data[col++];
        pix.green=imdata.data[col++];
        pix.blue=imdata.data[col++];
        pix.alpha=imdata.data[col];
        return pix;
    }

    //Pixel Object
    function Pixel() {
        this.red;
        this.green;
        this.blue;
        this.alpha;
    }

    function putPixel(x,y,pixel){
        var imdata=this.ctx.getImageData(0,0,this.width,this.height);
        var col=4*(y*this.width+x);
        imdata.data[col++]=pixel.red;
        imdata.data[col++]=pixel.green;
        imdata.data[col++]=pixel.blue;
        imdata.data[col]=pixel.alpha;
        this.ctx.putImageData(imdata,0,0);
    }


    function main() {
        //set cid so that pixels are readable
        cid=$("imagedata");
        cid.width=W;
        cid.height=H;
        cid.ctx=cid.getContext('2d');
        cid.getPixel=getPixel;

        //set cmain so that pixels are settable to main canvas
        cmain=$("maincanvas");
        cmain.width=W;
        cmain.height=H;
        cmain.ctx=cmain.getContext('2d');
        cmain.putPixel=putPixel;

        var img; //image read from
        var pixel;//pixel data

        //testing getPixel and putPixel
        //should be alternating columns in the centre of the combined image in cmain canvas
        for(var x=30;x<70;x++) {
            for(var y=30;y<70;y++) {
                if(x%2==0) {
                    img=$("s")
                }
                else
                {
                    img=$("w")
                }
                pixel=cid.getPixel(x,y,img);
                cmain.putPixel(x,y,pixel);
            }
        }
    }
</script>
</head>
<body onload="main()">
    <!-- list of hidden images for reading data-->
    <img id="s" src="images/summer.jpg">
    <img id="w" src="images/winter.jpg">
    <!-- end of list of images  -->

    <canvas id="imagedata"></canvas>
    <canvas id="maincanvas"></canvas>   
</body>
</html>