IE browser failed to clear canvas canvas

  • use canvas to draw the image first
  • locate several div, on the image to trigger events
  • when an event is triggered, the box and connection of the message description are displayed
  • div, is not affected by the frame of information description
  • connections are drawn by canvas and can be cleared in both Google and Firefox when cleared, but not in IE browsers

IE

//
$(function () {
    var $my_canvas = $("-sharpmyCanvas");
    var my_canvas = $my_canvas[0];
    var context = my_canvas.getContext("2d");
    preImage("map.jpg", function () {
        context.drawImage(this, 0, 0, 1168, 704);
    });
});
//
function preImage(url, callback) {
    var img = new Image(); //Image  
    img.src = url;
    if (img.complete) { //   
        callback.call(img);
        return; // onload  
    }
    img.onload = function () { //callback  
        callback.call(img); //thisImage  
    };
}
//
function drawline(that) {
    var $my_canvas = $("-sharpmyCanvas");
    var my_canvas = $my_canvas[0];
    var contain = document.getElementById("contain");
    var context = my_canvas.getContext("2d");
    //1IE
    contain.removeChild(my_canvas);
    context.clearRect(0, 0, 1168, 704);
    contain.appendChild(my_canvas);
    //2IE
    //my_canvas.height = my_canvas.height;
    preImage("map.jpg", function () {
        context.drawImage(this, 0, 0, 1168, 704);
    });
    //
    var startx = that.offset().left + that.width() / 2 - 10;
    var starty = that.offset().top + that.height() - 10;
    //
    var endx = that.offset().left + that.width() / 2 - 10
    var endy = that.offset().top + that.height() + 40

    //
    context.moveTo(startx, starty); //
    context.lineTo(endx, endy); //
    context.lineWidth = 5; //
    context.strokeStyle = "red" //
    context.stroke(); //

    //
    var prev = that.prev();
    console.log(prev);
    if (prev.offset()) {
        var startxp = prev.offset().left + prev.width() / 2 - 10;
        var startyp = prev.offset().top + prev.height() - 10;
        //
        context.moveTo(startxp, startyp); //
        context.lineTo(endx, endy); //
        context.lineWidth = 5; //
        context.strokeStyle = "red" //
        context.stroke(); //
    }
    //
    var next = that.next();
    if (next.offset()) {
        var startxn = next.offset().left + next.width() / 2 - 10;
        var startyn = next.offset().top + next.height() - 10;
        //
        context.moveTo(startxn, startyn); //
        context.lineTo(endx, endy); //
        context.lineWidth = 5; //
        context.strokeStyle = "red" //
        context.stroke(); //
    }
}
//
function drawbox(that) {
    $(".block").show();
    var blockleft = that.offset().left + that.width() / 2 - $(".block").width() / 2;
    var blocktop = that.offset().top + that.height() + 50;
    $(".block").css({
        "left": blockleft + "px",
        "top": blocktop + "px"
    });
}
//div
$(".zd37").on("click", function () {
    var that = $(this);
    drawline(that);
    drawbox(that);
})
$(".zd40").on("click", function () {
    var that = $(this);
    drawline(that);
    drawbox(that);
})
$(".zd41").on("click", function () {
    var that = $(this);
    drawline(that);
    drawbox(that);
})
Sep.06,2021

  • changed the way of thinking, putting the initialization of canvas to the global, and writing the trigger event to a function.
  • this clears the contents of the canvas canvas successfully, but I don't know what distinguishes this method from the previous one.
  • the above method, or ask the boss to advise, what is wrong.
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
preImage("map.jpg", function () {
    ctx.drawImage(this, 0, 0, 1168, 704);
});

$(".position").click(function () {
    var that = $(this);
    draw(that);
});
$(".clear").click(function () {
    cc();
    preImage("map.jpg", function () {
        ctx.drawImage(this, 0, 0, 1168, 704);
    });
});

function preImage(url, callback) {
    var img = new Image(); //Image  
    img.src = url;
    if (img.complete) { //   
        callback.call(img);
        return; // onload  
    }
    img.onload = function () { //callback  
        callback.call(img); //thisImage  
    };
}

function cc() {
    ctx.clearRect(0, 0, c.width, c.height);
}

function draw(that) {
    cc();
    preImage("map.jpg", function () {
        ctx.drawImage(this, 0, 0, 1168, 704);
    });

    //
    var startx = that.offset().left + that.width() / 2 - 10;
    var starty = that.offset().top + that.height() - 10;
    //
    var endx = that.offset().left + that.width() / 2 - 10
    var endy = that.offset().top + that.height() + 40
    //
    ctx.lineWidth = 5; //
    ctx.strokeStyle = "red" //

    //
    ctx.beginPath();
    ctx.moveTo(startx, starty);
    ctx.lineTo(endx, endy);
    ctx.stroke(); //

    //
    var prev = that.prev();
    if (prev.offset()) {
        var startxp = prev.offset().left + prev.width() / 2 - 10;
        var startyp = prev.offset().top + prev.height() - 10;
        //
        ctx.beginPath();
        ctx.moveTo(startxp, startyp); //
        ctx.lineTo(endx, endy); //
        ctx.stroke(); //
    }
    //
    var next = that.next();
    if (next.offset()) {
        var startxn = next.offset().left + next.width() / 2 - 10;
        var startyn = next.offset().top + next.height() - 10;
        //
        ctx.beginPath();
        ctx.moveTo(startxn, startyn); //
        ctx.lineTo(endx, endy); //
        ctx.stroke(); //
    }

    $(".block").show();
    var blockleft = that.offset().left + that.width() / 2 - $(".block").width() / 2;
    var blocktop = that.offset().top + that.height() + 50;
    $(".block").css({
        "left": blockleft + "px",
        "top": blocktop + "px"
    });

}
Menu