博客园美化

博客园页脚小鱼

看到别的大佬的博客页脚的小鱼,好喜欢。

搜了类似的小鱼教程,特效放在了jsi-flying-fish-container,因为默认这个container的颜色和父级的颜色一样,所以我的页脚有一片灰色的长条很突兀(首页图的颜色应该是),改了一下container背景颜色为半透明白色(我的背景是白色的)。过渡的时候感觉自然多了。

参考教程:

博客园美化:添加底部小鱼特效 - Shu_HowZ - 博客园

博客园美化记(二) - 孤逐王 - 博客园

<!-- 小鱼 start -->
<div id="jsi-flying-fish-container" class="container"></div>
<script src="https://blog-static.cnblogs.com/files/wonux/myfish.js"></script>
<style>
  @media only screen and (max-width: 767px){
  #sidebar_search_box input[type=text]{width:calc(100% - 24px)}
  }
  #jsi-flying-fish-container{
    opacity: 0.3;
    position:fixed;
    width: 100%;
    height: 20%;
    bottom: 0;
    pointer-events: none;
    background-color: rgba(255, 255, 255, 0); /* 设置为透明 这行是我这个菜鸟加的 不太靠谱的一行*/ 
  }
</style>
<!-- 小鱼 end -->

鱼的颜色设置

通过修改小鱼的js源码 blog-static.cnblogs.com/files/wonux/myfish.js

可以实现各种效果。

下面是deepseek生成的教程

在这段代码中,鱼的颜色是通过 context.fillStyle 设置的。具体来说,鱼的颜色是在 RENDERER 对象的 render 方法中定义的:

render : function(){
    requestAnimationFrame(this.render);
    this.controlStatus();
    this.context.clearRect(0, 0, this.width, this.height);
    this.context.fillStyle = 'hsl(200, 100%, 50%)'; // 设置填充颜色为 HSL 格式的蓝色
    
    for(var i = 0, count = this.fishes.length; i < count; i++){
        this.fishes[i].render(this.context);
    }
    // 其他渲染逻辑...
}

颜色的形成

  1. fillStyle 属性

    • fillStyle 是 Canvas 2D API 的一个属性,用于设置填充颜色。
    • 在这段代码中,颜色被设置为 hsl(200, 100%, 50%),这是一种 HSL(色相、饱和度、亮度)颜色表示法。
      • 色相 (Hue)200,表示蓝色的范围。
      • 饱和度 (Saturation)100%,表示颜色非常鲜艳。
      • 亮度 (Lightness)50%,表示颜色既不偏暗也不偏亮。
  2. 鱼的颜色

    • FISH 对象的 render 方法中,鱼的身体和鳍的绘制都使用了 context.fill(),而 fill() 方法会使用当前设置的 fillStyle 颜色。
    • 因此,鱼的颜色是由 hsl(200, 100%, 50%) 决定的,呈现为一种鲜艳的蓝色。

修改鱼的颜色

如果你想修改鱼的颜色,可以更改 fillStyle 的值。例如:

  • 如果你想将鱼的颜色改为红色,可以将 fillStyle 设置为 hsl(0, 100%, 50%)
  • 如果你想将鱼的颜色改为绿色,可以将 fillStyle 设置为 hsl(120, 100%, 50%)

修改后的代码如下:

render : function(){
    requestAnimationFrame(this.render);
    this.controlStatus();
    this.context.clearRect(0, 0, this.width, this.height);
    this.context.fillStyle = 'hsl(0, 100%, 50%)'; // 红色
    // this.context.fillStyle = 'hsl(120, 100%, 50%)'; // 绿色
    
    for(var i = 0, count = this.fishes.length; i < count; i++){
        this.fishes[i].render(this.context);
    }
    // 其他渲染逻辑...
}

动态颜色

如果你想让鱼的颜色动态变化,可以在 render 方法中动态修改 fillStyle。例如,根据时间变化颜色:

render : function(){
    requestAnimationFrame(this.render);
    this.controlStatus();
    this.context.clearRect(0, 0, this.width, this.height);
    
    // 动态颜色:根据时间变化色相
    var hue = (Date.now() / 50) % 360; // 色相随时间变化
    this.context.fillStyle = `hsl(${hue}, 100%, 50%)`;
    
    for(var i = 0, count = this.fishes.length; i < count; i++){
        this.fishes[i].render(this.context);
    }
    // 其他渲染逻辑...
}

这样,鱼的颜色会随着时间的推移而动态变化。

总结

鱼的颜色是由 context.fillStyle 决定的,默认设置为 hsl(200, 100%, 50%)(蓝色)。你可以通过修改 fillStyle 的值来改变鱼的颜色,甚至实现动态颜色效果。

动态颜色小鱼代码

<!-- 小鱼 start -->
<div id="jsi-flying-fish-container" class="container"></div>
<style>
  @media only screen and (max-width: 767px){
  #sidebar_search_box input[type=text]{width:calc(100% - 24px)}
  }
  #jsi-flying-fish-container{
    opacity: 0.3;
    position:fixed;
    width: 100%;
    height: 20%;
    bottom: 0;
    pointer-events: none;
    background-color: rgba(255, 255, 255, 0); /* 设置为半透明白色 */ 
  }
</style>
<!-- 小鱼 end -->

<script>
var RENDERER = {
	POINT_INTERVAL : 5,
	FISH_COUNT : 3,
	MAX_INTERVAL_COUNT : 50,
	INIT_HEIGHT_RATE : 0.5,
	THRESHOLD : 50,
	
	init : function(){
		this.setParameters();
		this.reconstructMethods();
		this.setup();
		this.bindEvent();
		this.render();
	},
	setParameters : function(){
		this.$window = $(window);
		this.$container = $('#jsi-flying-fish-container');
		this.$canvas = $('<canvas />');
		this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d');
		this.points = [];
		this.fishes = [];
		this.watchIds = [];
	},
	createSurfacePoints : function(){
		var count = Math.round(this.width / this.POINT_INTERVAL);
		this.pointInterval = this.width / (count - 1);
		this.points.push(new SURFACE_POINT(this, 0));
		
		for(var i = 1; i < count; i++){
			var point = new SURFACE_POINT(this, i * this.pointInterval),
				previous = this.points[i - 1];
				
			point.setPreviousPoint(previous);
			previous.setNextPoint(point);
			this.points.push(point);
		}
	},
	reconstructMethods : function(){
		this.watchWindowSize = this.watchWindowSize.bind(this);
		this.jdugeToStopResize = this.jdugeToStopResize.bind(this);
		this.startEpicenter = this.startEpicenter.bind(this);
		this.moveEpicenter = this.moveEpicenter.bind(this);
		this.reverseVertical = this.reverseVertical.bind(this);
		this.render = this.render.bind(this);
	},
	setup : function(){
		this.points.length = 0;
		this.fishes.length = 0;
		this.watchIds.length = 0;
		this.intervalCount = this.MAX_INTERVAL_COUNT;
		this.width = this.$container.width();
		this.height = this.$container.height();
		this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500;
		this.$canvas.attr({width : this.width, height : this.height});
		this.reverse = false;
		
		this.fishes.push(new FISH(this));
		this.createSurfacePoints();
	},
	watchWindowSize : function(){
		this.clearTimer();
		this.tmpWidth = this.$window.width();
		this.tmpHeight = this.$window.height();
		this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL));
	},
	clearTimer : function(){
		while(this.watchIds.length > 0){
			clearTimeout(this.watchIds.pop());
		}
	},
	jdugeToStopResize : function(){
		var width = this.$window.width(),
			height = this.$window.height(),
			stopped = (width == this.tmpWidth && height == this.tmpHeight);
			
		this.tmpWidth = width;
		this.tmpHeight = height;
		
		if(stopped){
			this.setup();
		}
	},
	bindEvent : function(){
		this.$window.on('resize', this.watchWindowSize);
		this.$container.on('mouseenter', this.startEpicenter);
		this.$container.on('mousemove', this.moveEpicenter);
		this.$container.on('click', this.reverseVertical);
	},
	getAxis : function(event){
		var offset = this.$container.offset();
		
		return {
			x : event.clientX - offset.left + this.$window.scrollLeft(),
			y : event.clientY - offset.top + this.$window.scrollTop()
		};
	},
	startEpicenter : function(event){
		this.axis = this.getAxis(event);
	},
	moveEpicenter : function(event){
		var axis = this.getAxis(event);
		
		if(!this.axis){
			this.axis = axis;
		}
		this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y);
		this.axis = axis;
	},
	generateEpicenter : function(x, y, velocity){
		if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){
			return;
		}
		var index = Math.round(x / this.pointInterval);
		
		if(index < 0 || index >= this.points.length){
			return;
		}
		this.points[index].interfere(y, velocity);
	},
	reverseVertical : function(){
		this.reverse = !this.reverse;
		
		for(var i = 0, count = this.fishes.length; i < count; i++){
			this.fishes[i].reverseVertical();
		}
	},
	controlStatus : function(){
		for(var i = 0, count = this.points.length; i < count; i++){
			this.points[i].updateSelf();
		}
		for(var i = 0, count = this.points.length; i < count; i++){
			this.points[i].updateNeighbors();
		}
		if(this.fishes.length < this.fishCount){
			if(--this.intervalCount == 0){
				this.intervalCount = this.MAX_INTERVAL_COUNT;
				this.fishes.push(new FISH(this));
			}
		}
	},
	render : function(){
		requestAnimationFrame(this.render);
		this.controlStatus();
		this.context.clearRect(0, 0, this.width, this.height);

        // 动态颜色:根据时间变化色相
        var hue = (Date.now() / 50) % 360; // 色相随时间变化
        this.context.fillStyle = `hsl(${hue}, 100%, 50%)`;
		
		for(var i = 0, count = this.fishes.length; i < count; i++){
			this.fishes[i].render(this.context);
		}
		this.context.save();
		this.context.globalCompositeOperation = 'xor';
		this.context.beginPath();
		this.context.moveTo(0, this.reverse ? 0 : this.height);
		
		for(var i = 0, count = this.points.length; i < count; i++){
			this.points[i].render(this.context);
		}
		this.context.lineTo(this.width, this.reverse ? 0 : this.height);
		this.context.closePath();
		this.context.fill();
		this.context.restore();
	}
};
var SURFACE_POINT = function(renderer, x){
	this.renderer = renderer;
	this.x = x;
	this.init();
};
SURFACE_POINT.prototype = {
	SPRING_CONSTANT : 0.03,
	SPRING_FRICTION : 0.9,
	WAVE_SPREAD : 0.3,
	ACCELARATION_RATE : 0.01,
	
	init : function(){
		this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE;
		this.height = this.initHeight;
		this.fy = 0;
		this.force = {previous : 0, next : 0};
	},
	setPreviousPoint : function(previous){
		this.previous = previous;
	},
	setNextPoint : function(next){
		this.next = next;
	},
	interfere : function(y, velocity){
		this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity);
	},
	updateSelf : function(){
		this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height);
		this.fy *= this.SPRING_FRICTION;
		this.height += this.fy;
	},
	updateNeighbors : function(){
		if(this.previous){
			this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height);
		}
		if(this.next){
			this.force.next = this.WAVE_SPREAD * (this.height - this.next.height);
		}
	},
	render : function(context){
		if(this.previous){
			this.previous.height += this.force.previous;
			this.previous.fy += this.force.previous;
		}
		if(this.next){
			this.next.height += this.force.next;
			this.next.fy += this.force.next;
		}
		context.lineTo(this.x, this.renderer.height - this.height);
	}
};
var FISH = function(renderer){
	this.renderer = renderer;
	this.init();
};
FISH.prototype = {
	GRAVITY : 0.4,
	
	init : function(){
		this.direction = Math.random() < 0.5;
		this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD;
		this.previousY = this.y;
		this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1);
		
		if(this.renderer.reverse){
			this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10);
			this.vy = this.getRandomValue(2, 5);
			this.ay = this.getRandomValue(0.05, 0.2);
		}else{
			this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10);
			this.vy = this.getRandomValue(-5, -2);
			this.ay = this.getRandomValue(-0.2, -0.05);
		}
		this.isOut = false;
		this.theta = 0;
		this.phi = 0;
	},
	getRandomValue : function(min, max){
		return min + (max - min) * Math.random();
	},
	reverseVertical : function(){
		this.isOut = !this.isOut;
		this.ay *= -1;
	},
	controlStatus : function(context){
		this.previousY = this.y;
		this.x += this.vx;
		this.y += this.vy;
		this.vy += this.ay;
		
		if(this.renderer.reverse){
			if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
				this.vy -= this.GRAVITY;
				this.isOut = true;
			}else{
				if(this.isOut){
					this.ay = this.getRandomValue(0.05, 0.2);
				}
				this.isOut = false;
			}
		}else{
			if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
				this.vy += this.GRAVITY;
				this.isOut = true;
			}else{
				if(this.isOut){
					this.ay = this.getRandomValue(-0.2, -0.05);
				}
				this.isOut = false;
			}
		}
		if(!this.isOut){
			this.theta += Math.PI / 20;
			this.theta %= Math.PI * 2;
			this.phi += Math.PI / 30;
			this.phi %= Math.PI * 2;
		}
		this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY);
		
		if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){
			this.init();
		}
	},
	render : function(context){
		context.save();
		context.translate(this.x, this.y);
		context.rotate(Math.PI + Math.atan2(this.vy, this.vx));
		context.scale(1, this.direction ? 1 : -1);
		context.beginPath();
		context.moveTo(-30, 0);
		context.bezierCurveTo(-20, 15, 15, 10, 40, 0);
		context.bezierCurveTo(15, -10, -20, -15, -30, 0);
		context.fill();
		
		context.save();
		context.translate(40, 0);
		context.scale(0.9 + 0.2 * Math.sin(this.theta), 1);
		context.beginPath();
		context.moveTo(0, 0);
		context.quadraticCurveTo(5, 10, 20, 8);
		context.quadraticCurveTo(12, 5, 10, 0);
		context.quadraticCurveTo(12, -5, 20, -8);
		context.quadraticCurveTo(5, -10, 0, 0);
		context.fill();
		context.restore();
		
		context.save();
		context.translate(-3, 0);
		context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1));
		
		context.beginPath();
		
		if(this.renderer.reverse){
			context.moveTo(5, 0);
			context.bezierCurveTo(10, 10, 10, 30, 0, 40);
			context.bezierCurveTo(-12, 25, -8, 10, 0, 0);
		}else{
			context.moveTo(-5, 0);
			context.bezierCurveTo(-10, -10, -10, -30, 0, -40);
			context.bezierCurveTo(12, -25, 8, -10, 0, 0);
		}
		context.closePath();
		context.fill();
		context.restore();
		context.restore();
		this.controlStatus(context);
	}
};
$(function(){
	RENDERER.init();
});
</script>

评论区自定义图片

url可以写你找到的任何图片(包括gif动图,jpg静态图片等)。

以我在网上随便找的gif动图为例。

<!-- 评论区美化 -->
<style>
    textarea#tbCommentBody {
	width: calc(100% - 20px);
	border-radius: 10px;
	outline: 0;
	padding: 10px;
	height: 200px;
	position: relative;
	background: #fff url(https://s1.aigei.com/prevfiles/3ee85a2cf1b3408f8647e48fd0a36715.gif?e=2051020800&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:LD99uvo6ZrCwo4tBvveLbdbDixw=);
	background-size: contain;
	background-repeat: no-repeat;
	background-position: right;
	resize: vertical;
}
</style>

复制代码到页脚。

posted @   快乐星猫i  阅读(30)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示