画布显示不出来,对照了几遍还是不知道错在哪里

来源:2-2 绘制直线、多边形和七巧板

只要干不死就往死里干

2018-11-13 14:38

<!DOCTYPE html>

<html lang="en">


<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>


<body>

<canvas id="box" style="border:1px solid #aaa;display: block;margin:50px auto;" width="800" height="800"> 你的浏览器不支持canvas,请升级你的浏览器</canvas>


<script>

var tangram = [

{ p: [{ x: 0, y: 0 }, { x: 800, y: 0 }, { x: 400, y: 400 }], color: "#caff67" },

{ p: [{ x: 0, y: 0 }, { x: 400, y: 400 }, { x: 0, y: 800 }], color: "#61b7d2" },

{ p: [{ x: 800, y: 0 }, { x: 800, y: 400 }, { x: 600, y: 600 }, , { x: 600, y: 200 }], color: "#ff5063" },

{ p: [{ x: 600, y: 200 }, { x: 600, y: 600 }, { x: 400, y: 400 }], color: "#feea13" },

{ p: [{ x: 400, y: 400 }, { x: 600, y: 600 }, { x: 400, y: 800 }, { x: 200, y: 600 }], color: "#a597c2" },

{ p: [{ x: 200, y: 600 }, { x: 400, y: 800 }, { x: 0, y: 800 }], color: "#ff9ccf" },

{ p: [{ x: 400, y: 800 }, { x: 800, y: 800 }, { x: 400, y: 800 }], color: "#fdc521" }


];

window.onload = function () {

console.log(tangram);

var canvas = document.getElementById("box");

var context = canvas.getContext("2d'");

for (var i = 0; i < tangram.length; i++) {

draw(tangram[i], context)

}

}

function draw(piece, cxt) {

cxt.beginPath();

cxt.moveTo(piece.p[0].x, piece.p[0].y);

for (var i = 1; i < piece.p.length; i++){

cxt.lineTo(piece.p[i].x, piece.p[i], y);

}

cxt.closePath();

cxt.fillStyle = piece.color;

cxt.fill();

}

</script>

</body>


</html>



控制台报错,如下:

https://img4.mukewang.com/5bea712c0001537a03960120.jpg

请问要怎么改???

写回答 关注

4回答

  • 随风1990
    2019-04-03 11:03:20

    var context = canvas.getContext("2d'"); 是声明的是context,cxt.beginPath()中ctx哪来的

  • 专注程序不可自拔
    2018-12-05 15:00:59

    canvas的style没有设置display:block

  • asteriaGirl
    2018-11-13 15:57:58

    https://img4.mukewang.com/5bea83db0001619306540045.jpg去掉一个,

    https://img1.mukewang.com/5bea83f4000161e003590061.jpg 改成 piece.p[i]. y


  • 慕用3483772
    2018-11-13 15:54:21

    canvas.getContext("2d'");   2d多了个'

炫丽的倒计时效果Canvas绘图与动画基础

学习HTML5中最激动人心的技术Canvas,彻底释放自己的创造力

96746 学习 · 1000 问题

查看课程

相似问题