html 粒子效果文字特效

这篇具有很好参考价值的文章主要介绍了html 粒子效果文字特效。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

有两个代码如下:

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML粒子文字动画特效</title>
</head>

<body style="background-color:#000">

<div style="text-align:center;clear:both">
<script src="/gg_bd_ad_720x90.js" type="text/javascript"></script>
<script src="/follow.js" type="text/javascript"></script>
</div>
      <iframe frameborder="0" scrolling="no" src="index2.html" width="100%" height="500px"></iframe>
</body>
</html>

index2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML粒子文字动画特效</title>
</head>

<body>

<script type="text/javascript">
BLUR = false;

PULSATION = true;
PULSATION_PERIOD = 600;
PARTICLE_RADIUS = 4;

/* disable blur before using blink */

BLINK = false;

GLOBAL_PULSATION = false;

QUALITY = 2; /* 0 - 5 */

/* set false if you prefer rectangles */
ARC = true;

/* trembling + blur = fun */
TREMBLING = 0; /* 0 - infinity */

FANCY_FONT = "Arial";

BACKGROUND = "#000";

BLENDING = true;

/* if empty the text will be a random number */
var TEXT;
num = 0;
TEXTArray = ["C", "S", "D", "N"];

QUALITY_TO_FONT_SIZE = [10, 12, 40, 50, 100, 350];
QUALITY_TO_SCALE = [20, 6, 4, 2, 0.9, 0.5];
QUALITY_TO_TEXT_POS = [10, 20, 60, 100, 370, 280];


window.onload = function () {
	document.body.style.backgroundColor = BACKGROUND;

	var canvas = document.getElementById("canvas");
	var ctx = canvas.getContext("2d");

	var W = canvas.width;
	var H = canvas.height;

	var tcanvas = document.createElement("canvas");
	var tctx = tcanvas.getContext("2d");
	tcanvas.width = W;
	tcanvas.height = H;

	total_area = W * H;
	total_particles = 928;
	single_particle_area = total_area / total_particles;
	area_length = Math.sqrt(single_particle_area);

	var particles = [];
	for (var i = 1; i <= total_particles; i++) {
		particles.push(new particle(i));
	}

	function particle(i) {
		this.r = Math.round(Math.random() * 255 | 0);
		this.g = Math.round(Math.random() * 255 | 0);
		this.b = Math.round(Math.random() * 255 | 0);
		this.alpha = 1;

		this.x = (i * area_length) % W;
		this.y = (i * area_length) / W * area_length;

		/* randomize delta to make particles sparkling */
		this.deltaOffset = Math.random() * PULSATION_PERIOD | 0;

		this.radius = 0.1 + Math.random() * 2;
	}

	var positions = [];

	function new_positions() {

		TEXT = TEXTArray[num];

		if (num < TEXTArray.length - 1) {
			num++;
		} else {
			num = 0;
		}
		//alert(TEXT);

		tctx.fillStyle = "white";
		tctx.fillRect(0, 0, W, H)
		//tctx.fill();

		tctx.font = "bold " + QUALITY_TO_FONT_SIZE[QUALITY] + "px " + FANCY_FONT;

		//tctx.textAlign='center';//文本水平对齐方式
		//tctx.textBaseline='middle';

		//tctx.strokeStyle = "black";
		tctx.fillStyle = "#f00";
		//tctx.strokeText(TEXT,30, 50);
		tctx.fillText(TEXT, 20, 60);

		image_data = tctx.getImageData(0, 0, W, H);
		pixels = image_data.data;
		positions = [];
		for (var i = 0; i < pixels.length; i = i + 2) {
			if (pixels[i] != 255) {
				position = {
					x: (i / 2 % W | 0) * QUALITY_TO_SCALE[QUALITY] | 0,
					y: (i / 2 / W | 0) * QUALITY_TO_SCALE[QUALITY] | 0
				}
				positions.push(position);
			}
		}

		get_destinations();
	}

	function draw() {

		var now = Date.now();

		ctx.globalCompositeOperation = "source-over";

		if (BLUR) ctx.globalAlpha = 0.1;
		else if (!BLUR && !BLINK) ctx.globalAlpha = 1.0;

		ctx.fillStyle = BACKGROUND;
		ctx.fillRect(0, 0, W, H)

		if (BLENDING) ctx.globalCompositeOperation = "lighter";

		for (var i = 0; i < particles.length; i++) {

			p = particles[i];

			/* in lower qualities there is not enough full pixels for all of  them - dirty hack*/

			if (isNaN(p.x)) continue

			ctx.beginPath();
			ctx.fillStyle = "rgb(" + p.r + ", " + p.g + ", " + p.b + ")";
			ctx.fillStyle = "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.alpha + ")";

			if (BLINK) ctx.globalAlpha = Math.sin(Math.PI * mod * 1.0);

			if (PULSATION) { /* this would be 0 -> 1 */
				var mod = ((GLOBAL_PULSATION ? 0 : p.deltaOffset) + now) % PULSATION_PERIOD / PULSATION_PERIOD;

				/* lets make the value bouncing with sinus */
				mod = Math.sin(mod * Math.PI);
			} else var mod = 1;

			var offset = TREMBLING ? TREMBLING * (-1 + Math.random() * 2) : 0;

			var radius = PARTICLE_RADIUS * p.radius;

			if (!ARC) {
				ctx.fillRect(offset + p.x - mod * radius / 2 | 0, offset + p.y - mod * radius / 2 | 0, radius * mod,
					radius * mod);
			} else {
				ctx.arc(offset + p.x | 0, offset + p.y | 0, radius * mod, Math.PI * 2, false);
				ctx.fill();
			}

			p.x += (p.dx - p.x) / 10;
			p.y += (p.dy - p.y) / 10;
		}
	}

	function get_destinations() {
		for (var i = 0; i < particles.length; i++) {
			pa = particles[i];
			particles[i].alpha = 1;
			var distance = [];
			nearest_position = 0;
			if (positions.length) {
				for (var n = 0; n < positions.length; n++) {
					po = positions[n];
					distance[n] = Math.sqrt((pa.x - po.x) * (pa.x - po.x) + (pa.y - po.y) * (pa.y - po.y));
					if (n > 0) {
						if (distance[n] <= distance[nearest_position]) {
							nearest_position = n;
						}
					}
				}
				particles[i].dx = positions[nearest_position].x;
				particles[i].dy = positions[nearest_position].y;
				particles[i].distance = distance[nearest_position];

				var po1 = positions[nearest_position];
				for (var n = 0; n < positions.length; n++) {
					var po2 = positions[n];
					distance = Math.sqrt((po1.x - po2.x) * (po1.x - po2.x) + (po1.y - po2.y) * (po1.y - po2.y));
					if (distance <= 5) {
						positions.splice(n, 1);
					}
				}
			} else {
				//particles[i].alpha = 0;
			}
		}
	}

	function init() {
		new_positions();
		setInterval(draw, 30);
		setInterval(new_positions, 2000);
	}

	init();
};
</script>

<style type="text/css">
body {
	background: #000;
	text-align: center;
	font-family: "simhei"
}
canvas {
	margin: auto;
	position: absolute;
	left: 0;
	right:0;
	top: 0;
	bottom: 0;
}
</style>

<canvas id="canvas" width="1000" height="800"></canvas>

</body>
</html>

下面是运行效果视频:

20240124_21:16:06_1


代码可以直接复制

如果有啥问题可以问我看到一定会回复大家,如果大家喜欢可以作者点赞和关注

大家的支持是我创作下去的最大动力!文章来源地址https://www.toymoban.com/news/detail-823808.html

到了这里,关于html 粒子效果文字特效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 前端:运用html+css+js模仿京东上商品图片区域放大特效

    1. 前言 最近在网页端浏览京东上的商品时,觉得上面的那张gif图片上实现的特效不错,于是自己打算使用html+css+js技术来实现一下上述特效效果,我的实效果如下: 2. 前端界面 主要使用到浮动、绝对定位、相对定位等知识,不了解这部分知识点的读者可以先去了解了解,再

    2024年02月16日
    浏览(45)
  • 【HTML+CSS+JavaScript】实现鼠标点击烟花效果

    本文主要讲解三种烟花效果(爆炸型、心型、圆形),文章末尾附有完整的代码和图片获取链接。 效果图(一) - 心型 : 效果图(二) - 圆型 : 效果图(三) - 爆炸型 : (1) HTML部分代码 (2) CSS部分代码 (3) 内部的JavaScript部分代码 (1) HTML部分代码 (2) CSS部分代码 (3) 内部的JavaScript部分

    2024年02月01日
    浏览(65)
  • 前端:运用html+css+js模仿百度热搜电影榜鼠标移入特效

    1. 实现原理 百度热搜上电影榜鼠标移入特效如上图所示。个人觉得上述特效实现原理为使用相对定位、绝对定位实现的(鼠标移入和没有移入时,元素布局有一些不同而已)。至于鼠标移入时,出现延迟效果,则是在css上设置transition(用于设置过渡效果的)实现的。我的实现效果

    2024年02月15日
    浏览(47)
  • 圣诞节酷炫特效合集【含十几个HTML+CSS前端特效+34个桌面酷炫圣诞程序】

    ❤️源码获取:订阅后见文末 ❤️内容介绍:包含HTML+CSS等十几个圣诞特效;以及三十四个桌面酷炫圣诞树合集 ❤️订阅后所得如下: ❤️HTML圣诞+桌面圣诞程序效果如下: 下方展示代码仅举例其中几个 所有效果源码及文件订阅后找博主获取即可

    2024年02月04日
    浏览(40)
  • HTML+CSS+JavaScript:实现B站评论发布效果

    1、用户输入内容,输入框右下角实时显示输入字数  2、为避免用户输入时在内容左右两端误按多余的空格,在发送评论时,检测用户输入的内容左右两端是否带有空格,若有空格,发布时自动取消左右两端的空格 3、若用户发布的内容为空,则自动取消该条评论的发送,并弹

    2024年02月14日
    浏览(29)
  • HTML + CSS + JavaScript【实战案例】 实现动画导航栏效果

    ​Hello~ 咱们今天一起来学习一个动画导航的小项目 HTML结构

    2024年02月03日
    浏览(48)
  • HTML、CSS和JavaScript,实现换肤效果的原理

    这篇涉及到HTML DOM的节点类型、节点层级关系、DOM对象的继承关系、操作DOM节点和HTML元素 还用到HTML5的本地存储技术。 换肤效果的原理:是在选择某种皮肤样式之后,通过JavaScript脚本来加载选中的样式,再通过localStorage存储。 先来回忆一下HTML DOM的相关知识。 DOM模型就是通

    2024年02月06日
    浏览(38)
  • 情人节程序员用HTML网页表白【可爱的节日贺卡ui动画特效】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript

    这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看。 此波共有100个表白网站,可以任意修改和使用,很多人会希望向心爱的男孩女孩告白,生性腼腆的人即使那个TA站在眼前都不敢向前表白。说不出口的话就用网页告诉

    2024年02月02日
    浏览(43)
  • 27.CSS粒子特效

    2024年02月10日
    浏览(26)
  • HTML+CSS+JavaScript:两种方法实现商品价格筛选效果

    鼠标点击上方菜单栏中不同的价格区间,自动筛选出价格符合条件的商品,并渲染在页面中   以下是缺失JS部分的代码,感兴趣的小伙伴可以先自己试着写一写 1、封装渲染函数,传入的参数为数组对象,将数组中的每一个对象进行数据处理,再渲染到页面中 2、利用事件委托

    2024年02月14日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包