???♂? 個(gè)人主頁@老虎也淘氣 個(gè)人主頁
???作者簡介:Python學(xué)習(xí)者
?? 希望大家多多支持我們一起進(jìn)步!??
如果文章對(duì)你有幫助的話,
歡迎評(píng)論 ??點(diǎn)贊???? 收藏 ??加關(guān)注
最近看到不少關(guān)于李峋同款愛心的視頻、文章,今天我們也分享一下李峋同款愛心 Python 代碼版。
簡單來說李峋同款愛心就是一個(gè)動(dòng)態(tài)的?型效果,主要 Python 代碼實(shí)現(xiàn)如下:
def __init__(self, generate_frame=20):
# 原始愛心坐標(biāo)集合
self._points = set()
# 邊緣擴(kuò)散效果點(diǎn)坐標(biāo)集合
self._edge_diffusion_points = set()
# 中心擴(kuò)散效果點(diǎn)坐標(biāo)集合
self._center_diffusion_points = set()
# 每幀動(dòng)態(tài)點(diǎn)坐標(biāo)
self.all_points = {}
self.build(2000)
self.random_halo = 1000
self.generate_frame = generate_frame
for frame in range(generate_frame):
self.calc(frame)
def build(self, number):
for _ in range(number):
t = random.uniform(0, 2 * pi)
x, y = heart(t)
self._points.add((x, y))
# 愛心內(nèi)擴(kuò)散
for _x, _y in list(self._points):
for _ in range(3):
x, y = scatter_inside(_x, _y, 0.05)
self._edge_diffusion_points.add((x, y))
# 愛心內(nèi)再次擴(kuò)散
point_list = list(self._points)
for _ in range(4000):
x, y = random.choice(point_list)
x, y = scatter_inside(x, y, 0.17)
self._center_diffusion_points.add((x, y))
@staticmethodstaticmethod
def calc_position(x, y, ratio):
force = 1 / (((x - X) ** 2 +
(y - Y) ** 2) ** 0.520)
dx = ratio * force * (x - X) + random.randint(-1, 1)
dy = ratio * force * (y - Y) + random.randint(-1, 1)
return x - dx, y - dy
def calc(self, generate_frame):
ratio = 10 * curve(generate_frame / 10 * pi)
halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
halo_number = int(
3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
all_points = []
# 光環(huán)
heart_halo_point = set()
for _ in range(halo_number):
t = random.uniform(0, 2 * pi)
x, y = heart(t, shrink_ratio=11.6)
x, y = shrink(x, y, halo_radius)
if (x, y) not in heart_halo_point:
heart_halo_point.add((x, y))
x += random.randint(-14, 14)
y += random.randint(-14, 14)
size = random.choice((1, 2, 2))
all_points.append((x, y, size))
# 輪廓
for x, y in self._points:
x, y = self.calc_position(x, y, ratio)
size = random.randint(1, 3)
all_points.append((x, y, size))
# 內(nèi)容
for x, y in self._edge_diffusion_points:
x, y = self.calc_position(x, y, ratio)
size = random.randint(1, 2)
all_points.append((x, y, size))
self.all_points[generate_frame] = all_points
for x, y in self._center_diffusion_points:
x, y = self.calc_position(x, y, ratio)
size = random.randint(1, 2)
all_points.append((x, y, size))
self.all_points[generate_frame] = all_points
init(self, generate_frame=20):
這是類的初始化方法,用于創(chuàng)建一個(gè)對(duì)象時(shí)進(jìn)行一些初始設(shè)置。
generate_frame:一個(gè)可選參數(shù),默認(rèn)值為20,表示生成動(dòng)畫的幀數(shù)。
self._points、self._edge_diffusion_points、self._center_diffusion_points:這些變量分別用于存儲(chǔ)原始愛心坐標(biāo)、邊緣擴(kuò)散效果點(diǎn)坐標(biāo)和中心擴(kuò)散效果點(diǎn)坐標(biāo)。
self.all_points:一個(gè)字典,用于存儲(chǔ)每一幀的動(dòng)態(tài)點(diǎn)坐標(biāo)。
self.build(2000):在初始化時(shí)調(diào)用 build 方法生成初始的愛心坐標(biāo)集合。
self.random_halo = 1000:一個(gè)用于生成隨機(jī)光環(huán)點(diǎn)的參數(shù)。
- build(self, number):
這個(gè)方法用于生成愛心的初始坐標(biāo),并在愛心內(nèi)進(jìn)行擴(kuò)散。
number:參數(shù),表示生成的愛心點(diǎn)的數(shù)量。
通過遍歷生成愛心的坐標(biāo),將其添加到 self._points 集合中。
進(jìn)行愛心內(nèi)的邊緣擴(kuò)散,將新生成的點(diǎn)添加到 self._edge_diffusion_points 集合中。
進(jìn)行愛心內(nèi)的再次擴(kuò)散,將新生成的點(diǎn)添加到 self._center_diffusion_points 集合中。
- @staticmethod calc_position(x, y, ratio):
這是一個(gè)靜態(tài)方法,用于計(jì)算點(diǎn)的新位置。
x, y:原始點(diǎn)的坐標(biāo)。
ratio:一個(gè)參數(shù),影響計(jì)算的力度。
通過一個(gè)力的公式,計(jì)算出點(diǎn)的新位置。
- calc(self, generate_frame):
這個(gè)方法用于計(jì)算每一幀的動(dòng)態(tài)點(diǎn)坐標(biāo)。
generate_frame:參數(shù),表示當(dāng)前幀數(shù)。
通過調(diào)用 calc_position 方法計(jì)算光環(huán)、輪廓和內(nèi)容的點(diǎn)的新位置。
將計(jì)算得到的點(diǎn)的坐標(biāo)和大小信息存儲(chǔ)在 all_points 字典中。
實(shí)現(xiàn)效果如下:
滿屏愛心代碼(修改名字版本)
<!DOCTYPE html>
<!-- saved from url=(0051)https://httishere.gitee.io/notion/v4/love-name.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Love you </title>
<style type="text/css">
body {
margin: 0;
overflow: hidden;
background: #000;
}
canvas {
position: absolute;
width: 100%;
height: 100%;
}
#pinkboard {
animation: anim 1.5s ease-in-out infinite;
-webkit-animation: anim 1.5s ease-in-out infinite;
-o-animation: anim 1.5s ease-in-out infinite;
-moz-animation: anim 1.5s ease-in-out infinite;
}
@keyframes anim {
0% {
transform: scale(0.8);
}
25% {
transform: scale(0.7);
}
50% {
transform: scale(1);
}
75% {
transform: scale(0.7);
}
100% {
transform: scale(0.8);
}
}
@-webkit-keyframes anim {
0% {
-webkit-transform: scale(0.8);
}
25% {
-webkit-transform: scale(0.7);
}
50% {
-webkit-transform: scale(1);
}
75% {
-webkit-transform: scale(0.7);
}
100% {
-webkit-transform: scale(0.8);
}
}
@-o-keyframes anim {
0% {
-o-transform: scale(0.8);
}
25% {
-o-transform: scale(0.7);
}
50% {
-o-transform: scale(1);
}
75% {
-o-transform: scale(0.7);
}
100% {
-o-transform: scale(0.8);
}
}
@-moz-keyframes anim {
0% {
-moz-transform: scale(0.8);
}
25% {
-moz-transform: scale(0.7);
}
50% {
-moz-transform: scale(1);
}
75% {
-moz-transform: scale(0.7);
}
100% {
-moz-transform: scale(0.8);
}
}
#name {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin-top: -20px;
font-size: 46px;
color: #ea80b0;
}
</style>
<script src="./ Love you _files/monitors.3.6.36.cn.js.下載" async="" crossorigin="anonymous"></script><script src="./ Love you _files/sentry.3.6.36.cn.js.下載" async="" crossorigin="anonymous"></script></head>
<body>
<canvas id="pinkboard" width="1707" height="868"></canvas>
<canvas id="canvas" width="1707" height="868"></canvas>
<script type="text/javascript">
const colors = [
"#eec996",
"#8fb7d3",
"#b7d4c6",
"#c3bedd",
"#f1d5e4",
"#cae1d3",
"#f3c89d",
"#d0b0c3",
"#819d53",
"#c99294",
"#cec884",
"#ff8e70",
"#e0a111",
"#fffdf6",
"#cbd7ac",
"#e8c6c0",
"#dc9898",
"#ecc8ba",
]; //用來設(shè)置的顏色
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
let count = 1;
var ww = window.innerWidth;
var wh = window.innerHeight;
var hearts = [];
function init() {
requestAnimationFrame(render);
canvas.width = ww;
canvas.height = wh;
for (var i = 0; i < 100; i++) {
hearts.push(new Heart());
}
}
function Heart() {
this.x = Math.random() * ww;
this.y = Math.random() * wh;
this.opacity = Math.random() * 0.5 + 0.5;
this.vel = {
x: (Math.random() - 0.5) * 4,
y: (Math.random() - 0.5) * 4,
};
this.targetScale = Math.random() * 0.15 + 0.02;
this.scale = this.targetScale * Math.random();
}
Heart.prototype.update = function (i) {
this.x += this.vel.x;
this.y += this.vel.y;
this.scale += (this.targetScale - this.scale) * 0.01;
if (this.x - this.width > ww || this.x + this.width < 0) {
this.scale = 0;
this.x = Math.random() * ww;
}
if (this.y - this.height > wh || this.y + this.height < 0) {
this.scale = 0;
this.y = Math.random() * wh;
}
this.width = 473.8;
this.height = 408.6;
};
Heart.prototype.draw = function (i) {
ctx.globalAlpha = this.opacity;
ctx.font = `${180 * this.scale}px "微軟雅黑"`;
// ctx.font="20px";
ctx.fillStyle = colors[i % 18];
ctx.fillText(
"kawsar",
this.x - this.width * 0.5,
this.y - this.height * 0.5,
this.width,
this.height
);
// ctx.drawImage(
// heartImage,
// this.x - this.width * 0.5,
// this.y - this.height * 0.5,
// this.width,
// this.heig
修改滿屏文字操作步驟
將上方的代碼全部復(fù)制
在電腦新建一個(gè)txt文件,命名love.txt
打開txt文件,黏貼代碼
將雙引號(hào)的文件給成你想要展示的文字,保存
將txt文件后綴改成 .hmtl
如果你嫌麻煩
可以下方后臺(tái)回復(fù)【愛心】直接下載編輯文章來源:http://www.zghlxwxcb.cn/news/detail-460667.html
結(jié)尾:
在代碼的海洋中,創(chuàng)意是燈塔,指引我們前行。李峋同款愛心Python代碼版,不僅僅是一段代碼,更是一個(gè)技術(shù)的表白和對(duì)編程藝術(shù)的追求。讓我們一起沉浸在這個(gè)充滿溫馨的代碼世界,激發(fā)我們內(nèi)心的創(chuàng)造力,為技術(shù)的發(fā)展貢獻(xiàn)一份獨(dú)特的力量。愿這份愛心代碼激發(fā)更多的創(chuàng)意,讓我們共同構(gòu)建一個(gè)更加美好的技術(shù)未來,希望大家喜歡。文章來源地址http://www.zghlxwxcb.cn/news/detail-460667.html
到了這里,關(guān)于李峋同款愛心Python代碼版來了的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!