实例介绍
【实例简介】一款利用HTML5 Canvas模拟出来的粒子特效(部分电脑会有点卡,属于正常因为代码中的循环导致)
【实例截图】
【核心代码】
【实例截图】

【核心代码】
<!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>htm5浪漫表白</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 = ["HELLO WORLD!"];
QUALITY_TO_FONT_SIZE = [10, 12, 20, 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 = 1500;
single_particle_area = total_area / total_particles;
area_length = Math.sqrt(single_particle_area);
var particles = [];
function createParticles(num){
single_particle_area = total_area / num;
for (var i = 1; i <= num; 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.7;
}
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,10, 80);
// tctx.fillText(TEXT, 20, 60);
image_data = tctx.getImageData(0, 0, W, H);
pixels = image_data.data;
positions = [];
countPixels = 0;
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
}
countPixels ;
positions.push(position);
}
}
createParticles(countPixels);
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="1600" height="800"></canvas>
<div style="text-align:center;">
</div>
</body>
</html>
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论