只要干不死就往死里干
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>
控制台报错,如下:
请问要怎么改???
var context = canvas.getContext("2d'"); 是声明的是context,cxt.beginPath()中ctx哪来的
canvas的style没有设置display:block
去掉一个,
改成 piece.p[i]. y
canvas.getContext("2d'"); 2d多了个'
炫丽的倒计时效果Canvas绘图与动画基础
96746 学习 · 1000 问题
相似问题