Home  >  Q&A  >  body text

javascript - Can't draw a straight line to a specified position on a div?

<p id="myCanvas"></p>
#myCanvas {
    width:100px;height:100px;
    border: 1px dashed black;
}

        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
        context.lineWidth = 10;
        context.moveTo(0, 50);
        context.lineTo(50, 50);
        context.stroke();
        
        canvas的用法是否仅仅对canvas标签起作用?
        上面的代码无法在普通p内绘制直线,将p修改成canvas就可以。
        

欧阳克欧阳克2647 days ago726

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:54:43

    How can p use canvas api? Replace p with canvas element

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-26 10:54:43

    Canvas and p are not the same thing at all. You definitely can’t use canvas’s API!

    reply
    0
  • Cancelreply