javascript - Why can't I draw a straight line?
阿神
阿神 2017-06-26 10:52:49
0
2
710

Why no straight line appears?

<canvas id="myCanvas" width="400" height="200">
canvas {
    border: 1px dashed black;
}

    window.onload = function() {
        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
        context.lineWidth = 10;
        context.moveTo(0, 50);
        context.lineTo(400, 150);
        context.stroke();
    }
    
    

Use chrome firefox to open jsfiddle respectively. No straight line appears?

阿神
阿神

闭关修行中......

reply all(2)
黄舟

Looking at the style of the screenshot, I think the questioner used jsfiddle. I ran it normally on runjs. I tried jsfiddle and found out why it didn’t work.

It seems that jsfiddle wraps user-defined js in window.onload. As shown in the screenshot, it is the outer onload that is called after the page is loaded, and reassigning onload in the inner layer does not help.

Remove window.onload and the jsfiddle preview will be normal...

仅有的幸福

I have tested it on chrome and ieedge, and both can produce straight lines.
There is nothing wrong with your code
You can look at other reasons

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!