javascript - this.draw 不是函数,不会正确运行代码
问题描述
我正在使用此代码为我的学校 webdev 课程完成一个项目。我一直盯着这个几个小时尝试不同的东西,看不出我哪里出错了。这里的任何帮助都会很热。
//Initial Setup
var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');
canvas.width = innerWidth;
canvas.height = innerHeight;
//Variables
const mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};
const colors = ['#2185C5', '#7ECEFD', '#FFF6E5', '#FF7F66'];
// Event Listeners
addEventListener('mousemove', event => {
mouse.x = event.clientX;
mouse.y = event.clientY;
});
addEventListener('resize', () => {
canvas.width = innerWidth
canvas.height = innerHeight
init();
})
//Utility Functions
function randomIntFromRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomColor(colors) {
return colors [Math.floor(Math.random() * colors.length)];
}
// Objects
function Particle(x, y, radius, color) {
this.x = x
this.y = y
this.radius = radius
this.color = color;
this.radians = Math.random() * Math.PI * 2;
this.velocity = 0.05;
this.distanceFromCenter = randomIntFromRange(50, 120);
this.lastMouse = {x: x, y: y};
this.update = () => {
const lastPoint = {x: this.x, y: this.y};
//move points over time
this.radians += this.velocity;
//Drag Effect
this.lastMouse.x += (mouse.x - this.lastMouse.x) * 0.05;
this.lastMouse.y += (mouse.y - this.lastMouse.y) * 0.05;
//Circuler Motion
this.x = this.lastMouse.x + Math.cos(this.radians) * this.distanceFromCenter;
this.y = this.lastMouse.y + Math.sin(this.radians) * this.distanceFromCenter;
this.draw(lastPoint);
};
}
this.draw = lastPoint => {
c.beginPath();
c.strokestyle = this.color;
c.lineWidth = this.radius;
c.moveTo(lastPoint.x, lastPoint.y);
c.lineTo(this.x, this.y);
c.stroke();
c.closePath();
}
// Implementation
let particles;
function init() {
particles = []
for (let i = 0; i < 10; i++) {
const radius = (Math.random() * 2) + 1;
particles.push(new Particle(canvas.width / 2, canvas.height / 2, radius, randomColor(colors)));
}
}
// Animation Loop
function animate() {
requestAnimationFrame(animate);
c.fillstyle = 'rgba (255, 255, 255, 0.05)';
c.fillRect(0, 0, canvas.width, canvas.height)
particles.forEach(particle => {
particle.update();
})
}
init();
animate();
我一直遇到...“未捕获的 TypeError:this.draw 不是函数”的错误。我一直在绞尽脑汁试图完成这项工作。有人可以再看看这个以减轻压力吗?谢谢!
这是我运行 JS 的 HTML。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEST</title>
<style type="text/css">
canvas {
border: 1px solid black;
}
body {
margin: 0;
}
</style>
</head>
<body>
<canvas></canvas>
<script src="java/project.js"></script>
</body>
</html>
解决方案
这听起来像是一个愚蠢的答案,但是您是否尝试过将 this.draw 函数重命名为 draw()?看到 this.draw() 函数,函数名称中的“this”似乎没有理由。并且不要忘记将 this.draw(lastPoint) 更改为仅 draw(lastPoint)。我真的希望这会有所帮助。你的代码真的很漂亮!
推荐阅读
- java - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“”
- java - 单击片段内回收视图中的按钮时显示自定义对话框片段
- gmail - 防病毒软件正在阻止 nodemailer - 错误:证书链中的自签名证书
- arduino - 我真的搞砸了!我需要撤消一些笨拙的脚本
- apache-spark - Spark,如何打印查询?
- python - 无法弄清楚如何让 BS4 检索 /videos 页面上的 youtube 观看次数
- python - groupby pandas python的自定义排序功能
- javascript - 为什么我输入大写方法后空格键不起作用?
- sql-server - 如何创建索引以加快数据检索
- apache-spark - spark sql 无法识别 hive 分区列