首页 > 解决方案 > 在 Canvas 上看不到任何东西。它就像白色背景

问题描述

最近尝试练习HTML canvas,但是JS无法在canvas上绘制任何东西。它只是全白。我不知道问题是什么。我的代码是这样的:

     <!DOCTYPE html>
     <html lang="en">
     <head>
       <meta charset="UTF-8">
       <title>Document</title>
       <style>
         canvas {border: 1px solid black;}
         body{margin: 0;}
       </style>
     </head>
     <body>
       <canvas id="myCanvas"></canvas>
       <script src="app.js"></script>
     </body>
     </html>

这是JS代码:

     const myCanvas = document.querySelector('#myCanvas');
         myCanvas.width = window.innerWidth;
         myCanvas.height = window.innerHeight;

     const ctx = myCanvas.getContext('2d');
         ctx.fillStyle('#000')
         ctx.fillRect(100, 100, 80, 80);

标签: javascriptcanvas

解决方案


fillStyle不是函数,是属性。所以你需要分配颜色代码

 ctx.fillStyle = '#000'

推荐阅读