Skip to content

Commit 80a679e

Browse files
committed
add heart
1 parent deba7d1 commit 80a679e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3613
-0
lines changed

满屏爱心和文字动画.html

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<canvas id="drawHeart"></canvas>
13+
</body>
14+
<script>
15+
const canvas = document.getElementById('drawHeart');
16+
const ctx = canvas.getContext('2d');
17+
let wW = window.innerWidth;
18+
let wH = window.innerHeight;
19+
const num = 100;
20+
const hearts = [];
21+
const heartImage = new Image();
22+
heartImage.src = 'data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><path id="heart" d="M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z" fill="red"/></svg>';
23+
class Heart {
24+
constructor(type) {
25+
this.type = type;
26+
// 初始化生成范围
27+
this.x = Math.random() * wW;
28+
this.y = Math.random() * wH;
29+
this.opacity = Math.random() * .5 + .5;
30+
// 偏移量
31+
this.vel = {
32+
x: (Math.random() - .5) * 5,
33+
y: (Math.random() - .5) * 5
34+
}
35+
this.initialW = wW * .5;
36+
this.initialH = wH * .5;
37+
// 缩放比例
38+
this.targetScale = Math.random() * .15 + .02; // 最小0.02
39+
this.scale = Math.random() * this.targetScale;
40+
// 文字位置
41+
this.fx = Math.random() * wW;
42+
this.fy = Math.random() * wH;
43+
this.fs = Math.random() * 10;
44+
this.text = getText();
45+
this.fvel = {
46+
x: (Math.random() - .5) * 5,
47+
y: (Math.random() - .5) * 5,
48+
f: (Math.random() - .5) * 2
49+
}
50+
}
51+
draw() {
52+
ctx.save();
53+
ctx.globalAlpha = this.opacity;
54+
ctx.drawImage(heartImage, this.x, this.y, this.width, this.height);
55+
// ctx.scale(this.scale + 1, this.scale + 1);
56+
if (!this.type) {
57+
// 设置文字属性
58+
ctx.fillStyle = getColor();
59+
ctx.font = 'italic ' + this.fs + 'px sans-serif';
60+
// 填充字符串
61+
ctx.fillText(this.text, this.fx, this.fy);
62+
}
63+
ctx.restore();
64+
}
65+
update() {
66+
this.x += this.vel.x;
67+
this.y += this.vel.y;
68+
if (this.x - this.width > wW || this.x + this.width < 0) {
69+
// 重新初始化位置
70+
this.scale = 0;
71+
this.x = Math.random() * wW;
72+
this.y = Math.random() * wH;
73+
}
74+
if (this.y - this.height > wH || this.y + this.height < 0) {
75+
// 重新初始化位置
76+
this.scale = 0;
77+
this.x = Math.random() * wW;
78+
this.y = Math.random() * wH;
79+
}
80+
// 放大
81+
this.scale += (this.targetScale - this.scale) * .1;
82+
this.height = this.scale * this.initialH;
83+
this.width = this.height * 1.4;
84+
// -----文字-----
85+
this.fx += this.fvel.x;
86+
this.fy += this.fvel.y;
87+
this.fs += this.fvel.f;
88+
if (this.fs > 50) {
89+
this.fs = 2;
90+
}
91+
if (this.fx - this.fs > wW || this.fx + this.fs < 0) {
92+
// 重新初始化位置
93+
this.fx = Math.random() * wW;
94+
this.fy = Math.random() * wH;
95+
}
96+
if (this.fy - this.fs > wH || this.fy + this.fs < 0) {
97+
// 重新初始化位置
98+
this.fx = Math.random() * wW;
99+
this.fy = Math.random() * wH;
100+
}
101+
}
102+
}
103+
function getText() {
104+
const val = Math.random() * 10;
105+
if (val > 1 && val <= 3) {
106+
return 'always';
107+
} else if (val > 3 && val <= 5) {
108+
return 'zzy';
109+
} else if (val > 5 && val <= 8) {
110+
return 'taylor swift';
111+
} else {
112+
return 'I Love You';
113+
}
114+
}
115+
function getColor() {
116+
const val = Math.random() * 10;
117+
if (val > 0 && val <= 1) {
118+
return '#00f';
119+
} else if (val > 1 && val <= 2) {
120+
return '#f00';
121+
} else if (val > 2 && val <= 3) {
122+
return '#0f0';
123+
} else if (val > 3 && val <= 4) {
124+
return '#368';
125+
} else if (val > 4 && val <= 5) {
126+
return '#666';
127+
} else if (val > 5 && val <= 6) {
128+
return '#333';
129+
} else if (val > 6 && val <= 7) {
130+
return '#f50';
131+
} else if (val > 7 && val <= 8) {
132+
return '#e96d5b';
133+
} else if (val > 8 && val <= 9) {
134+
return '#5be9e9';
135+
} else {
136+
return '#d41d50';
137+
}
138+
}
139+
function init() {
140+
canvas.width = wW;
141+
canvas.height = wH;
142+
for (let i = 0; i < num; i++) {
143+
hearts.push(new Heart(i % 5));
144+
}
145+
render();
146+
}
147+
148+
function render() {
149+
ctx.clearRect(0, 0, wW, wH);
150+
for (let i = 0; i < hearts.length; i++) {
151+
hearts[i].draw();
152+
hearts[i].update();
153+
}
154+
setTimeout(render, 60);
155+
}
156+
157+
init();
158+
window.addEventListener('resize', function () {
159+
canvas.width = wW = window.innerWidth;
160+
canvas.height = wH = window.innerHeight;
161+
});
162+
</script>
163+
164+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Anton Mudrenok (https://codepen.io/mudrenok/pen/XMobMa)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# I Love You (mo.js animation)
2+
3+
A Pen created on CodePen.io. Original URL: [https://codepen.io/mudrenok/pen/XMobMa](https://codepen.io/mudrenok/pen/XMobMa).
4+
5+
<img src="https://media.giphy.com/media/3oKIPjBudwsQAuuu8E/giphy.gif" height="180" width="180"/>
6+
7+
<a href="https://dribbble.com/shots/3278810-I-Love-You-Responsive">Dribbble Shot</a> by <a href="https://twitter.com/galgalshir">Gal Shir</a>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en" >
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>CodePen - I Love You (mo.js animation)</title>
6+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
7+
<link rel="stylesheet" href="./style.css">
8+
9+
</head>
10+
<body>
11+
<!-- partial:index.partial.html -->
12+
<div class="container">
13+
<svg class="svg-container" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 200">
14+
<line class="line line--left" x1="10" y1="17" x2="10" y2="183"> </line>
15+
<line class="line line--rght" x1="490" y1="17" x2="490" y2="183"> </line>
16+
<g>
17+
<path class="lttr lttr--I" d="M42.2,73.9h11.4v52.1H42.2V73.9z"></path>
18+
<path class="lttr lttr--L" d="M85.1,73.9h11.4v42.1h22.8v10H85.1V73.9z"></path>
19+
<path class="lttr lttr--O" d="M123.9,100c0-15.2,11.7-26.9,27.2-26.9s27.2,11.7,27.2,26.9s-11.7,26.9-27.2,26.9S123.9,115.2,123.9,100zM166.9,100c0-9.2-6.8-16.5-15.8-16.5c-9,0-15.8,7.3-15.8,16.5s6.8,16.5,15.8,16.5C160.1,116.5,166.9,109.2,166.9,100z"> </path>
20+
<path class="lttr lttr--V" d="M180.7,73.9H193l8.4,22.9c1.7,4.7,3.5,9.5,5,14.2h0.1c1.7-4.8,3.4-9.4,5.2-14.3l8.6-22.8h11.7l-19.9,52.1h-11.5L180.7,73.9z"></path>
21+
<path class="lttr lttr--E" d="M239.1,73.9h32.2v10h-20.7v10.2h17.9v9.5h-17.9v12.4H272v10h-33V73.9z"></path>
22+
<path class="lttr lttr--Y" d="M315.8,102.5l-20.1-28.6H309l6.3,9.4c2,3,4.2,6.4,6.3,9.6h0.1c2-3.2,4.1-6.4,6.3-9.6l6.3-9.4h12.9l-19.9,28.5v23.6h-11.4V102.5z"></path>
23+
<path class="lttr lttr--O2" d="M348.8,100c0-15.2,11.7-26.9,27.2-26.9c15.5,0,27.2,11.7,27.2,26.9s-11.7,26.9-27.2,26.9C360.5,126.9,348.8,115.2,348.8,100z M391.8,100c0-9.2-6.8-16.5-15.8-16.5c-9,0-15.8,7.3-15.8,16.5s6.8,16.5,15.8,16.5C385,116.5,391.8,109.2,391.8,100z"></path>
24+
<path class="lttr lttr--U" d="M412.4,101.1V73.9h11.4v26.7c0,10.9,2.4,15.9,11.5,15.9c8.4,0,11.4-4.6,11.4-15.8V73.9h11v26.9c0,7.8-1.1,13.5-4,17.7c-3.7,5.3-10.4,8.4-18.7,8.4c-8.4,0-15.1-3.1-18.8-8.5C413.4,114.2,412.4,108.5,412.4,101.1z"></path>
25+
</g>
26+
</svg>
27+
<div class="mo-container"> </div>
28+
</div>
29+
30+
31+
<audio class="blup" style="display: none">
32+
<source src="https://www.freesound.org/data/previews/265/265115_4373976-lq.mp3" type="audio/ogg">
33+
</audio >
34+
<audio class="blop" style="display: none">
35+
<source src="https://www.freesound.org/data/previews/265/265115_4373976-lq.mp3" type="audio/ogg">
36+
</audio>
37+
<div class="sound">sound</div>
38+
<!-- partial -->
39+
<script src='https://cdn.jsdelivr.net/mojs/latest/mo.min.js'></script><script src="./script.js"></script>
40+
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)