Html5多个画布,一页上有图像

时间:2016-03-29 13:19:48

标签: javascript php html5 canvas

我有这个PHP代码

 @for($i=1;$i<=$cate;$i++)
    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12" style="min-height: 412px" id="etalon">
            <canvas id="respondCanvas_{{$i}}" class="respondCanvas" style="position: absolute"></canvas>
            <div class="col-md-2"><h3>

                   </h3></div>
        </div>
    </div>
    <!-- /.row -->
    @endfor

和这个javascritp

var imageObj = new Image();
    imageObj.src = "/frontEnd/images/coupons/back1.png";


    for (var i=1;i<=2;i++) {
        var canvas = document.getElementById("respondCanvas_"+i);
        alert(i);
        var context = canvas.getContext('2d');

        imageObj.onload = function () {
            context.drawImage(imageObj, 0, 0);
        };

    }

我想创建每个动态行以拥有它的画布。 谁能帮我? 感谢

1 个答案:

答案 0 :(得分:0)

我设法解决了!不管怎样,谢谢!

$(document).ready( function(){
        var count = document.getElementsByClassName('respondCanvas').length;

    var imageObj = new Image();
    imageObj.src = "/frontEnd/images/coupons/back1.png";
        var canvas = new Array();
        var context = new Array();

        for (var i=1;i<=count;i++) {
        canvas[i] = document.getElementById("respondCanvas_"+i);


         context[i] = canvas[i].getContext('2d');



    }
        imageObj.onload = function () {
            for (var i=1;i<=count;i++) {
                context[i].drawImage(imageObj, 0, 0);

            }
        };