没有最好,只要更好,如题所示,这篇文章只如果分享一个用 Canvas 来完成的粒子活动结果。觉得有点题目党了,但换个角度,勉勉强强算是炫丽吧,虽然颜色上与炫丽无关,但活动结果上照样算得上有点点炫的。不论怎样,我们照样最先这个所谓的炫丽结果吧!
直接上代码 ,不懂能够看代码解释。预计就会看邃晓也许的思绪了。
html 代码
HTML 代码不多,只如果几个操纵元素。这里一看就邃晓。不费过量口舌。我们来看看本文的主角 Javascript 代码,不过,在看代码前,我们无妨先听听完成这个结果的思绪:
Javascript 代码中使用了三个 Canvas 画布,this.iCanvas(主场)、this.iCanvasCalculate(用来盘算笔墨宽度)、this.iCanvasPixel(用于画出笔墨,并从中获得笔墨对应的像素点的位置坐标)。
this.iCanvasCalculate 和 this.iCanvasPixel 这两个无需在页面中显示出来,只是辅佐作用。
下面就献上棒棒的 JS 完成代码
function Circle() {
var This = this;
this.init();
this.generalRandomParam();
this.drawCircles();
this.ballAnimate();
this.getUserText();
// 窗口转变大小后,生盘算并猎取画面
window.Onresize= function(){
This.stateW = document.body.offsetWidth;
This.stateH = document.body.offsetHeight;
This.iCanvasW = This.iCanvas.width = This.stateW;
This.iCanvasH = This.iCanvas.height = This.stateH;
This.ctx = This.iCanvas.getContext("2d");
}
}
// 初始化
Circle.prototype.init = function(){
//父元素宽高
this.stateW = document.body.offsetWidth;
this.stateH = document.body.offsetHeight;
this.iCanvas = document.createElement("canvas");
// 设置Canvas 与父元素同宽高
this.iCanvasW = this.iCanvas.width = this.stateW;
this.iCanvasH = this.iCanvas.height = this.stateH;
// 猎取 2d 绘画环境
this.ctx = this.iCanvas.getContext("2d");
// 插进去到 body 元素中
document.body.appendChild(this.iCanvas);
this.iCanvasCalculate = document.createElement("canvas");
// 用于保留盘算笔墨宽度的画布
this.mCtx = this.iCanvasCalculate.getContext("2d");
this.mCtx.fOnt= "128px 微软雅黑";
this.iCanvasPixel = document.createElement("canvas");
this.iCanvasPixel.setAttribute("style","position:absolute;top:0;left:0;");
this.pCtx = null; // 用于绘画笔墨的画布
// 随机天生圆的数目
this.ballNumber = ramdomNumber(1000, 2000);
// 保留一切小球的数组
this.balls = [];
// 保留动画中末了一个住手活动的小球
this.animte = null;
this.imageData = null;
this.textWidth = 0; // 保留天生笔墨的宽度
this.textHeight = 150; // 保留天生笔墨的高度
this.inputText = ""; // 保留用户输入的内容
this.actiOnCount= 0;
this.ballActor = []; // 保留天生笔墨的粒子
this.actorNumber = 0; // 保留天生笔墨的粒子数目
this.backType = "back"; // 归位
this.backDynamics = ""; // 动画结果
this.isPlay = false; // 标识(在天生笔墨过程当中,不能再天生)
}
// 衬着出一切圆
Circle.prototype.drawCircles = function () {
for(var i=0;i
}
}
// 猎取用户输入笔墨
Circle.prototype.getUserText = function(){
This = this; // 保留 this 指向
ipu = document.getElementById("input-text");
ipu.addEventListener("keydown",function(event){
if(event.which === 13){ // 如果是回车键
ipu.value = ipu.value.trim(); // 去头尾空格
var pat = /[\u4e00-\u9fa5]/; // 中文推断
var isChinese = pat.test(ipu.value);
if(ipu.value.length !=0 && isChinese){
This.inputText = ipu.value;
}else{
alert("请输入汉字");
return;
}
if(This.isPlay){
return
}
This.getAnimateType();
This.getTextPixel();
This.isPlay = true;
}
});
}
// 盘算笔墨的宽
Circle.prototype.calculateTextWidth = function () {
this.textWidth = this.mCtx.measureText(this.inputText).width;
}
// 猎取笔墨像素点
Circle.prototype.getTextPixel = function () {
if(this.pCtx){
this.pCtx.clearRect(0,0,this.textWidth,this.textHeight);
}
this.calculateTextWidth(this.inputText);
this.iCanvasPixel.width = this.textWidth;
this.iCanvasPixel.height = this.textHeight;
this.pCtx = this.iCanvasPixel.getContext("2d");
this.pCtx.fOnt= "128px 微软雅黑";
this.pCtx.fillStyle = "#FF0000";
this.pCtx.textBaseline = "botom";
this.pCtx.fillText(this.inputText,0,110);
this.imageData = this.pCtx.getImageData(0,0,this.textWidth,this.textHeight).data;
this.getTextPixelPosition(this.textWidth,this.textHeight);
}
// 猎取笔墨粒子像素点位置
Circle.prototype.getTextPixelPosition = function (width,height) {
var left = (this.iCanvasW - width)/2;
var top = (this.iCanvasH - height)/2;
var space = 4;
this.actiOnCount= 0;
for(var i=0;i
if(this.imageData[index] == 255){
if(this.actionCount
this.balls[this.actionCount].targetX = left+j;
this.balls[this.actionCount].targetY = top+i;
this.balls[this.actionCount].backX = this.balls[this.actionCount].x;
this.balls[this.actionCount].backY = this.balls[this.actionCount].y;
this.ballActor.push(this.balls[this.actionCount]);
this.actionCount++;
}
}
}
this.actorNumber = this.ballActor.length;
}
this.animateToText();
}
// 粒子活动到指定位置
Circle.prototype.animateToText = function(){
for(var i=0;i
x: this.ballActor[i].targetX,
y: this.ballActor[i].targetY
},{
type: dynamics.easeIn,
duration: 1024,
});
}
setTimeout(function(){
This.ballbackType();
},3000);
}
// 粒子原路返回
Circle.prototype.ballBackPosition = function(){
for(var i=0;i
dynamics.animate(ball, {
x: ball.backX,
y: ball.backY
},{
type: dynamics[this.backDynamics],
duration: 991,
complete:this.changeStatus(ball)
});
}
}
// 猎取范例|动画结果
Circle.prototype.getAnimateType = function() {
var selectType = document.getElementById("selectType");
var selectDynamics = document.getElementById("selectDynamics");
this.backType = selectType.options[selectType.options.selectedIndex].value;
this.backDynamics = selectDynamics.options[selectDynamics.options.selectedIndex].value;
}
// 复位散开
Circle.prototype.ballbackType = function(){
if(this.backType == "back"){
this.ballBackPosition();
}else{
this.ballAutoPosition();
}
this.ballActor = [];
}
// 随机散开
Circle.prototype.ballAutoPosition = function(ball){
for(var i=0;i
}
}
// 变动小球状况
Circle.prototype.changeStatus = function(ball){
ball.status = 0;
if(this.isPlay == true){
this.isPlay = false;
}
}
// 随机天生每一个圆的相干参数
Circle.prototype.generalRandomParam = function(){
for(var i=0;i
ball.size = 1; // 随机天生圆半径
// 随机天生圆心 x 坐标
ball.x = ramdomNumber(0+ball.size, this.iCanvasW-ball.size);
ball.y = ramdomNumber(0+ball.size, this.iCanvasH-ball.size);
ball.speedX = ramdomNumber(-1, 1);
ball.speedY = ramdomNumber(-1, 1);
this.balls.push(ball);
ball.status = 0;
ball.targetX = 0;
ball.targetY = 0;
ball.backX = 0;
ball.backY = 0;
}
}
// 转变圆的位置
Circle.prototype.changeposition = function(){
for(var i=0;i
this.balls[i].x += this.balls[i].speedX;
this.balls[i].y += this.balls[i].speedY;
}
}
}
// 画圆
Circle.prototype.renderBall = function(ball){
this.ctx.fillStyle = "#fff";
this.ctx.beginPath(); // 这个一定要加
this.ctx.arc(ball.x, ball.y, ball.size, 0, 2 * Math.PI);
this.ctx.closePath(); // 这个一定要加
this.ctx.fill();
}
// 小球碰撞推断
Circle.prototype.collision = function(ball){
for(var i=0;i
ball.x = this.iCanvasW-ball.size;
}else{
ball.x = ball.size;
}
ball.speedX = - ball.speedX;
}
if(ball.y>this.iCanvasH-ball.size || ball.y
ball.y = this.iCanvasH-ball.size;
}else{
ball.y = ball.size;
}
ball.speedY = - ball.speedY;
}
}
}
// 最先动画
Circle.prototype.ballAnimate = function(){
var This = this;
var animateFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
(function move(){
animte = animateFrame(move);
This.ctx.clearRect(0, 0, This.iCanvasW, This.iCanvasH);
This.changeposition();
for(var i=0;i
This.renderBall(This.balls[i]);
}
})();
}
// 天生一个随机数
function ramdomNumber(min, max) {
return Math.random() * (max - min) + min;
}
看了代码预计也只是内心炫了一下,也没有让你想把这个东西做出来的欲望,为此我晓得必须得让你眼睛心悦诚服才行。在线 DEMO: 动感的粒子示例。
人无完人,代码也一样。看起来运转顺畅的代码也或多或少有一些瑕疵,日前这个结果还只支撑中文。英文的话,我得再勤奋一把,不论怎样,英文背面肯定是会到场来的,只是时间问题了。另有代码顶用于标记是不是可再次实行天生笔墨的 属性:this.isPlay ,照样一点瑕疵,this.isPlay 的状况变动没有正确的在粒子归位的那一霎时变动,而是提早变动了状况。但这个状况不会影响本例子结果的完全完成。
这个例子顶用到了 dynamics.js 库,主如果用到它内里的一些活动函数,让粒子动起来更动人一些,仅此而已。