擦除粒子文本时如何加载下一个字符串?

如何解决擦除粒子文本时如何加载下一个字符串?

我是 vanilla js 和 webgl 内容的新手。我创建了一个在鼠标悬停时会扭曲的粒子文本。并且它们的粒子之间用一条线相连。代码如下。

const canvas = document.getElementById("canvas1");
const dpr = window.devicePixelRatio;
//console.log(dpr);
const ctx = canvas.getContext("2d");
ctx.scale(dpr,dpr);

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particleArray = [];
let adjustX = 10;
let adjustY = 0;
//handle mouse
const mouse = {
  x: null,y: null,radius: 30,};
window.addEventListener("mousemove",function (event) {
  mouse.x = event.x;
  mouse.y = event.y;
  //console.log(mouse.x,mouse.y);
});


ctx.fillStyle = "white";
ctx.font = "35px verdana";
ctx.fillText("aman ",30);

const textCoordinates = ctx.getimageData(0,100,100);

class Particle {
  constructor(x,y) {
    this.x = x + Math.random() * 6 - 3;
    this.y = y + Math.random() * 6 - 3;
    this.size = 1;
    this.baseX = this.x;
    this.baseY = this.y;
    this.density = Math.random() * 10 + 50;
  }
  draw() {
    ctx.fillStyle = "white";
    ctx.beginPath();
    ctx.arc(this.x,this.y,this.size,Math.PI * 2);
    ctx.closePath();
    ctx.fill();
  }
  //distance between mouse and particle
  update() {
    let dx = mouse.x - this.x;
    let dy = mouse.y - this.y;
    let distance = Math.sqrt(dx * dx + dy * dy);
    let forceDirectionX = dx / distance;
    let forceDirectionY = dy / distance;
    let maxdistance = mouse.radius;
    let force = (maxdistance - distance) / maxdistance;
    let directionX = forceDirectionX * force * this.density;
    let directionY = forceDirectionY * force * this.density;

    if (distance < mouse.radius) {
      this.x -= directionX;
      this.y -= directionY;
    } else {
      if (this.x !== this.baseX) {
        let dx = this.x - this.baseX;
        this.x += dx / 10;
      }
      if (this.y !== this.baseY) {
        let dy = this.y - this.baseY;
        this.y += dy / 10;
      }
    }
  }
}

//console.log(textCoordinates);
function init() {
  particleArray = [];
  for (let y = 0,y2 = textCoordinates.height; y < y2; y++) {
    for (let x = 0,x2 = textCoordinates.width; x < x2; x++) {
      if (
        textCoordinates.data[y * 4 * textCoordinates.width + x * 4 + 3] > 128
      ) {
        let positionX = x + adjustX;
        let positionY = y + adjustY;
        particleArray.push(new Particle(positionX * 4,positionY * 4));
      }
    }
  }
}
init();
//console.log(particleArray);

function animate() {
  ctx.clearRect(0,canvas.width,canvas.height);
  for (let i = 0; i < particleArray.length; i++) {
    particleArray[i].draw();
    particleArray[i].update();
  }
  connect();
  requestAnimationFrame(animate);
}
animate();
function connect() {
  let opacityValue = 1;
  for (let a = 0; a < particleArray.length; a++) {
    for (let b = a; b < particleArray.length; b++) {
      let dx = particleArray[a].x - particleArray[b].x;
      let dy = particleArray[a].y - particleArray[b].y;
      let distance = Math.sqrt(dx * dx + dy * dy);

      if (distance < 14) {
        opacityValue = 0.5;
        ctx.strokeStyle = "rgb(200,150,160," + opacityValue + ")";
        ctx.linewidth = 1;
        ctx.beginPath();
        ctx.moveto(particleArray[a].x,particleArray[a].y);
        ctx.lineto(particleArray[b].x,particleArray[b].y);
        ctx.stroke();
      }
    }
  }
}

现在我想做的是,当我擦除这个粒子文本时,必须出现下一个文本。就像在这个网站上一样 https://animal.cc/ 。 任何人都可以帮助我:))

解决方法

(这与three.js无关)
当您确定“此粒子文本”已被删除时,您可以在 animate() 中执行此操作:

ctx.fillText("NEW ",30);
textCoordinates = ctx.getImageData(0,100,100);
init();

编辑:评论代码(下方)

  for (let y = 0,y2 = textCoordinates.height; y < y2; y++) {
    for (let x = 0,x2 = textCoordinates.width; x < x2; x++) {

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?