简体   繁体   English

为什么不进行画布绘制?

[英]Why won't canvas draw?

I've looked over this code every way I can, and I'm not seeing what's tripping me up. 我已经尽我所能查看了这段代码,但看不到是什么使我烦恼。 This is my first night looking into canvas, and my first night messing with JS outside of Codecademy, so I'm sure it's something fundamental that I missed at some point. 这是我进入画布的第一个夜晚,也是我在Codecademy之外弄乱JS的第一个夜晚,所以我确定这是我有时会错过的基本知识。

function draw() {
    var canvas = document.getElementById('canvas');
    if (canvas.getContext) {
        var ctx = canvas.getContext('2d');

        ctx.beginPath();
        ctx.moveTo(75, 50);
        ctx.lineTo(100, 75);
        ctx.lineTo(100, 25);
        ctx.fill();
    }
}

Here's the fiddle 是小提琴

You forgot to call draw function. 您忘记了调用绘图功能。 Take a look here. 在这里看看

draw();

code looks like this 代码看起来像这样

function draw() {
  var canvas = document.getElementById('canvas');
  if (canvas.getContext){
    var ctx = canvas.getContext('2d');

    ctx.beginPath(); 
    ctx.moveTo(75,50);
    ctx.lineTo(100,75);
    ctx.lineTo(100,25);
    ctx.fill();
  }
}
draw();

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

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