1 前言
当天边那颗星出现 你可知我又开始想念 有多少爱恋只能遥遥相望 就像月光洒向海面 年少的我们曾以为 相爱的人就能到永远 当我们相信情到深处在一起 听不见风中的叹息 谁知道爱是什么 短暂的相遇却念念不忘 用尽一生的时间 竟学不会遗忘
谁是你夜空中最亮的星,一直鼓励你前行;愿那个人被岁月温柔以待~
2 星光闪烁动画
2.1 开发环境
- 开发工具:VSCode;
- 开发框架:Vue;
- 开发语言:JavaScript+html;
2.2 效果
方法一效果:
方法二效果:
2.3 源码以及原图
代码中用到的源图如下:原图
源码如下:
- 方法一
starrySky.vue:
<template>
<div id="page">
<canvas ref="myCanvas"></canvas>
</div>
</template>
<script>
import { Star } from "./starrySky.js";
export default {
data() {
return {
myCanvas: {},
starImg: [],
bgImg: {},
starNum: 80,
stars: [],
lastTime: 0,
deltaTime: 0
};
},
created() {
this.initStar();
this.initParams();
},
mounted() {
this.myCanvas = this.$refs.myCanvas;
this.myCanvas.width = window.innerWidth;
this.myCanvas.height = window.innerHeight;
console.log(this.myCanvas);
this.loop();
},
methods: {
initStar() {
const _this = this;
Star.prototype.init = function() {
this.x = Math.random() * window.innerWidth;
this.y = Math.random() * window.innerHeight * 0.6;
this.timer = 0;
this.picNo = Math.floor(Math.random() * 7);
};
Star.prototype.draw = function(ctx, starImg) {
ctx.save();
ctx.drawImage(starImg, this.picNo * 7, 0, 7, 7, this.x, this.y, 28, 28);
ctx.restore();
};
Star.prototype.update = function() {
this.timer += _this.deltaTime;
if (this.timer > 70) {
this.picNo += 1;
this.timer = 0;
this.picNo = this.picNo % 7;
}
};
},
initParams() {
(this.starImg = new Image()), (this.bgImg = new Image());
this.starImg.src = require("./imgs/star.png");
this.bgImg.src = require("./imgs/starrySky.jpg");
for (let i = 0; i < this.starNum; i++) {
this.stars[i] = new Star();
this.stars[i].init();
}
this.lastTime = Date.now();
},
loop() {
const ctx = this.myCanvas.getContext("2d");
const _this = this;
let animation = window.webkitRequestAnimationFrame(function f() {
animation = requestAnimationFrame(f);
const now = Date.now();
_this.deltaTime = now - _this.lastTime;
_this.lastTime = now;
ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
ctx.drawImage(_this.bgImg, 0, 0, window.innerWidth, window.innerHeight);
_this.drawStars(ctx);
});
},
drawStars(ctx) {
for (let i = 0; i < this.starNum; i++) {
this.stars[i].update();
this.stars[i].draw(ctx, this.starImg);
}
}
}
};
</script>
- 方法二
starFlutter.vue:
<template>
<div id="page">
<canvas ref="myCanvas"></canvas>
</div>
</template>
<script>
import { StarF } from "./starrySky.js";
const Star = StarF;
export default {
data() {
return {
myCanvas: {},
bgImg: {},
starNum: 100,
stars: [],
lastTime: 0,
deltaTime: 0,
cvh: window.innerHeight,
cvw: window.innerWidth
};
},
created() {
this.initStar();
this.initParams();
},
mounted() {
this.myCanvas = this.$refs.myCanvas;
this.myCanvas.width = window.innerWidth;
this.myCanvas.height = window.innerHeight;
this.loop();
},
methods: {
initStar() {
const _this = this;
Star.prototype.init = function(radiu) {
this.x = Math.random() * window.innerWidth;
this.y = Math.random() * window.innerHeight * 0.6;
this.radiu = radiu + Math.random() * 3;
this.alpha = Math.floor(Math.random() * 5) * 2;
this.timer = 0;
this.period = 1;
};
Star.prototype.draw = function(ctx) {
const grd = ctx.createRadialGradient(
this.x,
this.y,
this.radiu / 8,
this.x,
this.y,
(this.radiu + this.alpha) * 2
);
grd.addColorStop(0.025, "#fff");
grd.addColorStop(0.5, "hsl(217, 61%, 33%)");
grd.addColorStop(1, "hsl(217, 64%, 6%)");
ctx.fillStyle = grd;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radiu + this.alpha * 2, 0, 2 * Math.PI);
ctx.fill();
};
Star.prototype.update = function() {
this.timer += _this.deltaTime;
if (this.timer > 100) {
if (this.alpha == 10) {
this.period = 0;
} else if (this.alpha == 0) {
this.period = 1;
}
if (this.period == 0) {
this.alpha -= 2;
} else if (this.period == 1) {
this.alpha += 2;
}
console.log(this.alpha);
this.timer = 0;
}
};
},
initParams() {
this.bgImg = new Image();
this.bgImg.src = require("./imgs/starrySky02.jpg");
for (let i = 0; i < this.starNum; i++) {
this.stars[i] = new Star();
if (i < this.starNum / 3) {
this.stars[i].init(2);
} else if (i >= this.starNum / 3 && i < (this.starNum * 2) / 3) {
this.stars[i].init(4);
} else {
this.stars[i].init(6);
}
}
this.lastTime = Date.now();
},
loop() {
const ctx = this.myCanvas.getContext("2d");
const _this = this;
let animation = window.webkitRequestAnimationFrame(function f() {
animation = requestAnimationFrame(f);
const now = Date.now();
_this.deltaTime = now - _this.lastTime;
_this.lastTime = now;
_this.drawSky(ctx);
_this.drawStars(ctx);
});
},
drawSky(ctx) {
ctx.drawImage(this.bgImg, 0, 0, this.cvw, this.cvh);
},
drawStars(ctx) {
for (let i = 0; i < this.starNum; i++) {
if (i < this.starNum / 2) {
this.stars[i].draw(ctx);
} else {
this.stars[i].update();
this.stars[i].draw(ctx);
}
}
}
}
};
</script>
starrySky.js:
export const Star = function() {
this.x;
this.y;
this.picNo;
this.timer;
};
export const StarF = function() {
this.x;
this.y;
this.radiu;
this.alpha;
this.period;
this.timer;
};
2.4 思路
??方法一中的思路是使用到图片切换而形成动画播放效果,这比较依赖源图片,通过此方法也能实现其他依赖图片切换的动画; ??方法二中的思路是绘制圆形星星,然后通过渐变色来控制星星颜色,通过改变星星半径以及渐变色半径来实现星光闪烁动画,大家可以通过调制自己喜欢的颜色来为星星填充色彩。
2.5 踩坑
- 每次更新图片或者星星大小的时候,需要对画布重新绘制,否则画布会遗留上一次的绘制,无法自动刷新;
context 无法被初始化为一个对象,每次使用的时候即用即取;这一点有待优化。
|