表白html特效在线,html5 canvas酷炫表白爱心动画特效

特效描述:酷炫表白动画 表白爱心动画 动画特效。html5爱心动画

代码结构

1. 引入JS

2. HTML代码

var container;

var camera, scene, renderer;

var group, text, plane;

var targetRotation = 0;

var targetRotationOnMouseDown = 0;

var mouseX = 0;

var mouseXOnMouseDown = 0;

var windowHalfX = window.innerWidth / 2;

var windowHalfY = window.innerHeight / 2;

var heartShape, particleCloud, sparksEmitter, emitterPos;

var _rotation = 0;

var timeOnShapePath = 0;

init();

animate();

function init() {

container = document.createElement('div');

document.body.appendChild(container);

//相机

camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);

camera.position.set(0, 150, 800);

//场景

scene = new THREE.Scene();

group = new THREE.Group();

scene.add(group);

// Get text from hash

var string = "千千";

var hash = document.location.hash.substr(1);

if (hash.length !== 0) {

string = hash;

}

var text3d = new THREE.TextGeometry(string, {

size: 80,

height: 20,

curveSegments: 2,

font: "helvetiker"

});

text3d.computeBoundingBox();

var centerOffset = -0.5 * (text3d.boundingBox.max.x - text3d.boundingBox.min.x);

var textMaterial = new THREE.MeshBasicMaterial({color: Math.random() * 0xffffff, overdraw: 0.5});

text = new THREE.Mesh(text3d, textMaterial);

// Potentially, we can extract the vertices or faces of the text to generate particles too.

// Geo > Vertices > Position

text.position.x = centerOffset;

text.position.y = 100;

text.position.z = 0;

text.rotation.x = 0;

text.rotation.y = Math.PI * 2;

group.add(text);

particleCloud = new THREE.Object3D(); // Just a group

particleCloud.y = 800;

group.add(particleCloud);

// Create Particle Systems

// Heart

var x = 0, y = 0;

heartShape = new THREE.Shape();

heartShape.moveTo(x + 25, y + 25);

heartShape.bezierCurveTo(x + 25, y + 25, x + 20, y, x, y);

heartShape.bezierCurveTo(x - 30, y, x - 30, y + 35, x - 30, y + 35);

heartShape.bezierCurveTo(x - 30, y + 55, x - 10, y + 77, x + 25, y + 95);

heartShape.bezierCurveTo(x + 60, y + 77, x + 80, y + 55, x + 80, y + 35);

heartShape.bezierCurveTo(x + 80, y + 35, x + 80, y, x + 50, y);

heartShape.bezierCurveTo(x + 35, y, x + 25, y + 25, x + 25, y + 25);

var hue = 0;

var hearts = function(context) {

context.globalAlpha = 0.5;

var x = 0, y = 0;

context.scale(0.05, -0.05); // Scale so canvas render can redraw within bounds

context.beginPath();

// From http://blog.burlock.org/html5/130-paths

context.bezierCurveTo(x + 2.5, y + 2.5, x + 2.0, y, x, y);

context.bezierCurveTo(x - 3.0, y, x - 3.0, y + 3.5, x - 3.0, y + 3.5);

context.bezierCurveTo(x - 3.0, y + 5.5, x - 1.0, y + 7.7, x + 2.5, y + 9.5);

context.bezierCurveTo(x + 6.0, y + 7.7, x + 8.0, y + 5.5, x + 8.0, y + 3.5);

context.bezierCurveTo(x + 8.0, y + 3.5, x + 8.0, y, x + 5.0, y);

context.bezierCurveTo(x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5);

context.fill();

context.lineWidth = 0.5; //0.05

context.stroke();

}

var setTargetParticle = function() {

var material = new THREE.SpriteCanvasMaterial({

program: hearts

});

material.color.setHSL(hue, 1, 0.75);

hue += 0.001;

if (hue > 1)

hue -= 1;

particle = new THREE.Sprite(material);

particle.scale.x = particle.scale.y = Math.random() * 40 + 40;

particleCloud.add(particle);

return particle;

};

var onParticleCreated = function(p) {

p.target.position.copy(p.position);

};

var onParticleDead = function(particle) {

particle.target.visible = false;

particleCloud.remove(particle.target);

};

sparksEmitter = new SPARKS.Emitter(new SPARKS.SteadyCounter(160));

emitterpos = new THREE.Vector3();

sparksEmitter.addInitializer(new SPARKS.Position(new SPARKS.PointZone(emitterpos)));

sparksEmitter.addInitializer(new SPARKS.Lifetime(0, 2));

sparksEmitter.addInitializer(new SPARKS.Target(null, setTargetParticle));

sparksEmitter.addInitializer(new SPARKS.Velocity(new SPARKS.PointZone(new THREE.Vector3(0, -50, 10))));

// TOTRY Set velocity to move away from centroid

sparksEmitter.addAction(new SPARKS.Age());

//sparksEmitter.addAction(new SPARKS.Accelerate(0.2));

sparksEmitter.addAction(new SPARKS.Move());

sparksEmitter.addAction(new SPARKS.RandomDrift(50, 50, 2000));

sparksEmitter.addCallback("created", onParticleCreated);

sparksEmitter.addCallback("dead", onParticleDead);

sparksEmitter.addCallback("updated", function(particle) {

particle.target.position.copy(particle.position);

});

sparksEmitter.start();

// End Particles

renderer = new THREE.CanvasRenderer();

renderer.setClearColor(0xf0f0f0);

renderer.setPixelRatio(window.devicePixelRatio);

renderer.setSize(window.innerWidth, window.innerHeight);

container.appendChild(renderer.domElement);

document.addEventListener('mousedown', onDocumentMouseDown, false);

document.addEventListener('touchstart', onDocumentTouchStart, false);

document.addEventListener('touchmove', onDocumentTouchMove, false);

//

window.addEventListener('resize', onWindowResize, false);

}

function onWindowResize() {

windowHalfX = window.innerWidth / 2;

windowHalfY = window.innerHeight / 2;

camera.aspect = window.innerWidth / window.innerHeight;

camera.updateProjectionMatrix();

renderer.setSize(window.innerWidth, window.innerHeight);

}

//

document.addEventListener('mousemove', onDocumentMouseMove, false);

function onDocumentMouseDown(event) {

event.preventDefault();

mouseXOnMouseDown = event.clientX - windowHalfX;

targetRotationOnMouseDown = targetRotation;

if (sparksEmitter.isRunning()) {

sparksEmitter.stop();

} else {

sparksEmitter.start();

}

}

function onDocumentMouseMove(event) {

mouseX = event.clientX - windowHalfX;

targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;

}

function onDocumentTouchStart(event) {

if (event.touches.length == 1) {

event.preventDefault();

mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;

targetRotationOnMouseDown = targetRotation;

}

}

function onDocumentTouchMove(event) {

if (event.touches.length == 1) {

event.preventDefault();

mouseX = event.touches[ 0 ].pageX - windowHalfX;

targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;

}

}

//

function animate() {//更新场景

requestAnimationFrame(animate);

render();

}

function render() {

timeOnShapePath += 0.0337;

if (timeOnShapePath > 1)

timeOnShapePath -= 1;

// TODO Create a PointOnShape Action/Zone in the particle engine

var pointOnShape = heartShape.getPointAt(timeOnShapePath);

emitterpos.x = pointOnShape.x * 5 - 100;

emitterpos.y = -pointOnShape.y * 5 + 400;

// Pretty cool effect if you enable this

// particleCloud.rotation.y += 0.05;

group.rotation.y += (targetRotation - group.rotation.y) * 0.05;

renderer.render(scene, camera);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 HTML5 Canvas 的全屏酷炫星光闪烁 3D 视差背景动画特效HTML 代码: ```html <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Canvas Star Field</title> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), stars = [], particles = [], maxStars = 1300, maxParticles = 100; // 创建星星 for (var i = 0; i < maxStars; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 1.5, depth: Math.random() * 2000 + 500 }); } // 创建粒子 function emitParticle() { if (particles.length < maxParticles) { var particle = { x: canvas.width / 2, y: canvas.height / 2, vx: Math.random() * 2 - 1, vy: Math.random() * 2 - 1, radius: Math.random() * 2 + 2, alpha: Math.random() * 0.5 + 0.5, life: Math.random() * 200 + 100 }; particles.push(particle); } } // 更新粒子 function updateParticles() { particles.forEach(function(particle, index) { particle.x += particle.vx; particle.y += particle.vy; particle.life--; particle.alpha -= 0.01; if (particle.life <= 0 || particle.alpha <= 0) { particles.splice(index, 1); } }); } // 绘制星星 function drawStar(star) { var x = (star.x - canvas.width / 2) * (star.depth / canvas.width), y = (star.y - canvas.height / 2) * (star.depth / canvas.width), radius = star.radius * (star.depth / canvas.width); context.beginPath(); context.arc(x + canvas.width / 2, y + canvas.height / 2, radius, 0, Math.PI * 2); context.fillStyle = '#fff'; context.fill(); } // 绘制粒子 function drawParticle(particle) { context.beginPath(); context.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); context.fillStyle = 'rgba(255, 255, 255, ' + particle.alpha + ')'; context.fill(); } // 绘制 function draw() { context.clearRect(0, 0, canvas.width, canvas.height); // 绘制星星 stars.forEach(function(star) { drawStar(star); }); // 绘制粒子 particles.forEach(function(particle) { drawParticle(particle); }); } // 循环 function loop() { emitParticle(); updateParticles(); draw(); requestAnimationFrame(loop); } // 初始化画布 function initCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; window.addEventListener('resize', function() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); } // 初始化 function init() { initCanvas(); loop(); } // 执行初始化 init(); </script> </body> </html> ``` 这段代码创建了一个全屏的 Canvas 画布,并在其中绘制了星星和粒子。通过调整粒子的数量和大小,以及星星的深度和大小,可以得到不同的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值