将多个画布转换为html5中的dataURL [英] convert multiple canvases to dataURL in html5

查看:107
本文介绍了将多个画布转换为html5中的dataURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to join multiple canvases to make a single image. So is there any method to covert more than one canvases to toDataURL to make a single image?

推荐答案

试试这个例子希望它有助于看到这里

try this example hope it will help see 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"); 
                    });
                };

这篇关于将多个画布转换为html5中的dataURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆