在vuepress博客添加樱花特效(vue樱花组件源码)

news/2024/12/2 19:02:33/

文章目录

  • 写在前面
    • 樱花的源码
    • 在vuepress使用

写在前面

写过博客的同学都知道,飘落樱花是一个比较常见的博客特效。
平时都是用插件来实现的,有想过去了解它到底是怎么实现的吗?

  • 它是用canvas实现的
  • 就针对vuepress,我们需要写一个vue组件来实现效果

本篇文章会贴出樱花的vue组件源码vuepress使用樱花的步骤

可以先来我博客看看效果,哈哈 博客效果演示

樱花的源码

<template><div class="Sakura"><canvas id="canvas_sakura" ref="canvas_sakura" :style="{ zIndex: zIndex }"></canvas></div>
</template><script>
class Sakura {constructor(x, y, s, r, fn, that, img) {this.x = x;this.y = y;this.s = s;this.r = r;this.fn = fn;this.that = that;this.img = img;}draw (cxt) {cxt.save();// eslint-disable-next-line no-unused-varsvar xc = 40 * this.s / 4;cxt.translate(this.x, this.y);cxt.rotate(this.r);cxt.drawImage(this.img, 0, 0, 40 * this.s, 40 * this.s)cxt.restore();}update () {this.x = this.fn.x(this.x, this.y);this.y = this.fn.y(this.y, this.y);this.r = this.fn.r(this.r);if (this.x > window.innerWidth ||this.x < 0 ||this.y > window.innerHeight ||this.y < 0) {this.r = this.that.getRandom('fnr');if (Math.random() > 0.4) {this.x = this.that.getRandom('x');this.y = 0;this.s = this.that.getRandom('s');this.r = this.that.getRandom('r');} else {this.x = window.innerWidth;this.y = this.that.getRandom('y');this.s = this.that.getRandom('s');this.r = this.that.getRandom('r');}}}
}
class SakuraList {constructor() {this.list = []}push (sakura) {this.list.push(sakura);}update () {for (var i = 0, len = this.list.length; i < len; i++) {this.list[i].update();}}draw (cxt) {for (var i = 0, len = this.list.length; i < len; i++) {this.list[i].draw(cxt);}}get (i) {return this.list[i];}size () {return this.list.length;}
}
export default {// eslint-disable-next-line vue/multi-word-component-namesname: 'Sakura',data () {return {staticx: false,stop: null,num: 50,show: true,zIndex: 0,}},mounted () {this.$nextTick(() => {if (this.show) {this.startSakura()}})},methods: {getRandom (option) {var ret, random;switch (option) {case 'x':ret = Math.random() * window.innerWidth;break;case 'y':ret = Math.random() * window.innerHeight;break;case 's':ret = Math.random();break;case 'r':ret = Math.random() * 6;break;case 'fnx':random = -0.5 + Math.random() * 1;// eslint-disable-next-line no-unused-varsret = function (x, y) {return x + 0.15 * random - 1.7 * 0.3;};break;case 'fny':random = 1.5 + Math.random() * 0.7ret = function (x, y) {return y + 0.3 * random;};break;case 'fnr':random = Math.random() * 0.03;ret = function (r) {return r + random;};break;}return ret;},startSakura () {let that = this// eslint-disable-next-line no-global-assignrequestAnimationFrame = window.requestAnimationFrame ||window.mozRequestAnimationFrame ||window.webkitRequestAnimationFrame ||window.msRequestAnimationFrame ||window.oRequestAnimationFrame;var canvas = document.getElementById('canvas_sakura');this.staticx = true;this.$refs.canvas_sakura.width = window.innerWidth;this.$refs.canvas_sakura.height = window.innerHeight;var cxt = this.$refs.canvas_sakura.getContext('2d');var sakuraList = new SakuraList();const img = new Image();img.src = '/Hundred-refining-into-Immortals/sakura.png'; //樱花效果图相对路径for (var i = 0; i < that.num; i++) {let sakura, randomX, randomY, randomS, randomR, randomFnx, randomFny, randomFnR;randomX = this.getRandom('x');randomY = this.getRandom('y');randomR = this.getRandom('r');randomS = this.getRandom('s');randomFnx = this.getRandom('fnx');randomFny = this.getRandom('fny');randomFnR = this.getRandom('fnr');sakura = new Sakura(randomX, randomY, randomS, randomR, {x: randomFnx,y: randomFny,r: randomFnR}, this, img);sakura.draw(cxt);sakuraList.push(sakura);}this.stop = requestAnimationFrame(function fn () {cxt.clearRect(0, 0, canvas.width, canvas.height);sakuraList.update();sakuraList.draw(cxt);that.stop = requestAnimationFrame(fn);})},stopp () {if (this.staticx) {var child = document.getElementById("canvas_sakura");child.parentNode.removeChild(child);window.cancelAnimationFrame(this.stop);this.staticx = false;} else {this.startSakura();}}}
}
</script><style scoped>
#canvas_sakura {pointer-events: none;position: fixed;top: 0;left: 0;
}
</style>

在vuepress使用

  • .vuepress/components文件夹下面新建一个Sakura.vue文件,内容就是上面的源码
    • 在vuepress里面会自动吧上面文件夹里面的vue文件挂载到全局
    • 这样全局就可以使用
  • 在你想出现樱花的页面调用组件就可以了,这样<Sakura></Sakura>

http://www.ppmy.cn/news/399780.html

相关文章

三月,和她一起看一次樱花吧(vue实现樱花漫天效果)

目录 1 前言2 樱花雨实现2.1 环境2.2 效果2.3 源码与源图2.4 踩坑 1 前言 去年三月 她的长发在风中随樱花起舞 难以忘怀的不止是樱花 更是她的笑靥 此时此刻 恰如彼时彼刻&#xff1b; 希望各位能与命中注定的他或她&#xff0c;在樱花雨中来一场邂逅&#xff1b; 2 樱花雨实现…

樱花树(2)

Hi~ o(*&#xffe3;▽&#xffe3;*)ブ大家好今天我又来分享代码啦&#xff01;还是老样子不喜勿喷mua 原码如下&#xff1a; from turtle import * from random import * from math import * def tree(n,l): pd()#下笔 #阴影效果 t cos(radians(heading()45)…

酷炫樱花飞舞

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>樱花飞舞</TITLE><META NAME"Generator" CONTENT"EditPlus"><META NAME"Author" CONTENT""&…

樱花樱花想见你:关于不一样的爱

樱花樱花想见你 歌曲起源 编辑 作者高野健一的创作灵感来源于 西加奈子写的小说《樱》 [2] &#xff0c;该小说讲诉了一条叫“樱”的小狗与其主人一家的感人故事。高野健一据此在歌词中构造一个失去女儿 [3] 的父亲的形象&#xff0c;并以此展开后续歌词创作。 本系列歌曲共有三…

P1833 樱花

文章目录 R e s u l t Result Result H y p e r l i n k Hyperlink Hyperlink D e s c r i p t i o n Description Description S o l u t i o n Solution Solution C o d e 1 Code1 Code1 C o d e 2 Code2 Code2 R e s u l t Result Result H y p e r l i n k Hyperlink Hyper…

樱花

美丽的樱花~ 求不定方程&#xff1a; 1 x 1 y 1 n ! \frac{1}{x}\frac{1}{y}\frac{1}{n!} x1​y1​n!1​ 的正整数解 (x,y) 的数目。 Input 一个整数 n。 Output 一个整数&#xff0c;表示有多少对 (x,y) 满足题意。答案对 1097 取模。 Example 样例输入 2 样例输出 3…

IE不兼容ES6箭头函数的解决方法

第一种&#xff1a; npm install babel-polyfill --save 页面中引用"polyfill.js" 和 "browser.min.js" JS代码script标签加上 type"text/babel" <script type"text/babel"></script> polyfill为什么可以&#xff1f; va…

樱花何处有?动态樱花飘落图

用言辞表达爱就如用一台有故障的发报机发送密码情报&#xff0c;总是不确定怎样才能被收到。——阿兰德波顿《爱情笔记》 掏心话&#xff1a;以前丢失的东西&#xff0c;到最后或许都得慢慢找回来。 前几天武大的云赏樱甚是好看&#xff0c;可惜本人没有亲自去看过 **花语&a…