特效描述:利用HTML5实现 Canvas 粒子文字特效。利用HTML5实现Canvas粒子文字特效

代码结构

1. HTML代码

change

/*jshint esversion:6*/

var can = document.createElement("canvas");

document.body.appendChild(can);

var ctx = can.getContext('2d');

function resize(){

can.width = window.innerWidth;

can.height = window.innerHeight;

}

const max_radius = 3;

const min_radius = 1;

const drag = 50;

window.onresize = function(){

resize();

};

function cfill(){

ctx.fillStyle = "#000";

ctx.fillRect(0,0,can.width,can.height);

ctx.fill();

}

var mouse = {

x:-1000,

y:-1000

};

can.onmousemove = function(e){

mouse.x = e.clientX;

mouse.y = e.clientY;

};

can.ontouchmove = function(e){

mouse.x = e.touches[0].clientX;

mouse.y = e.touches[0].clientY;

};

resize();

cfill();

function distance(x,y,x1,y1){

return Math.sqrt( ( x1-x ) * ( x1-x ) + ( y1-y ) * ( y1-y ) );

}

class Particle{

constructor(pos,target,vel,color,radius){

this.pos = pos;

this.target = target;

this.vel = vel;

this.color = color;

this.radius = radius;

var arr = [-1,1];

this.direction = arr[~~(Math.random()*2)]*Math.random()/10;

}

set(type,value){

this[type] = value;

}

update(){

this.radius += this.direction;

this.vel.x = (this.pos.x - this.target.x)/drag;

this.vel.y = (this.pos.y - this.target.y)/drag;

if(distance(this.pos.x,this.pos.y,mouse.x,mouse.y) < 50){

this.vel.x += this.vel.x - (this.pos.x - mouse.x)/15;

this.vel.y += this.vel.y - (this.pos.y - mouse.y)/15;

}

if(this.radius >= max_radius){

this.direction *= -1;

}

if(this.radius <= 1){

this.direction *= -1;

}

this.pos.x -= this.vel.x;

this.pos.y -= this.vel.y;

}

draw(){

ctx.beginPath();

ctx.fillStyle = this.color;

ctx.arc(this.pos.x,this.pos.y,this.radius,0,Math.PI*2);

ctx.fill();

}

}

var particles = [];

var colors = ["#bf1337","#f3f1f3","#084c8d","#f2d108","#efd282"];

var bool = true;

var current = 0,i;

function changeText(text){

var current = 0,temp,radius,color;

cfill();

ctx.fillStyle = "#fff";

ctx.font = "120px Times";

ctx.fillText(text,can.width*0.5-ctx.measureText(text).width*0.5,can.height*0.5+60);

var data = ctx.getImageData(0,0,can.width,can.height).data;

cfill();

for(i = 0;i < data.length;i += 8){

temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)};

if(data[i] !== 0 && ~~(Math.random()*5) == 1/*(temp.x % (max_radius+1) === 0 && temp.y % (max_radius+1) === 0)*/){

if(data[i+4] !== 255 || data[i-4] !== 255 || data[i+can.width*4] !== 255 || data[i-can.width*4] !== 255){

if(current < particles.length){

particles[current].set("target",temp);

}else{

radius = max_radius-Math.random()*min_radius;

temp = {x:Math.random()*can.width,y:Math.random()*can.height};

if(bool){

temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)};

}

color = colors[~~(Math.random()*colors.length)];

var p = new Particle(

temp,

{x:(i/4)%can.width,y:~~((i/4)/can.width)},{x:0,y:0},

color,

radius);

particles.push(p);

}

++current;

}

}

}

bool = false;

particles.splice(current,particles.length-current);

}

function draw(){

cfill();

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

particles[i].update();

particles[i].draw();

}

}

changeText("51qianduan.com");

setInterval(draw,1);

html5实现粒子效果的文字,利用HTML5实现Canvas粒子文字特效相关推荐

  1. html5简单制作效果,分享一个利用HTML5制作的海浪效果代码-H5教程

    在前面简单讲述了一下HTML里的Canvas,这次根据Canvas完成了"海浪效果"(水波上升). (O(∩_∩)O哈哈哈~作者我能看这个动画看一下午) 上升水波.gif动画分析 ...

  2. html5闪光字效果,如何使用css3+html5来制作文字霓虹灯效果(付完整代码)

    在科技日益发展的今天,人们对于审美越来越挑剔,这就要求我们对于前端开发的态度更加的严谨的同时,需要加入新鲜的元素,以达到吸引目光的目的.那么今天本文带大家了解一下如何使用css3+html5来制作文字 ...

  3. 苹果手机html5摇一摇游戏戏码,利用HTML5的devicemotion事件实现手机摇一摇抽奖,年会抽奖,html5devicemotion...

    利用HTML5的devicemotion事件实现手机摇一摇抽奖,年会抽奖,html5devicemotion 摇一摇JS脚本逻辑: 接下来是移动端JS脚本逻辑的实现,摇一摇的实现需借助html5新增的 ...

  4. html5如何写出圆背景,利用HTML5实现Canvas虚幻圆点背景特效

    特效描述:利用HTML5实现 Canvas 虚幻圆点 背景特效.利用HTML5实现Canvas虚幻圆点背景特效 代码结构 1. 引入JS 2. HTML代码 (function($){ var can ...

  5. html5 可旋转的三维树形结构,利用HTML5实现勾股树植物生长动画特效

    特效描述:利用HTML5实现 勾股树 植物生长 动画特效.利用HTML5实现勾股树植物生长动画特效 代码结构 1. HTML代码 const canvas = document.querySelect ...

  6. html中图片链接生成文字,利用HTML5使文字转图片【长微博生成器】

    RT,实现类似现在网上流行的长微博生成器,就是把长文字转成图片.当然功能比不上网上流传的,不知道网上那些用的什么方法.我这是用的HTML5的Canvas特性,也算一种实现吧. 示例地址: TextTo ...

  7. html5经纬度定位 源码_利用HTML5定位功能获取经纬度,在根据经纬度利用百度地图获取位置信息...

    一.引入相关js 二.js代码 // 单次位置请求执行的函数 function getLocation(){ navigator.geolocation.getCurrentPosition(mapI ...

  8. html5 幻灯片动画效果,18种响应式分层动画jQuery幻灯片特效

    这是一款效果非常炫酷的响应式分层动画jQuery幻灯片特效.该幻灯片每一组动画分为多个层,每一层可以使用不同的动画效果,共有18种不同的效果可以选择. 所有的动画效果都是通过js来在页面中插入CSS代 ...

  9. canvas文字居中;canvas画布文字右对齐;canvas画布文字左对齐;canvas文字自动换行;canvas设置行间距;

    canvas参考手册 场景:画布绘制文本,使用 context.fillText(text,x,y,maxWidth).文本对其也就是设置xy坐标问题. 以下的画布设定宽度假设都是width:500 ...

最新文章

  1. 未来安防人工智能需要攻克的几大技术方向
  2. python语言属于-python属于哪种语言
  3. 终端报错xcrun: error: unable to find utility “xcodebuild”, not a developer tool or
  4. linux mint 安装内核,如何在Ubuntu, Linux Mint中安装Linux Kernel 4.18
  5. UI设计实用素材|闪屏模板
  6. android非法字符 ufeff,Java-在Android Studi上编译时出现错误(1,1)非法字符'\ ufeff'...
  7. Oracle 警告:Weblogic 服务器中含有多个可遭远程利用的严重漏洞
  8. 只需要几行代码就可以轻松实现OCR图片转文字
  9. 手写实现bpnn神经网络
  10. 软考高级/高项/信息系统项目管理师看江山好还是马军好?
  11. Python中用于身份证验证的库函数
  12. 先决条件检查失败错误
  13. 《与大象共舞》读书笔记
  14. 聚光灯下的熊猫TV技术架构演进
  15. Kubernetes 实战——部署基于 Redis 和 Docker 的留言簿
  16. 学计算机励志名言,适合程序员的励志名言
  17. 真正解决vbox不能为虚拟电脑打开一个新任务的解决方法
  18. 杰奇cms mysql查询_杰奇cms自动推送链接插件使用方式
  19. ERP项目文档--想到用时方恨少
  20. 基于JAVAWeb商铺租赁管理系统计算机毕业设计源码+数据库+lw文档+系统+部署

热门文章

  1. 华为实现中:STP链路或端口失效后网络重新收敛所需时间
  2. 乐视Pro3 使用FastBoot命令 双清 恢复出厂设置
  3. 怎样在每个视频中随机抽帧一帧图片出来
  4. 某宝x-sign签名分析-店铺全量商品接口
  5. 谷粒学院-统一结果返回
  6. dns nsswitch.conf
  7. Ubuntu 下安装官网最新版Mongodb (apt命令安装)
  8. 混凝土抗压弹性模量自动计算表_混凝土抗压强度计算表(自动算量)
  9. Laravel+ Vue+实现 + 三级联动!!!
  10. 聚名:高估价域名都有什么表现?