寫在前面
隨著七夕節(jié)的臨近,許多人都在尋找獨(dú)特而令人難忘的方式來表達(dá)愛意。在這個(gè)數(shù)字時(shí)代,結(jié)合創(chuàng)意和技術(shù),我們可以使用多種開發(fā)語言來編寫一個(gè)動(dòng)態(tài)的新型網(wǎng)頁,為這個(gè)浪漫的節(jié)日增添一份特別的禮物。在本文中,我們將帶你探索如何使用不同的開發(fā)語言來打造一個(gè)充滿心意的七夕表白網(wǎng)頁。
案例1:HTML + Three.js庫
網(wǎng)頁效果
當(dāng)使用 Three.js 庫來創(chuàng)建一個(gè)動(dòng)態(tài)的心形網(wǎng)頁時(shí),需要在 HTML 中引入 Three.js 庫,然后創(chuàng)建一個(gè)用于渲染的畫布,并編寫 JavaScript 代碼來生成動(dòng)畫效果。完整代碼如下(可直接復(fù)制粘貼到文本文檔或者開發(fā)軟件中運(yùn)行):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Heart Shape</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<canvas id="canvas"></canvas>
<script src="your-script.js"></script>
</body>
<script>
// 獲取畫布元素
const canvas = document.getElementById("canvas");
// 創(chuàng)建場(chǎng)景
const scene = new THREE.Scene();
// 創(chuàng)建攝像機(jī)
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 創(chuàng)建渲染器
const renderer = new THREE.WebGLRenderer({
canvas
});
renderer.setSize(window.innerWidth, window.innerHeight);
// 創(chuàng)建心形幾何體
const heartShape = new THREE.Shape();
heartShape.moveTo(0, -1);
heartShape.bezierCurveTo(0, -1.8, -1.5, -1.8, -1.5, -1);
heartShape.bezierCurveTo(-1.5, -0.2, -0.6, 0.6, 0, 1.4);
heartShape.bezierCurveTo(0.6, 0.6, 1.5, -0.2, 1.5, -1);
heartShape.bezierCurveTo(1.5, -1.8, 0, -1.8, 0, -1);
const heartGeometry = new THREE.ShapeGeometry(heartShape);
const heartMaterial = new THREE.MeshBasicMaterial({
color: 0xff4d6a
});
const heartMesh = new THREE.Mesh(heartGeometry, heartMaterial);
scene.add(heartMesh);
// 創(chuàng)建動(dòng)畫循環(huán)
const animate = () => {
requestAnimationFrame(animate);
// 使心形旋轉(zhuǎn)
heartMesh.rotation.x += 0.005;
heartMesh.rotation.y += 0.005;
renderer.render(scene, camera);
};
animate();
</script>
</html>
上述代碼使用到了 Three.js 庫來創(chuàng)建一個(gè)動(dòng)態(tài)的心形網(wǎng)頁。它創(chuàng)建了一個(gè)場(chǎng)景、攝像機(jī)、渲染器以及一個(gè)心形幾何體,然后通過動(dòng)畫循環(huán)讓心形幾何體旋轉(zhuǎn)起來。大家可以根據(jù)需要進(jìn)行調(diào)整和擴(kuò)展,例如添加光源、調(diào)整心形的大小和顏色等等。
案例2:HTML + CSS + JavaScript
網(wǎng)頁效果
完整代碼如下(可直接復(fù)制粘貼到文本文檔或者開發(fā)軟件中運(yùn)行):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>love++</title>
<style>
/* 設(shè)置全屏顯示 */
body {
margin: 0;
overflow: hidden;
background: #f6d8e2;
display: flex;
align-items: center;
justify-content: center;
}
/* 畫布樣式 */
canvas {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
pointer-events: none;
}
</style>
</head>
<body>
<!-- 畫布元素 -->
<canvas id="pinkboard"></canvas>
<script>
var settings = {
particles: {
length: 1000,
duration: 2,
velocity: 150,
effect: -0.5,
size: 30,
},
};
(function () {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
var cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
window.cancelAnimationFrame = cancelAnimationFrame;
if (!requestAnimationFrame || !cancelAnimationFrame) {
var lastTime = 0;
window.requestAnimationFrame = function (callback) {
var currentTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currentTime - lastTime));
var id = setTimeout(function () {
callback(currentTime + timeToCall);
}, timeToCall);
lastTime = currentTime + timeToCall;
return id;
};
window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}
})();
// 2D 坐標(biāo)點(diǎn)類
var Point = (function () {
function Point(x, y) {
this.x = x || 0;
this.y = y || 0;
}
Point.prototype.clone = function () {
return new Point(this.x, this.y);
};
Point.prototype.length = function (length) {
if (length === undefined)
return Math.sqrt(this.x * this.x + this.y * this.y);
this.normalize();
this.x *= length;
this.y *= length;
return this;
};
Point.prototype.normalize = function () {
var length = this.length();
this.x /= length;
this.y /= length;
return this;
};
return Point;
})();
// 粒子類
var Particle = (function () {
function Particle() {
this.position = new Point();
this.velocity = new Point();
this.acceleration = new Point();
this.age = 0;
}
Particle.prototype.initialize = function (x, y, dx, dy) {
this.position.x = x;
this.position.y = y;
this.velocity.x = dx;
this.velocity.y = dy;
this.acceleration.x = dx * settings.particles.effect;
this.acceleration.y = dy * settings.particles.effect;
this.age = 0;
};
Particle.prototype.update = function (deltaTime) {
this.position.x += this.velocity.x * deltaTime;
this.position.y += this.velocity.y * deltaTime;
this.velocity.x += this.acceleration.x * deltaTime;
this.velocity.y += this.acceleration.y * deltaTime;
this.age += deltaTime;
};
Particle.prototype.draw = function (context, image) {
function ease(t) {
return (--t) * t * t + 1;
}
var size = image.width * ease(this.age / settings.particles.duration);
context.globalAlpha = 1 - this.age / settings.particles.duration;
context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
};
return Particle;
})();
// 粒子池類
var ParticlePool = (function () {
var particles,
firstActive = 0,
firstFree = 0,
duration = settings.particles.duration;
function ParticlePool(length) {
particles = new Array(length);
for (var i = 0; i < particles.length; i++)
particles[i] = new Particle();
}
ParticlePool.prototype.add = function (x, y, dx, dy) {
particles[firstFree].initialize(x, y, dx, dy);
firstFree++;
if (firstFree == particles.length) firstFree = 0;
if (firstActive == firstFree) firstActive++;
if (firstActive == particles.length) firstActive = 0;
};
ParticlePool.prototype.update = function (deltaTime) {
var i;
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].update(deltaTime);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].update(deltaTime);
for (i = 0; i < firstFree; i++)
particles[i].update(deltaTime);
}
while (particles[firstActive].age >= duration && firstActive != firstFree) {
firstActive++;
if (firstActive == particles.length) firstActive = 0;
}
};
ParticlePool.prototype.draw = function (context, image) {
// 繪制活躍的粒子
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].draw(context, image);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].draw(context, image);
for (i = 0; i < firstFree; i++)
particles[i].draw(context, image);
}
};
return ParticlePool;
})();
(function (canvas) {
var context = canvas.getContext('2d'),
particles = new ParticlePool(settings.particles.length),
particleRate = settings.particles.length / settings.particles.duration,
time;
function pointOnHeart(t) {
return new Point(
180 * Math.pow(Math.sin(t), 3),
160 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
);
}
var image = (function () {
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d');
canvas.width = settings.particles.size;
canvas.height = settings.particles.size;
function to(t) {
var point = pointOnHeart(t);
point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
return point;
}
context.beginPath();
var t = -Math.PI;
var point = to(t);
context.moveTo(point.x, point.y);
while (t < Math.PI) {
t += 0.01;
point = to(t);
context.lineTo(point.x, point.y);
}
context.closePath();
context.fillStyle = '#fa759f';
context.fill();
var image = new Image();
image.src = canvas.toDataURL();
return image;
})();
function render() {
requestAnimationFrame(render);
var newTime = new Date().getTime() / 1000,
deltaTime = newTime - (time || newTime);
time = newTime;
context.clearRect(0, 0, canvas.width, canvas.height);
var amount = particleRate * deltaTime;
for (var i = 0; i < amount; i++) {
var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
var dir = pos.clone().length(settings.particles.velocity);
particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
}
particles.update(deltaTime);
particles.draw(context, image);
}
function onResize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.onresize = onResize;
setTimeout(function () {
onResize();
render();
}, 50);
})(document.getElementById('pinkboard'));
</script>
</body>
</html>
這段代碼實(shí)現(xiàn)了一個(gè)動(dòng)態(tài)的桃心效果,包含文字、動(dòng)畫和特效。在 HTML 部分,你可以看到一個(gè) canvas 元素,主要用于繪制動(dòng)態(tài)的桃心效果。然后使用 CSS 內(nèi)聯(lián)樣式來設(shè)置頁面的寬度、高度和背景顏色,以及對(duì) canvas 元素進(jìn)行定位和設(shè)置寬高。最后 JavaScript 實(shí)現(xiàn)動(dòng)態(tài)的桃心效果。
代碼中的 Particle 和 ParticlePool 類定義了粒子和粒子池,用于繪制和管理粒子。pointOnHeart 函數(shù)返回一個(gè)位于桃心曲線上的點(diǎn),用來初始化粒子的位置。然后使用 canvas 繪制小粒子創(chuàng)造一個(gè)形狀為桃心的動(dòng)態(tài)效果。代碼中 render 函數(shù)是主要的渲染函數(shù),用于更新粒子狀態(tài)、繪制粒子以及創(chuàng)建新的粒子。最后通過延遲一段時(shí)間調(diào)用 onResize 函數(shù),設(shè)置畫布大小并開始渲染。
案例3:Python環(huán)境 + Flask框架
網(wǎng)頁效果
我們也可以使用 Python 的 Web 框架(比如 Flask 或 Django)來搭建一個(gè)簡(jiǎn)單的服務(wù)器,將動(dòng)態(tài)心形網(wǎng)頁通過服務(wù)器動(dòng)態(tài)地呈現(xiàn)給訪問者。這里使用的是 flask 框架。
首先,打開開發(fā)工具(我用的是 PyCharm)并創(chuàng)建項(xiàng)目。安裝 Flask 庫(如果尚未安裝):
pip install Flask
創(chuàng)建一個(gè)名為 app.py 的 Python 文件,將以下代碼粘貼到文件中:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
在同一目錄下創(chuàng)建一個(gè)名為 templates 的文件夾,然后在該文件夾內(nèi)創(chuàng)建一個(gè)名為 index.html 的 HTML 文件,并將以下代碼粘貼到文件中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
<title>動(dòng)態(tài)愛心</title>
</head>
<body>
<canvas id="heartCanvas"></canvas>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>
在同一目錄下創(chuàng)建一個(gè)名為 static 的文件夾,然后在該文件夾內(nèi)創(chuàng)建一個(gè)名為 styles.css 的 CSS 文件,并將以下代碼粘貼到文件中:
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
在 static 文件夾內(nèi)創(chuàng)建一個(gè)名為 script.js 的 JavaScript 文件,并將以下代碼粘貼到文件中:
const canvas = document.getElementById("heartCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function drawHeart(x, y, size) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.bezierCurveTo(
x, y - size / 3,
x - size / 2, y,
x, y + size / 3
);
ctx.bezierCurveTo(
x + size / 2, y,
x, y - size / 3,
x, y
);
ctx.fillStyle = "#ff4d6a";
ctx.fill();
ctx.closePath();
}
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
const heartSize = 300;
const heartX = canvas.width / 2;
const heartY = canvas.height / 2 + Math.sin(Date.now() * 0.005) * 30;
drawHeart(heartX, heartY, heartSize);
}
animate();
打開命令行窗口,導(dǎo)航到包含這些文件的目錄,并運(yùn)行以下命令來啟動(dòng) Flask 應(yīng)用:
python app.py
最后在你的瀏覽器中訪問 http://127.0.0.1:5000/
查看生成的動(dòng)態(tài)愛心網(wǎng)頁。
整個(gè)項(xiàng)目目錄是這樣的;
案例使用函數(shù)簡(jiǎn)要介紹:文章來源:http://www.zghlxwxcb.cn/news/detail-664522.html
- ctx.beginPath():開始創(chuàng)建一個(gè)新的路徑。
- ctx.moveTo(x, y):將繪圖路徑的起始點(diǎn)移動(dòng)到給定的 (x, y) 坐標(biāo)。
- ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y):繪制一個(gè)三次貝塞爾曲線,其中 cp1 和 cp2 是控制點(diǎn)的坐標(biāo),x 和 y 是結(jié)束點(diǎn)的坐標(biāo)。這個(gè)方法用于繪制復(fù)雜的曲線。通過兩次調(diào)用 bezierCurveTo 方法,分別繪制出心形的上半部分和下半部分。
- ctx.fillStyle = “#ff4d6a”:設(shè)置填充顏色為"#ff4d6a",這是一種深紅色。
- ctx.fill():使用之前指定的顏色填充當(dāng)前路徑。
- ctx.closePath():關(guān)閉路徑,將當(dāng)前點(diǎn)連接到起始點(diǎn),并填充顏色。
結(jié)語
在這個(gè)特別的七夕節(jié),用自己的方式編寫一個(gè)動(dòng)態(tài)的表白網(wǎng)頁,將你的真摯情感展現(xiàn)給愛人。無論你選擇哪種開發(fā)語言,都可以運(yùn)用創(chuàng)意和技術(shù)來制作一個(gè)讓人難以忘懷的表白禮物。不僅僅是代碼,更是一份對(duì)愛人的真摯表達(dá),希望這個(gè)七夕會(huì)成為你們美好回憶的一部分。文章來源地址http://www.zghlxwxcb.cn/news/detail-664522.html
到了這里,關(guān)于情人節(jié)特別定制:多種語言編寫動(dòng)態(tài)愛心網(wǎng)頁(附完整代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!