[removed] Drawing rectangle on canvas doesn't work on IE

后端 未结 3 874
感情败类
感情败类 2021-01-16 09:36

I have a web application where you can draw a rectangle on a canvas. I use two canvas elements: one for the preview while drawing and another one laying exactly under the ot

3条回答
  •  长发绾君心
    2021-01-16 10:25

    There is Nothing Wrong with the Code and nothing Wrong With IE 9,What you missed is a l'le concept ,

    addEventListener() didn't work For IE instead you have to use attachEvent() for it to make your Code run in IE

    //For your code to work in IE
    if (!canvas2.addEventListener) {
    canvas2.attachEvent("onclick", CanvasFunction);
    }
    //for rest of the Browser
    else {
    canvas2.addEventListener("click", CanvasFunction, false);
    }
    
    function CanvasFunction(evt)
    {
    var rect = canvas2.getBoundingClientRect();
        x = evt.clientX - rect.left;
        y = evt.clientY - rect.top;
        if (clicked) {
            canvas2.width = canvas2.width;
            console.log(xStart);
    
            // add beginPath so previous context2.rect's are dismissed
            context2.beginPath();
    
            context2.rect(xStart, yStart, x - xStart, y - yStart);
            context2.stroke();
        }
    }
    

    Playing with Canvas ,remember IE doesn't support addEventListners ..Enjoy Coding

提交回复
热议问题