5

我想加入多个画布以制作单个图像。那么有什么方法可以将多个画布转换toDataURL为单个图像?

4

3 回答 3

5

创建一个接受多个参数(画布元素)的函数,将它们放在一个空白画布上,然后返回新制作的画布的 dataurl。

var getImadeData = function () {
    var i = arguments.length,
        tempCanvas = document.createElement("canvas"),
        ctx = tempCanvas.getContext("2d");
     while (i--) {
        ctx.drawImage(arguments[i], 0, 0);
    };
    return tempCanvas.toDataURL();
};

现在只需像这样将要放入一个 dataurl 中的多个画布元素提供给函数。

var newData = getImageData(canvas1, canvas2, canvas3);

在此示例中,最后一个画布首先放置在空白画布上,因此请确保正确排列画布元素。

于 2014-06-22T06:13:15.117 回答
4

是的,drawImage画布 2d 渲染上下文中的方法接受画布元素作为图像元素。所以你所要做的就是:

  • 创建一个新画布
  • 获取它的上下文
  • 绘制所有其他画布到它drawImage
  • 从这个新画布中提取最终图像
于 2012-06-07T09:36:46.177 回答
3

试试这个例子希望它会帮助看到这里

                //html block


                <canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">


                </canvas>
                <canvas id="myCanvas1" width="200" height="100" style="border:1px solid #c3c3c3;">
                Your browser does not support the canvas element.
                </canvas>

                <canvas id="Canvasimage" width="500" height="100" style="border:1px solid #c3c3c3;">
                Your browser does not support the canvas element.
                </canvas>
    <img id="finalimage" width="500" height="100"  style="border:1px solid #c3c3c3;"/>

                //script block
                function loadImages(sources, callback) {
                    var images = {};
                    var loadedImages = 0;
                    var numImages = 0;
                    // get num of sources
                    for (var src in sources) {
                        numImages++;
                    }
                    for (var src in sources) {
                        images[src] = new Image();
                        images[src].onload = function () {
                            if (++loadedImages >= numImages) {
                                callback(images);
                            }
                        };
                        images[src].src = sources[src];
                    }
                }

                window.onload = function (images) {

            //Canvas first  here
                    var c = document.getElementById("myCanvas");
                    var ctx = c.getContext("2d");
                    ctx.fillStyle = "#FF0000";
                    ctx.fillRect(0, 0, 200, 100);
                 //Canvas second here
                    var c1 = document.getElementById("myCanvas1");
                    var ctx1 = c1.getContext("2d");
                    ctx1.fillStyle = "#00FF00";
                    ctx1.fillRect(0, 0, 200, 100);


               //Canvas final here.
                    var canvas = document.getElementById("Canvasimage");
                    var context = canvas.getContext("2d");

                    var sources = {
                        darthVader: c.toDataURL("image/png"),
                        yoda: c1.toDataURL("image/png")
                    };

                    loadImages(sources, function (images) {
                        context.drawImage(images.darthVader, 100, 30, 200, 137);
                        context.drawImage(images.yoda, 350, 55, 93, 104);
//finalimage  here which has two canvas data
                var finalimage = document.getElementById("finalimage");
        finalimage.src = canvas.toDataURL("image/png"); 
                    });
                };
于 2012-06-07T10:04:01.067 回答