画布矩形得到然后消失javascript

时间:2015-01-23 04:35:24

标签: javascript canvas draw

我想要一个带有背景图像的画布,并在顶部绘制矩形。矩形在我的应用程序中称为hotSpots。他们被吸引到画布然后迅速消失。我怎么能让他们留下来?

appendSection()首先用appendPicture()附加一张图片,它只是将一个画布附加到div,然后在该函数运行之后,制作画布和上下文,然后为每个hotSpot,在这种情况下有3个,它会绘制一个矩形,然后它们会消失。

function appendSection(theSection, list) {
        list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>');
        if (theSection.picture) {
            appendPicture(list, theSection);
            var canvas = document.getElementById('assessmentImage');
            var ctx=canvas.getContext("2d");
             var img=new Image();
                img.onload = function() {
                    ctx.drawImage(img, 0, 0,canvas.width,canvas.height);
                }
                img.src = "data:image/jpeg;base64,"+ theSection.picture;
            if(theSection.allHotSpots.length > 0) {
                for( var x = 0; x < theSection.allHotSpots.length; x++) {
                    appendHotSpot(list, theSection.allHotSpots[x], theSection.thePicture, ctx);
                }
            }
        }
        appendSectionQuestions(theSection, list);
        if (theSection.allSubSections) {
            for (var x = 0; x < theSection.allSubSections.length; x++) {
                var theSectionA = theSection.allSubSections[x];
                appendSection(theSectionA, list);
            }
        }
    }

    function appendHotSpot(list, HotSpot, picture, ctx) {
        var imageWidth = document.getElementById('assessmentImage' + platform).clientWidth;
        var imageHeight = document.getElementById('assessmentImage' + platform).clientHeight;

        var xScale = imageWidth / picture.xSize;
        var yScale = imageHeight / picture.ySize;

        HotSpot.topLeft = [Math.round(HotSpot.topLeft[0] * xScale), Math.round(HotSpot.topLeft[1] * yScale)];
        HotSpot.bottomRight = [Math.round(HotSpot.bottomRight[0] * xScale), Math.round(HotSpot.bottomRight[1] * yScale)];


        var rect = {x1: HotSpot.topLeft[0], y1: HotSpot.topLeft[1], x2: HotSpot.bottomRight[0], y2: HotSpot.bottomRight[1]};



        ctx.fillStyle = "#FF0000";
        ctx.fillRect(rect.x1, rect.y1, rect.x2, rect.y2);

        //addRect("blue", rect);

    }

    function appendPicture(list, theSection) {


            list.append('<div id="wrapper' + platform + '" style="width:100%; text-align:center">\
                <canvas class="assessmentImageSmall" style="width:100%;" id="assessmentImage' + platform + '" align="middle" ></canvas>\
                <!--<p style="color:#666;" id="imageInstruction">Tap image to enlarge.</p>-->\
                </div>');
            $("#wrapper").kendoTouch({
                                         tap: function (e) {
                                             switchImage();
                                         }
                                     });
    }

1 个答案:

答案 0 :(得分:0)

看起来你的rects正在被绘制,然后图像(异步加载)最终被加载,然后在rects之上绘制 - 使它们消失。

if(theSection.allHotSpots.length > 0放在onload中,这样图像就不会破坏这些图像。