canvas+js实现荧光字符效果
一个小玩意,代码来源于网络。
效果图如下

代码如下
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
} html,
body {
height: 100%;
position: relative;
width: 100%;
} body {
background: #eee;
} canvas {
background: white;
display: block;
} #c {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
<script>
;(function(main) {
var args = {};
window.onload = function() {
main(args);
};
})(function(args) { 'use strict'; var Box = function(x, y, w, h, s) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.s = s;
this.a = Math.random() * Math.PI * 2;
this.hue = Math.random() * 360;
}; Box.prototype = {
constructor: Box,
update: function() {
this.a += Math.random() * 0.5 - 0.25;
this.x += Math.cos(this.a) * this.s;
this.y += Math.sin(this.a) * this.s;
this.hue += 5;
if(this.x > WIDTH) this.x = 0;
else if(this.x < 0) this.x = WIDTH;
if(this.y > HEIGHT) this.y = 0;
else if(this.y < 0) this.y = HEIGHT;
},
render: function(ctx) {
ctx.save();
ctx.fillStyle = 'hsla(' + this.hue + ', 100%, 50%, 1)';
ctx.translate(this.x, this.y);
ctx.rotate(this.a);
ctx.fillRect(-this.w, -this.h / 2, this.w, this.h);
ctx.restore();
}
}; var Circle = function(x, y, tx, ty, r) {
this.x = x;
this.y = y;
this.ox = x;
this.oy = y;
this.tx = tx;
this.ty = ty;
this.lx = x;
this.ly = y;
this.r = r;
this.br = r;
this.a = Math.random() * Math.PI * 2;
this.sx = Math.random() * 0.5;
this.sy = Math.random() * 0.5;
this.o = Math.random() * 1;
this.delay = Math.random() * 200;
this.delayCtr = 0;
this.hue = Math.random() * 360;
}; Circle.prototype = {
constructor: Circle,
update: function() { if(this.delayCtr < this.delay) {
this.delayCtr++;
return;
} this.hue += 1;
this.a += 0.1; this.lx = this.x;
this.ly = this.y; if(!clickToggle) {
this.x += (this.tx - this.x) * this.sx;
this.y += (this.ty - this.y) * this.sy;
} else {
this.x += (this.ox - this.x) * this.sx;
this.y += (this.oy - this.y) * this.sy;
} this.r = this.br + Math.cos(this.a) * (this.br * 0.5);
},
render: function(ctx) { ctx.save();
ctx.globalAlpha = this.o;
ctx.fillStyle = 'hsla(' + this.hue + ', 100%, 50%, 1)';
ctx.translate(this.x, this.y);
ctx.beginPath();
ctx.arc(0, 0, this.r, 0, Math.PI * 2);
ctx.fill();
ctx.restore(); if(clickToggle) {
ctx.save();
ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, 50%, 1)';
ctx.beginPath();
ctx.moveTo(this.lx, this.ly);
ctx.lineTo(this.x, this.y);
ctx.stroke();
ctx.restore();
} }
}; var txtCanvas = document.createElement('canvas');
var txtCtx = txtCanvas.getContext('2d'); var c = document.getElementById('c');
var ctx = c.getContext('2d'); var WIDTH = c.width = window.innerWidth;
var HEIGHT = c.height = window.innerHeight;
var imgData = null;
var idx = null;
var skip = 4;
var circles = [];
var circle = null;
var a = null;
var clickToggle = false;
var boxList = [];
var box = null; txtCanvas.width = WIDTH;
txtCanvas.height = HEIGHT; txtCtx.font = 'bold 70px Sans-serif';
txtCtx.textAlign = 'center';
txtCtx.baseline = 'middle';
txtCtx.fillText('I L O V E U', WIDTH / 2, HEIGHT / 2); ctx.font = 'bold 12px Monospace';
ctx.textAlign = 'center';
ctx.baseline = 'middle'; imgData = txtCtx.getImageData(0, 0, WIDTH, HEIGHT).data; for(var y = 0; y < HEIGHT; y += skip) {
for(var x = 0; x < WIDTH; x += skip) {
idx = (x + y * WIDTH) * 4 - 1;
if(imgData[idx] > 0) {
a = Math.PI * 2 * Math.random();
circle = new Circle(
WIDTH / 2 + Math.cos(a) * WIDTH,
HEIGHT / 2 + Math.sin(a) * WIDTH,
x,
y,
Math.random() * 4
);
circles.push(circle);
}
}
} for(var b = 0; b < 10; b++) {
box = new Box(
WIDTH * Math.random(),
HEIGHT * Math.random(),
5,
2,
5 + Math.random() * 5
);
boxList.push(box);
} c.addEventListener('click', function(e) {
clickToggle = !clickToggle;
}); requestAnimationFrame(function loop() {
requestAnimationFrame(loop); ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, WIDTH, HEIGHT); ctx.fillStyle = 'white';
ctx.fillText('CLICK TO TOGGLE', WIDTH / 2, HEIGHT / 2 + 100); ctx.globalCompositeOperation = 'lighter'; for(var i = 0, len = circles.length; i < len; i++) {
circle = circles[i];
circle.update();
circle.render(ctx);
} for(var j = 0; j < boxList.length; j++) {
box = boxList[j];
box.update();
box.render(ctx);
} }); });
</script>
</head>
<body>
<canvas id="c"></canvas>
</body>
</html>
可直接复制代码保存为html文件,打开查看效果
canvas+js实现荧光字符效果的更多相关文章
- 用HTML5 Canvas 做擦除及扩散效果
2013年的时候曾经使用canvas实现了一个擦除效果的需求,即模拟用户在模糊的玻璃上擦除水雾看到清晰景色的交互效果.好在2012年的时候学习HTML5的时候研究过canvas了,所以在比较短的时间内 ...
- html5 canvas+js实现ps钢笔抠图
html5 canvas+js实现ps钢笔抠图 1. 项目要求需要用js实现photoshop中钢笔抠图功能,就用了近三四天的时间去解决它,最终还是基本上把他实现了. 做的过程中走了不少弯路,最终一同 ...
- html5 canvas+js实现ps钢笔抠图(速抠图 www.sukoutu.com)
html5 canvas+js实现ps钢笔抠图(速抠图 www.sukoutu.com) 根据html5 canvas+js实现ps钢笔抠图的实现,aiaito 开发者开发了一套在线抠图工具,速抠 ...
- 使用JavaScript和Canvas打造真实的雨滴效果
使用JavaScript和Canvas打造真实的雨滴效果 寸志 · 1 年前 我最近搞了一个有趣的项目——rainyday.js .我认为这个项目并不怎么样,而且,事实上这是我第一次尝试接触一些比弹窗 ...
- vue+canvas实现炫酷时钟效果的倒计时插件(已发布到npm的vue2插件,开箱即用)
前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...
- HTML5在canvas中绘制复杂形状附效果截图
HTML5在canvas中绘制复杂形状附效果截图 一.绘制复杂形状或路径 在简单的矩形不能满足需求的情况下,绘图环境提供了如下方法来绘制复杂的形状或路径. beginPath() : 开始绘制一个新路 ...
- 原生JS封装简单动画效果
原生JS封装简单动画效果 一致使用各种插件,有时候对原生JS陌生了起来,所以决定封装一个简单动画效果,熟悉JS原生代码 function animate(obj, target,num){ if(ob ...
- JS实现回到顶部效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 经典!HTML5 Canvas 模拟可撕裂布料效果
这是一个模拟可撕裂布料效果的 HTML5 Canvas 应用演示,效果逼真.你会看到,借助 Canvas 的强大绘图和动画功能,只需很少的代码就能实现让您屏息凝神的效果. 温馨提示:为保证最佳的效果, ...
随机推荐
- js自执行函数的常见写法
js自执行函数的常见写法 2016-12-20 20:02:26 1.关于自执行函数 1.1 写自执行函数的好处:独立的作用域,不会污染全局环境 (function() { })(); 1.2 理解重 ...
- XML—代码—DOM4J解析
什么是xml: 众所周知,xml常用语数据存储和传输,文件后缀为 .xml: 它是可扩展标记语言(Extensible Markup Language,简称XML),是一种标记语言. 如何定义这些标记 ...
- 好消息啊,有些c似乎不用加厂商前缀了
今天写程序,一个不小心,没写厂商前缀,然而,,,,,,, 可以运行了(我居然不知道这个消息!!!!) 赶紧写几个字纪念一下. (把鼠标移动到红色的字上边~~~) don' you think that ...
- git必备命令
git status 查看git的状态git add <path>的形式把我们<path>添加到索引库中,<path>可以是文件也可以是目录.git add -u ...
- 20180112final和static
final关键字 fianl关键字可以修饰类.成员变量.和方法中的局部变量. (java中的局部变量?定义在方法中的变量都是局部变量,使用前必须初始化) 1.fianl类 Final类不能被继承,即可 ...
- POJO/VO/DTO等对象模型
JavaBean 要想成为JavaBean,需要满足以下条件: 1,提供一个默认的无参构造函数. 2,需要被序列化并且实现了Serializable接口. 3,可能有一系列可读写属性伴随"g ...
- c++获取时间戳
vc获取时间戳的代码如下: SYSTEMTIME st; }; GetLocalTime(&st); // vc专用 _snprintf_s(ts, sizeof(ts), "%4d ...
- 04:sqlalchemy操作数据库
目录: 1.1 ORM介绍(作用:不用原生SQL语句对数据库操作) 1.2 安装sqlalchemy并创建表 1.3 使用sqlalchemy对表基本操作 1.4 一对多外键关联 1.5 sqlalc ...
- Unity3D之预设
预设的两个特性: 1.重用性.可以使我们快速方便的创建大量的重复性资源.这种创建是通过预设的实例化Instance操作来完成的 在Hierarchy视图中将实例物理添加到Assests中,此时图标 ...
- 20145118《Java程序设计》 第9周学习总结
20145118 <Java程序设计>第9周学习总结 教材学习内容总结 1.SUN公司定义了一套Java操作数据库的规范(接口)来简化数据库操作,称之为JDBC.开发人员只需要学习jdbc ...