IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> canvas粒子字体动效 -> 正文阅读

[JavaScript知识库]canvas粒子字体动效

在这里插入图片描述

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Canvas粒子汇聚文字动画特效</title>

<style>
body {
  padding: 0px;
  margin: 0px;
  background-color: #252525;
}

.hidden {
  display: none;
}

#spring-text {
  position: absolute;
}
</style>

</head>
<body>

<!-- 尝试移动鼠标并单击文本区域 -->
<div id="title-desktop" class="hidden">粒子字体效果</div>
<div id="title-mobile" class="hidden">Chinaz</div>

<canvas id="spring-text"></canvas>

<script>
// WRITTEN BY TRUMAN HEBERLE
var COLOR = "#52E6FF" 
var MESSAGE = document.getElementById("title-desktop").textContent;

var FONT_SIZE = 50;
var AMOUNT = 8000;
var SIZE = 1;
var INITIAL_DISPLACEMENT = 100;
var INITIAL_VELOCITY = 3;
var VELOCITY_RETENTION = 0.95;
var SETTLE_SPEED = 1;
var FLEE_SPEED = 1;
var FLEE_DISTANCE = 20;
var FLEE = true;
var SCATTER_VELOCITY = 3;
var SCATTER = true;
const canvas = document.getElementById("spring-text");
let canvasWidth = 600;
let canvasHeight = 80;
const ctx = canvas.getContext("2d");

var POINTS = [];
var MOUSE = {
  x: 0,
  y: 0
}

function Point(x,y,r,g,b,a) {
  var angle = Math.random() * 6.28;
  this.dest_x = x;
  this.dest_y = y;
  this.original_r = r;
  this.original_g = g;
  this.original_a = a;
  this.x = canvas.width/2 - x + (Math.random() - 0.5) * INITIAL_DISPLACEMENT;
  this.y = canvas.height/2 - y + (Math.random() - 0.5) * INITIAL_DISPLACEMENT;
  this.velx = INITIAL_VELOCITY * Math.cos(angle);
  this.vely = INITIAL_VELOCITY * Math.sin(angle);
  this.target_x = canvas.width/2 - x;
  this.target_y = canvas.height/2 - y;
  this.r = r;
  this.g = g;
  this.b = b;
  this.a = a;
  
  this.getX = function() {
    return this.x;
  }
  
  this.getY = function() {
    return this.y;
  }
  
  this.resetTarget = function () {
    this.target_x = canvas.width/2 - this.dest_x;
    this.target_y = canvas.height/2 - this.dest_y;
  }
  
  this.fleeFrom = function(x, y) {
    this.velx -= ((MOUSE.x - this.x) * FLEE_SPEED / 10);
    this.vely -= ((MOUSE.y - this.y) * FLEE_SPEED / 10);
  }
  
  this.settleTo = function(x, y) {
    this.velx += ((this.target_x - this.x) * SETTLE_SPEED / 100);
    this.vely += ((this.target_y - this.y) * SETTLE_SPEED / 100);
    this.velx -= this.velx * (1-VELOCITY_RETENTION);
    this.vely -= this.vely * (1-VELOCITY_RETENTION);
  }
  
  this.scatter = function() {
    var unit = this.unitVecToMouse();
    var vel = SCATTER_VELOCITY * 10 * (0.5 + Math.random() / 2);
    this.velx = -unit.x * vel;
    this.vely = -unit.y * vel;
  }
  
  this.move = function() {
    if (this.distanceToMouse() <= FLEE_DISTANCE) {
      this.fleeFrom(MOUSE.x, MOUSE.y);
    }
    else {
      this.settleTo(this.target_x, this.target_y);
    }
    
    if (this.x + this.velx < 0 || this.x + this.velx >= canvas.width) {
      this.velx *= -1;
    }
    if (this.y + this.vely < 0 || this.y + this.vely >= canvas.height) {
      this.vely *= -1;
    }
    
    this.x += this.velx;
    this.y += this.vely;
  }
  
  this.distanceToTarget = function() {
    return this.distanceTo(this.target_x, this.target_y);
  }
  
  this.distanceToMouse = function() {
    return this.distanceTo(MOUSE.x, MOUSE.y);
  }
  
  this.distanceTo = function(x, y) {
    return Math.sqrt((x - this.x)*(x - this.x) + (y - this.y)*(y - this.y));
  }
  
  this.unitVecToTarget = function() {
    return this.unitVecTo(this.target_x, this.target_y);
  }
  
  this.unitVecToMouse = function() {
    return this.unitVecTo(MOUSE.x, MOUSE.y);
  }
  
  this.unitVecTo = function(x, y) {
    var dx = x - this.x;
    var dy = y - this.y;
    return {
      x: dx / Math.sqrt(dx*dx + dy*dy),
      y: dy / Math.sqrt(dx*dx + dy*dy)
    };
  }
}


if (FLEE) {
  window.addEventListener("mousemove", function(event) {
    MOUSE.x = event.clientX;
    MOUSE.y = event.clientY;
  });
}

if (SCATTER) {
  window.addEventListener("click", function(event) {
    MOUSE.x = event.clientX;
    MOUSE.y = event.clientY;
    for (var i=0; i<POINTS.length; i++) {
      POINTS[i].scatter();
    }
  });
}

function initCanvas() {
  canvas.width  = canvasWidth;
  canvas.height = canvasHeight;
}

function adjustText() {
  ctx.fillStyle = COLOR;
  ctx.textBaseline = "middle";
  ctx.textAlign = "center";
  ctx.font = FONT_SIZE + "px Arial";
  ctx.fillText(MESSAGE, canvas.width/2, canvas.height/2);
  var textWidth = ctx.measureText(MESSAGE).width;
  if (textWidth == 0) {
    return;
  }
  var minX = canvas.width/2 - textWidth/2;
  var minY = canvas.height/2 - FONT_SIZE/2;
  var data = ctx.getImageData(minX, minY, textWidth, FONT_SIZE).data;
  var isBlank = true;
  for (var i=0; i<data.length; i++) {
    if (data[i] != 0) {
      isBlank = false;
      break;
    }
  }
  
  if (!isBlank) {
    var count = 0;
    var curr = 0;
    var num = 0;
    var x = 0;
    var y = 0;
    var w = Math.floor(textWidth);
    POINTS = [];
    while (count < AMOUNT) {
      while (curr == 0) {
        num = Math.floor(Math.random() * data.length);
        curr = data[num];
      }
      num = Math.floor(num / 4);
      x = w/2 - num%w;
      y = FONT_SIZE/2 - Math.floor(num/w);
      POINTS.push(new Point(x,y,data[num*4],data[num*4 + 1],data[num*4 + 2],data[num*4 + 3]));
      curr = 0;
      count++;
    }
  }
}

function init() {
  initCanvas()
  adjustText()
  window.requestAnimationFrame(animate);
}

function animate() {
  update();
  draw();
}

function update() {
  var point;
  for (var i=0; i<POINTS.length; i++) {
    point = POINTS[i];
    point.move();
  }
}

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  
  var point;
  for (var i=0; i<POINTS.length; i++) {
    point = POINTS[i];
    ctx.fillStyle = "rgba("+point.r+","+point.g+","+point.b+","+point.a+")";
    ctx.beginPath();
    ctx.arc(point.getX(),point.getY(),SIZE,0,2*Math.PI);
    ctx.fill();
  }
  
  window.requestAnimationFrame(animate);
}

init();
</script>

</body>
</html>
  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-03-11 22:04:44  更:2022-03-11 22:08:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:42:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码