作者:戚敦崩 | 来源:互联网 | 2023-09-02 14:34
篇首语:本文由编程笔记#小编为大家整理,主要介绍了纯Javascript实现表白代码相关的知识,希望对你有一定的参考价值。
纯Javascript实现表白代码
文章目录
在国庆节当天,大家都在表达着对祖国的热爱,当然这也是个对自己爱的人表白的一个好机会,本文旨在使用Javascript实现表白的代码。
代码
首先是实现的具体代码:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns&#61;"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;utf-8" />
<title>htm5浪漫表白title>
head>
<body>
<script type&#61;"text/Javascript">
BLUR &#61; false;
PULSATION &#61; true;
PULSATION_PERIOD &#61; 600;
PARTICLE_RADIUS &#61; 4;
BLINK &#61; false;
GLOBAL_PULSATION &#61; false;
QUALITY &#61; 2;
ARC &#61; true;
TREMBLING &#61; 0;
FANCY_FONT &#61; "Arial";
BACKGROUND &#61; "#000";
BLENDING &#61; true;
var TEXT;
num &#61; 0;
TEXTArray &#61; ["陌陌", "深爱", "某某", "直到", "永远"];
QUALITY_TO_FONT_SIZE &#61; [10, 12, 40, 50, 100, 350];
QUALITY_TO_SCALE &#61; [20, 6, 4, 2, 0.9, 0.5];
QUALITY_TO_TEXT_POS &#61; [10, 20, 60, 100, 370, 280];
window.onload &#61; function ()
document.body.style.backgroundColor &#61; BACKGROUND;
var canvas &#61; document.getElementById("canvas");
var ctx &#61; canvas.getContext("2d");
var W &#61; canvas.width;
var H &#61; canvas.height;
var tcanvas &#61; document.createElement("canvas");
var tctx &#61; tcanvas.getContext("2d");
tcanvas.width &#61; W;
tcanvas.height &#61; H;
total_area &#61; W * H;
total_particles &#61; 928;
single_particle_area &#61; total_area / total_particles;
area_length &#61; Math.sqrt(single_particle_area);
var particles &#61; [];
for (var i &#61; 1; i <&#61; total_particles; i&#43;&#43;)
particles.push(new particle(i));
function particle(i)
this.r &#61; Math.round(Math.random() * 255 | 0);
this.g &#61; Math.round(Math.random() * 255 | 0);
this.b &#61; Math.round(Math.random() * 255 | 0);
this.alpha &#61; 1;
this.x &#61; (i * area_length) % W;
this.y &#61; (i * area_length) / W * area_length;
this.deltaOffset &#61; Math.random() * PULSATION_PERIOD | 0;
this.radius &#61; 0.1 &#43; Math.random() * 2;
var positions &#61; [];
function new_positions()
TEXT &#61; TEXTArray[num];
if (num < TEXTArray.length - 1)
num&#43;&#43;;
else
num &#61; 0;
tctx.fillStyle &#61; "white";
tctx.fillRect(0, 0, W, H)
tctx.font &#61; "bold " &#43; QUALITY_TO_FONT_SIZE[QUALITY] &#43; "px " &#43; FANCY_FONT;
tctx.fillStyle &#61; "#f00";
tctx.fillText(TEXT, 20, 60);
image_data &#61; tctx.getImageData(0, 0, W, H);
pixels &#61; image_data.data;
positions &#61; [];
for (var i &#61; 0; i < pixels.length; i &#61; i &#43; 2)
if (pixels[i] !&#61; 255)
position &#61;
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 &#61; Date.now();
ctx.globalCompositeOperation &#61; "source-over";
if (BLUR) ctx.globalAlpha &#61; 0.1;
else if (!BLUR && !BLINK) ctx.globalAlpha &#61; 1.0;
ctx.fillStyle &#61; BACKGROUND;
ctx.fillRect(0, 0, W, H)
if (BLENDING) ctx.globalCompositeOperation &#61; "lighter";
for (var i &#61; 0; i < particles.length; i&#43;&#43;)
p &#61; particles[i];
if (isNaN(p.x)) continue
ctx.beginPath();
ctx.fillStyle &#61; "rgb(" &#43; p.r &#43; ", " &#43; p.g &#43; ", " &#43; p.b &#43; ")";
ctx.fillStyle &#61; "rgba(" &#43; p.r &#43; ", " &#43; p.g &#43; ", " &#43; p.b &#43; ", " &#43; p.alpha &#43; ")";
if (BLINK) ctx.globalAlpha &#61; Math.sin(Math.PI * mod * 1.0);
if (PULSATION)
var mod &#61; ((GLOBAL_PULSATION ? 0 : p.deltaOffset) &#43; now) % PULSATION_PERIOD / PULSATION_PERIOD;
mod &#61; Math.sin(mod * Math.PI);
else var mod &#61; 1;
var offset &#61; TREMBLING ? TREMBLING * (-1 &#43; Math.random() * 2) : 0;
var radius &#61; PARTICLE_RADIUS * p.radius;
if (!ARC)
ctx.fillRect(offset &#43; p.x - mod * radius / 2 | 0, offset &#43; p.y - mod * radius / 2 | 0, radius * mod,
radius * mod);
else
ctx.arc(offset &#43; p.x | 0, offset &#43; p.y | 0, radius * mod, Math.PI * 2, false);
ctx.fill();
p.x &#43;&#61; (p.dx - p.x) / 10;
p.y &#43;&#61; (p.dy - p.y) / 10;
function get_destinations()
for (var i &#61; 0; i < particles.length; i&#43;&#43;)
pa &#61; particles[i];
particles[i].alpha &#61; 1;
var distance &#61; [];
nearest_position &#61; 0;
if (positions.length)
for (var n &#61; 0; n < positions.length; n&#43;&#43;)
po &#61; positions[n];
distance[n] &#61; Math.sqrt((pa.x - po.x) * (pa.x - po.x) &#43; (pa.y - po.y) * (pa.y - po.y));
if (n > 0)
if (distance[n] <&#61; distance[nearest_position])
nearest_position &#61; n;
particles[i].dx &#61; positions[nearest_position].x;
particles[i].dy &#61; positions[nearest_position].y;
particles[i].distance &#61; distance[nearest_position];
var po1 &#61; positions[nearest_position];
for (var n &#61; 0; n < positions.length; n&#43;&#43;)
var po2 &#61; positions[n];
distance &#61; Math.sqrt((po1.x - po2.x) * (po1.x - po2.x) &#43; (po1.y - po2.y) * (po1.y - po2.y));
if (distance <&#61; 5)
positions.splice(n, 1);
else
function init()
new_positions();
setInterval(draw, 30);
setInterval(new_positions, 2000);
init();
;
script>
<style type&#61;"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&#61;"canvas" width&#61;"1000" height&#61;"800">canvas>
<div style&#61;"text-align:center;">
<p><a href&#61;"" title&#61;"" target&#61;"_blank">a>p>
div>
body>
html>
效果图
下面展示代码的部分效果图&#xff0c;完整的效果可以复制上述的代码之后运行即就可以了。
最后&#xff0c;谢谢大家的支持了。