html5 飄雪花動畫(增加背景音樂)

人偶_阿諾發表於2019-02-16

冬天到了,少不了這樣的動畫效果(領導讓加聖誕背景音樂)
效果檢視地址:
1、PC端:https://www.huijindaicn.com/i…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{$title}</title>

    <style type="text/css">
        body {
            /*background:url(images/bg.jpg) center no-repeat;*/
            margin:0px;
        }*
        image{ width:100%; }
        canvas{ position: fixed; top:0; }
        /* 背景音樂開關按鈕 */
        .music{
            width:50px;height:50px;
            background:url(/cghd/201612/images/close.png) no-repeat;
            background-color: #b91b10;
            position: fixed;
            top:30%;
            right:10px;
            cursor: pointer;
            border-radius: 25px;
            z-index: 9999;
        }
        .music:hover{
            background-color: #b91b10;
        }
    </style>
</head>
<body>
    <img src="/cghd/201612/images/bg.jpg">
    <script type="text/javascript" src="/cghd/201612/js/ThreeCanvas.js"></script>
    <script type="text/javascript" src="/cghd/201612/js/Snow.js"></script>
    <script type="text/javascript">
        var SCREEN_WIDTH = window.innerWidth;
        var SCREEN_HEIGHT = window.innerHeight;
        var container;
        var particle;
        var camera;
        var scene;
        var renderer;
        var mouseX = 0;
        var mouseY = 0;
        var windowHalfX = SCREEN_WIDTH / 2;
        var windowHalfY = SCREEN_HEIGHT / 2;
        var particles = []; 
        var particleImage = new Image();//THREE.ImageUtils.loadTexture( "img/ParticleSmoke.png" );
        particleImage.src = `/cghd/201612/images/ParticleSmoke.png`; 
    
        function init() {
            container = document.createElement(`div`);
            document.body.appendChild(container);
    
            camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
            camera.position.z = 1000;
    
            scene = new THREE.Scene();
            scene.add(camera);
                
            renderer = new THREE.CanvasRenderer();
            renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
            var material = new THREE.ParticleBasicMaterial( { map: new THREE.Texture(particleImage) } );
                
            for (var i = 0; i < 500; i++) {
    
                particle = new Particle3D( material);
                particle.position.x = Math.random() * 2000 - 1000;
                particle.position.y = Math.random() * 2000 - 1000;
                particle.position.z = Math.random() * 2000 - 1000;
                particle.scale.x = particle.scale.y =  1;
                scene.add( particle );
                
                particles.push(particle); 
            }
    
            container.appendChild( renderer.domElement );
            document.addEventListener( `mousemove`, onDocumentMouseMove, false );
            setInterval( loop, 1000 / 60 );
        }
    
        function onDocumentMouseMove( event ) {
            mouseX = event.clientX - windowHalfX;
            mouseY = event.clientY - windowHalfY;
        }
    
        function loop() {
            for(var i = 0; i<particles.length; i++)
            {
                var particle = particles[i]; 
                particle.updatePhysics(); 
                with(particle.position)
                {
                    if(y<-1000) y+=2000; 
                    if(x>1000) x-=2000; 
                    else if(x<-1000) x+=2000; 
                    if(z>1000) z-=2000; 
                    else if(z<-1000) z+=2000; 
                }                
            }
            camera.position.x += ( mouseX - camera.position.x ) * 0.05;
            camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
            camera.lookAt(scene.position); 
            renderer.render( scene, camera );    
        }
        window.onload = init;
        
        /*背景音樂開關控制*/
        $(`.music`).on({
            `click`:function(){
                var flag = $(this).attr(`data-flag`);
                if(flag == `open`){
                    audio.pause();
                    $(this).attr(`data-flag`,`close`);
                    $(this).css(`background`,`url(/cghd/201612/images/open.png)`);
                    $(this).css(`background-color`,`#31628d`);
                }else{
                    audio.play();
                    $(this).attr(`data-flag`,`open`);
                    $(this).css(`background`,`url(/cghd/201612/images/close.png)`);
                    $(this).css(`background-color`,`#b91b10`);
                }
            },
        });
    </script>
</body>
</html>

2、WAP端:https://www.huijindaicn.com/w…
注意:在wap端,canvas 元素在頁面最上層,遮擋了頁面下層所有元素,導致無法選中頁面下層的元素,所以另外加了觸控事件,其他與PC端相同
為頁面新增觸控事件,使 canvas 元素 在使用者觸控螢幕時排在頁面元素的最底層,程式碼如下:

var canvas = document.getElementsByTagName(`canvas`);

document.addEventListener(`touchstart`, function(){
    canvas[0].setAttribute("style","z-index:-1");
}, false);

document.addEventListener(`touchmove`, function(){
    canvas[0].setAttribute("style","z-index:-1");
}, false);

document.addEventListener(`touchend`, function(){
    canvas[0].removeAttribute("style");
}, false);

相關文章