简体   繁体   English

画布不会绘制IE 11

[英]Canvas doesn't draw IE 11

I have a canvas code to draw a signature. 我有一个画布代码来绘制签名。 The code works perfectly fine with chrome and Firefox but does not draw at all on IE 11. 该代码可与chrome和Firefox完美配合,但在IE 11上完全无法使用。

My canvas is: 我的画布是:

<canvas id="signitureCanvas" style="border: 3px solid #000; cursor:crosshair; background-color:white;"></canvas>

My code is as below: 我的代码如下:

 var canvas = document.getElementById('signitureCanvas');
    var ctx = canvas.getContext('2d');
    var canvasWidth = 200;
    var canvasLength = 120;
    canvas.width = canvasWidth;
    canvas.height = canvasLength;
    var canvasx = $(canvas).offset().left;
    var canvasy = $(canvas).offset().top;
    var last_mousex = last_mousey = 0;
    var mousex = mousey = 0;
    var mousedown = false;
    var tooltype = 'draw';

    //Mousedown
    $(canvas).on('mousedown', function (e) {
        last_mousex = mousex = parseInt(e.clientX - canvasx);
        last_mousey = mousey = parseInt(e.clientY - canvasy);
        mousedown = true;
    });

    //Mouseup
    $(canvas).on('mouseup', function (e) {
        mousedown = false;
    });

    //Mousemove
    $(canvas).on('mousemove', function (e) {
        mousex = parseInt(e.clientX - canvasx);
        mousey = parseInt(e.clientY - canvasy);
        if (mousedown) {
            ctx.beginPath();
            if (tooltype == 'draw') {
                ctx.globalCompositeOperation = 'source-over';
                ctx.strokeStyle = 'black';
                ctx.lineWidth = 3;
            } else {
                ctx.globalCompositeOperation = 'destination-out';
                ctx.lineWidth = 10;
            }
            ctx.moveTo(last_mousex, last_mousey);
            ctx.lineTo(mousex, mousey);
            ctx.lineJoin = ctx.lineCap = 'round';
            ctx.stroke();
        }
        last_mousex = mousex;
        last_mousey = mousey;
    });

    function ClearCanvas() {
        var canvas = document.getElementById('signitureCanvas');
        var ctx = canvas.getContext('2d');
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    }

Is IE 11 in particular having problems? IE 11特别有问题吗?

Edit: 编辑:

I figured out the problem is with my Iframe: 我发现问题出在我的iframe上:

When height and width are set to 300 everything works fine: 将高度和宽度设置为300时,一切正常:

 <embed id="fred"  type="application/pdf" style="border:1px solid #666CCC" title="PDF in an i-Frame" src="@Model.FilePath" frameborder="1" scrolling="yes" height="300" width="300" />

When I set it to 1000, it won't work: 当我将其设置为1000时,它将不起作用:

 <embed id="fred"  type="application/pdf" style="border:1px solid #666CCC" title="PDF in an i-Frame" src="@Model.FilePath" frameborder="1" scrolling="yes" height="1000" width="1000" />

I believe it's something with the offset but I can't figure how to fix it. 我相信这与偏移量有关,但我不知道如何解决。

any help? 有什么帮助吗?

For further knowledge to whom might ask: 要进一步了解谁可能会问:

I have found this piece of code that works on all browsers, layerX and layerY are different for firefox browsers: 我发现这段代码可在所有浏览器上使用,对于firefox浏览器,layerX和layerY是不同的:

   var canvas = document.getElementById('signitureCanvas');
var ctx = canvas.getContext('2d');
var canvasWidth = 200;
var canvasLength = 120;
canvas.width = canvasWidth;
canvas.height = canvasLength;
var x = 0;
var y = 0;

function tool_pencil() {
    var tool = this;
    this.started = false;

    // This is called when you start holding down the mouse button
    // This starts the pencil drawing
    this.mousedown = function (ev) {
        ctx.beginPath();
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 3;
        ctx.lineJoin = ctx.lineCap = 'round';
        ctx.moveTo(x, y);
        tool.started = true;
    };

    // This function is called every time you move the mouse. Obviously, it only
    // draws if the tool.started state is set to true (when you are holding down
    // the mouse button)
    this.mousemove = function (ev) {
        if (tool.started) {
            ctx.lineTo(x, y);
            ctx.stroke();
        }
    };

    // This is called when you release the mouse button
    this.mouseup = function (ev) {
        if (tool.started) {
            tool.mousemove(ev);
            tool.started = false;
        }
    };
}

// The general-purpose event handler. This function just determines
// the mouse position relative to the <canvas> element
function ev_canvas(ev) {
    // Firefox
    if (ev.offsetX || ev.offsetX == 0) {
        x = ev.offsetX;
        y = ev.offsetY;
        // Opera
    } else if (ev.layerX || ev.layerX == 0) {
        x = ev.layerX;
        y = ev.layerX;
    }

    // Call the event handler of the tool
    var func = tool[ev.type];
    if (func) {
        func(ev);
    }
}

function ClearCanvas() {
    var canvas = document.getElementById('signitureCanvas');
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, canvas.width, canvas.height);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM