准备之js实现动画

1.定时器

const animateMethod = (e) => {
    var left = 0,
        flag = true,
        e = document.getElementById(e);
    // e.setAttribute('style', 'left:0px');
    // e.style['left'] = '0px';
    e.style.setProperty('left', '0px');
    /**
     * e.style.left 只适用于内联样式
     * 那么我们外部引用的css样式,要实现操作样式的效果,我个人认为这样最简单()
     * 1.先为其设置一个行内样式(要操作的样式)
     *      1.1     e.setAttribute('style', 'left:0px');
     *      1.2     e.style['left']='0px';
     *      1.3     e.style.setProperty('left', '0px');//如果需要加important 可以直接加第三个参数('important')
     * 2.之后就可以使用e.style.left操作对应样式了
     * */

    setInterval(() => {
        // 相当于
        // left == 0 ? flag = true :
        //     (left == 100 ? flag = false : '');
        left == 0 ? flag = true : left == 100 ? flag = false : '';
        flag ? e.style.left = ` ${left++}px` : e.style.left = ` ${left--}px`;
    }, 1000 / 60)
};

2.requestanimframe

特点:

* requestAnimationFrame与浏览器绘制同步

* 浏览器页面每次要重绘,就会触发requestAnimationFrame;直接使用它无疑性能优化了很多

* setInterval和setTimeout 只是通过传递的第二个参数去设置把该动画代码放在线程队列中等待执行的时间,如果前面有其他任务,需要等到前面任务完成后再执行

* 帧速率无法指定,即无法制定浏览器按时绘制下一帧

* requestAnimationFrame 不需要调用者指定帧速率,浏览器会自行决定最佳的帧效率。

// 兼容性处理
window.requestAnimFrame = (function() {
    return (
        window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback, element) {
            window.setTimeout(callback, 1000 / 60)
        }
    )
})();
window.cancelAnimFrame = window.cancelAnimationFrame ||
    window.webkitCancelAnimationFrame ||
    window.mozCancelAnimationFrame ||
    window.msCancelAnimationFrame ||
    window.oCancelAnimationFrame ||
    function(id) {
        //为了使setTimteout的尽可能的接近每秒60帧的效果
        window.clearTimeout(id);
    };


// requestAnimFrame
    var elem = document.getElementById('requestanimframe');
    elem.style.setProperty('left', '0px');
    var startTime = undefined,
        count = 0;

    function render(time) {
        time = Date.now();
        if (startTime === undefined) {
            startTime = time;
        }
        elem.style.left = ((time - startTime) / 10) % 100 + 'px';
    }
    elem.onclick = function() {
        (function animloop() {
            render();
            window.requestAnimFrame(animloop, elem);
            document.getElementById('stop').onclick = function() {
                window.cancelAnimFrame(animloop);
            }
        })()
    };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三维粒子动画可以使用 `THREE.Points` 和 `THREE.PointCloud` 来实现。 首先,你需要准备粒子的纹理贴图和顶点数据。 然后,你可以这样创建一个粒子系统: ``` const particles = new THREE.Points(geometry, material); scene.add(particles); ``` 其中,`geometry` 是粒子的顶点几何体,`material` 是粒子的材质。 你还可以使用动画循环更新粒子的位置,例如: ``` function animate() { particles.rotation.x += 0.01; particles.rotation.y += 0.02; renderer.render(scene, camera); requestAnimationFrame(animate); } ``` 完整的示例代码如下: ``` const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); const texture = new THREE.TextureLoader().load('particle.png'); const material = new THREE.PointsMaterial({ size: 0.1, transparent: true, depthWrite: false, map: texture }); const geometry = new THREE.BufferGeometry(); const vertices = []; for (let i = 0; i < 10000; i++) { vertices.push(THREE.Math.randFloatSpread(50)); vertices.push(THREE.Math.randFloatSpread(50)); vertices.push(THREE.Math.randFloatSpread(50)); } geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); const particles = new THREE.Points(geometry, material); scene.add(particles); camera.position.z = 100; function animate() { particles.rotation.x += 0.01; particles.rotation.y += 0.02; renderer.render(scene, camera); requestAnimationFrame(animate); } animate(); ``` 希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值