将多个画布转换为html5中的dataURL

时间:2012-06-07 09:24:07

标签: javascript html5

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

3 个答案:

答案 0 :(得分:5)

创建一个带有多个参数(canvas元素)的函数,将它们放在一个空白画布上,然后返回新制作的画布的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();
};

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

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

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

答案 1 :(得分:4)

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

  • 创建新画布
  • 获取其背景
  • 使用drawImage
  • 绘制所有其他画布
  • 从此新画布中提取最终图像

答案 2 :(得分:3)

尝试此示例希望它有助于查看 here

                //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"); 
                    });
                };