javascript - 在 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);
解决方案
fillStyle
不是函数,是属性。所以你需要分配颜色代码
ctx.fillStyle = '#000'
推荐阅读
- python - 多个机器人文本以 python/flask/jquery 响应
- android - NullPointerException 从 PreferenceFragment 访问活动
- laravel-5.2 - 我正在尝试在 Laravel 5.7 中加载 css、js 和图像,但它不起作用
- angular - Angular: nested router-outlet is always ignored
- c# - 更改 .NET Framework 时,Visual Studio 中的数据库连接丢失
- html - 输入范围 onChangeComplate 事件
- ios - VNCoreMLFeatureValueObservation VS 类 VNClassificationObservation
- java - 用JAVA1.8编译gradle时找不到类com.google.ar.core.Frame/
- flutter - 如何使用 InheritedWidget 在我的 Flutter 应用程序中随处调用 void 函数
- prolog - 在递归调用中如何在 Prolog 中只打印一次