typescript - 使用 React Typescript 飞过太空星星动画的 HTML5 画布
问题描述
我正在尝试使用 react typescript 制作星形动画,但我是 react ts 的新手,这是参考链接,这个动画是用 canvas 和 js 制作的
在我的代码中,我添加了相同的方式,但出现了太多错误
import React, { useRef, useEffect } from 'react';
const Canvas = () => {
let canvasRef = useRef<HTMLCanvasElement | null>(null);
let canvasCtxRef = React.useRef<CanvasRenderingContext2D | null>(null);
useEffect(() => {
// Initialize
if (canvasRef.current) {
var getUrlParameter = function getUrlParameter(sParam: any) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var field = canvasRef.current;
var f = canvasRef.current.getContext('2d');
var stars = {};
var starIndex = 0;
var numStars = 0;
var acceleration:any = 1;
var starsToDraw:any = (field.width * field.height) / 200;
if (getUrlParameter("stars")) {
starsToDraw = getUrlParameter("stars");
}
if (getUrlParameter("accel")) {
acceleration = getUrlParameter("accel");
}
function Star() {
this.X = field.width / 2;
this.Y = field.height / 2;
this.SX = Math.random() * 10 - 5;
this.SY = Math.random() * 10 - 5;
var start = 0;
if (field.width > field.height)
start = field.width;
else
start = field.height;
this.X += this.SX * start / 10;
this.Y += this.SY * start / 10;
this.W = 1;
this.H = 1;
this.age = 0;
this.dies = 500;
starIndex++;
stars[starIndex] = this;
this.ID = starIndex;
this.C = "#ffffff";
}
Star.prototype.Draw = function () {
this.X += this.SX;
this.Y += this.SY
this.SX += this.SX / (50 / acceleration);
this.SY += this.SY / (50 / acceleration);
this.age++;
if (this.age == Math.floor(50 / acceleration) || this.age == Math.floor(150 / acceleration) || this.age == Math.floor(300 / acceleration)) {
this.W++;
this.H++;
}
if (this.X + this.W < 0 || this.X > field.width ||
this.Y + this.H < 0 || this.Y > field.height) {
delete stars[this.ID];
numStars--;
}
f.fillStyle = this.C;
f.fillRect(this.X, this.Y, this.W, this.H);
}
field.width = window.innerWidth;
field.height = window.innerHeight;
function draw() {
if (field.width != window.innerWidth)
field.width = window.innerWidth;
if (field.height != window.innerHeight)
field.height = window.innerHeight;
// Play with the "a" value to create streams...it's fun!
f.fillStyle = "rgba(0, 0, 0, 0.8)";
f.fillRect(0, 0, field.width, field.height);
for (var i = numStars; i < starsToDraw; i++) {
new Star();
numStars++;
}
for (var star in stars) {
stars[star].Draw();
}
}
// Original timing of the screensaver
setInterval(draw, 40);
}
}, []);
return <canvas ref={canvasRef}></canvas>;
}
export default Canvas;
收到太多错误我尝试HTML
使用 react typescript 搜索有关画布的解决方案,但是如果有人知道如何解决此问题,则只有少数可用教程无法帮助我,请帮助我...谢谢:)
解决方案
推荐阅读
- css - 无法更改 PrimeFaces 消息样式
- javascript - 清除来自 vuetify 的自动完成建议
- git - 无法提交对 Github 页面的更改
- php - 如何获取所有 Laravel 用户会话?
- javascript - 自动将控制器添加到路由器(无需路由)是否存在安全或性能问题?
- java - 如何使用现有的基类对象构造派生类
- python - 如何使用 joblib 并行后端来拟合 LightGBM?
- javascript - 如何只允许将项目放置在 HTML/JavaScript 中的某个位置?
- javascript - 使用 Sequelize 获取关系计数为零的实例
- javascript - 我正在尝试在 findAll 中包含一个有很多关联,但出现错误