Javascript 不导入文件以在画布中工作的脚本

Javascript 不导入文件以在画布中工作的脚本,javascript,html,canvas,Javascript,Html,Canvas,html文件似乎没有导入javascript函数particle()以上载我在另一个文件中有脚本,我正在将其导入html文件,但它不起作用,可能是一些基本错误,我在这里无法理解! 如果你能修改这段代码,我将非常感激 var canvas=document.createElement(“canvas”); c=canvas.getContext(“2d”); var粒子={}; var particleIndex=0; var particleNum=15; //设置画布大小 canvas.wi

html文件似乎没有导入javascript函数particle()以上载我在另一个文件中有脚本,我正在将其导入html文件,但它不起作用,可能是一些基本错误,我在这里无法理解! 如果你能修改这段代码,我将非常感激

var canvas=document.createElement(“canvas”);
c=canvas.getContext(“2d”);
var粒子={};
var particleIndex=0;
var particleNum=15;
//设置画布大小
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
//将画布添加到主体
document.body.appendChild(画布);
//设计画布的样式
c、 fillStyle=“黑色”;
c、 fillRect(0,0,canvas.width,canvas.height);
函数粒子(){
这个.x=canvas.width/2;
这个.y=canvas.height/2;
this.vx=Math.random()*10-5;
this.vy=Math.random()*10-5;
这个重力=0.3;
particleIndex++;
粒子[particleIndex]=这个;
this.id=particleIndex;
这就是生命=0;
this.maxLife=Math.random()*30+60;
this.color=“hsla(“+parseInt(Math.random()*360,10)+”,90%,60%,0.5”;
}
Particle.prototype.draw=函数(){
this.x+=this.vx;
this.y+=this.vy;
if(Math.random()<0.1){
this.vx=Math.random()*10-5;
this.vy=Math.random()*10-5;
}
这就是生活;
如果(this.life>=this.maxLife){
删除粒子[this.id];
}
c、 fillStyle=this.color;
//c、 fillRect(this.x,this.y,5,10);
c、 beginPath();
c、 弧(这个.x,这个.y,2.5,0,2*Math.PI);
c、 填充();
};
setInterval(函数(){
//在带有黑色背景的画布上绘制之前的正常设置
c、 globalCompositeOperation=“源代码结束”;
c、 fillStyle=“rgba(0,0,0,0.5)”;
c、 fillRect(0,0,canvas.width,canvas.height);
对于(var i=0;i

不安的
window.onload=函数(){
粒子();
};
函数runParticles(){
var canvas=document.createElement(“canvas”);
c=canvas.getContext(“2d”);
var粒子={};
var particleIndex=0;
var particleNum=15;
//设置画布大小
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
//将画布添加到主体
document.body.appendChild(画布);
//设计画布的样式
c、 fillStyle=“黑色”;
c、 fillRect(0,0,canvas.width,canvas.height);
函数粒子(){
这个.x=canvas.width/2;
这个.y=canvas.height/2;
this.vx=Math.random()*10-5;
this.vy=Math.random()*10-5;
这个重力=0.3;
particleIndex++;
粒子[particleIndex]=这个;
this.id=particleIndex;
这就是生命=0;
this.maxLife=Math.random()*30+60;
this.color=“hsla(“+parseInt(Math.random()*360,10)+”,90%,60%,0.5”;
}
Particle.prototype.draw=函数(){
this.x+=this.vx;
this.y+=this.vy;
if(Math.random()<0.1){
this.vx=Math.random()*10-5;
this.vy=Math.random()*10-5;
}
这就是生活;
如果(this.life>=this.maxLife){
删除粒子[this.id];
}
c、 fillStyle=this.color;
//c、 fillRect(this.x,this.y,5,10);
c、 beginPath();
c、 弧(这个.x,这个.y,2.5,0,2*Math.PI);
c、 填充();
};
setInterval(函数(){
//在带有黑色背景的画布上绘制之前的正常设置
c、 globalCompositeOperation=“源代码结束”;
c、 fillStyle=“rgba(0,0,0,0.5)”;
c、 fillRect(0,0,canvas.width,canvas.height);
对于(var i=0;i

不安的
window.onload=()=>runParticles();

但是我想将脚本与windows.onload函数一起使用。我该怎么做?只需将JS封装在函数中并调用它即可