针对https://github.com/bitjjj/JS-3D-TagCloud这个版本的做了移动端性能优化(使用transform做偏移及缩放,优化帧)。基本原理一致。
class TagCould {
    mcList = [];
    active = false; // 事件控制
    lasta = ;
    lastb = ;
    distr = true;
    mouseX = ;
    mouseY = ;
    aA = null;
    oDiv = null;
    _now = ;
    _then = Date.now();
    _delta = ;
    isStart = false;
    defaultOptions = {
        dtr: Math.PI / ,
        d: ,
        tspeed: ,
        size: ,
        howElliptical: ,
        fps: ,
        radius: (window.innerWidth + ) /  >  ?  : (window.innerWidth + ) / 
    };
    constructor(container, tags = [], options = {}) {
        this.container = container;
        this.tags = tags;
        options = Object.assign(this.defaultOptions, options);
        for (var p in options) {
            this[p] = options[p];
        }
        this._interval =  / this.fps;
        window.requestAnimationFrame =
            window.requestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.msRequestAnimationFrame;
        this.init();
    }
    init() {
        this.createTag();
        this.setOffset();
        this.sineCosine(, , );
        this.positionAll();
        this.tick();
        this.bindEvent();
    }
    start() {
        this.isStart = true;
    }
    pause() {
        this.isStart = false;
    }
    createTag() {
        this.oDiv = typeof this.container == 'string' ? document.getElementById(this.container) : this.container;
        for (let i = ; i < this.tags.length; i++) {
            const item = this.tags[i];
            let aElE = document.createElement('a');
            aElE.innerHTML = item.text;
            aElE.classList.add(`tag${i}`);
            aElE.classList.add(`tag`);
            item.className && aElE.classList.add(item.className);
            aElE.setAttribute('href', item.url || 'javascript:;');
            this.oDiv.appendChild(aElE);
        }
    }
    setOffset() {
        this.oDiv = typeof this.container == 'string' ? document.getElementById(this.container) : this.container;
        let i = ,
            oTag = null;
        this.aA = this.oDiv.getElementsByTagName('a');
        for (i = ; i < this.aA.length; i++) {
            oTag = {};
            oTag.offsetWidth = this.aA[i].offsetWidth;
            oTag.offsetHeight = this.aA[i].offsetHeight;
            this.mcList.push(oTag);
        }
    }
    bindEvent() {
        let self = this;
        document.addEventListener(
            'mouseover',
            function() {
                self.active = true;
            },
            false
        );
        document.addEventListener(
            'mouseout',
            function() {
                self.active = false;
            },
            false
        );
        document.addEventListener(
            'mousemove',
            function(evt) {
                //var oEvent=window.event || evt;
                self.onmove(window.event || evt);
            },
            false
        );
        document.addEventListener(
            'touchstart',
            function() {
                self.active = true;
            },
            false
        );
        document.addEventListener(
            'touchmove',
            function(evt) {
                self.onmove(window.event || evt);
            },
            false
        );
        document.addEventListener(
            'touchend',
            function() {
                self.active = false;
            },
            false
        );
    }
    tick() {
        if (window.requestAnimationFrame) {
            window.requestAnimationFrame(this.tick.bind(this));
            this._now = Date.now();
            this._delta = this._now - this._then;
            if (this._delta > this._interval) {
                // 这里不能简单then=now,否则还会出现上边简单做法的细微时间差问题。例如fps=10,每帧100ms,而现在每16ms(60fps)执行一次draw。16*7=112>100,需要7次才实际绘制一次。这个情况下,实际10帧需要112*10=1120ms>1000ms才绘制完成。
                this._then = this._now - this._delta % this._interval;
                this.update(); // ... Code for Drawing the Frame ...
            }
        } else {
            setTimeout(this._tick, this._interval);
            this.update();
        }
    }
    onmove(oEvent) {
        oEvent.preventDefault();
        if (oEvent.touches && oEvent.touches.length > ) {
            oEvent.clientX = oEvent.touches[].clientX;
            oEvent.clientY = oEvent.touches[].clientY;
        }
        this.mouseX = oEvent.clientX - (this.oDiv.offsetLeft + this.oDiv.offsetWidth / );
        this.mouseY = oEvent.clientY - (this.oDiv.offsetTop + this.oDiv.offsetHeight / );
        this.mouseX /= ;
        this.mouseY /= ;
    }
    update() {
        if (!this.isStart) {
            return false;
        }
        var a, b;
        if (this.active) {
            a = -Math.min(Math.max(-this.mouseY, -this.size), this.size) / this.radius * this.tspeed;
            b = Math.min(Math.max(-this.mouseX, -this.size), this.size) / this.radius * this.tspeed;
        } else {
            a = this.lasta * 0.999;
            b = this.lastb * 0.999;
        }
        this.lasta = a;
        this.lastb = b;
        if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
            return;
        }
        var c = ;
        this.sineCosine(a, b, c);
        for (var j = ; j < this.mcList.length; j++) {
            var rx1 = this.mcList[j].cx,
                ry1 = this.mcList[j].cy * this.ca + this.mcList[j].cz * -this.sa,
                rz1 = this.mcList[j].cy * this.sa + this.mcList[j].cz * this.ca,
                rx2 = rx1 * this.cb + rz1 * this.sb,
                ry2 = ry1,
                rz2 = rx1 * -this.sb + rz1 * this.cb,
                rx3 = rx2 * this.cc + ry2 * -this.sc,
                ry3 = rx2 * this.sc + ry2 * this.cc,
                rz3 = rz2;
            this.mcList[j].cx = rx3;
            this.mcList[j].cy = ry3;
            this.mcList[j].cz = rz3;
            var per = this.d / (this.d + rz3);
            this.mcList[j].x = this.howElliptical * rx3 * per - this.howElliptical * ;
            this.mcList[j].y = ry3 * per;
            this.mcList[j].scale = per;
            this.mcList[j].alpha = per;
            this.mcList[j].alpha = (this.mcList[j].alpha - 0.6) * ( / );
        }
        this.doPosition();
        this.depthSort();
    }
    depthSort() {
        var i = ,
            aTmp = [];
        for (i = ; i < this.aA.length; i++) {
            aTmp.push(this.aA[i]);
        }
        aTmp.sort(function(vItem1, vItem2) {
            if (vItem1.cz > vItem2.cz) {
                return -;
            } else if (vItem1.cz < vItem2.cz) {
                return ;
            } else {
                return ;
            }
        });
        for (i = ; i < aTmp.length; i++) {
            aTmp[i].style.zIndex = i;
        }
    }
    positionAll() {
        var phi = ,
            theta = ,
            max = this.mcList.length,
            i = ,
            aTmp = [],
            oFragment = document.createDocumentFragment();
        //随机排序
        for (i = ; i < this.aA.length; i++) {
            aTmp.push(this.aA[i]);
        }
        aTmp.sort(function() {
            return Math.random() < 0.5 ?  : -;
        });
        for (i = ; i < aTmp.length; i++) {
            oFragment.appendChild(aTmp[i]);
        }
        this.oDiv.appendChild(oFragment);
        for (var i = ; i < max + ; i++) {
            if (this.distr) {
                phi = Math.acos(- + ( * i - ) / max);
                theta = Math.sqrt(max * Math.PI) * phi;
            } else {
                phi = Math.random() * Math.PI;
                theta = Math.random() * ( * Math.PI);
            }
            //坐标变换
            this.mcList[i - ].cx = this.radius * Math.cos(theta) * Math.sin(phi);
            this.mcList[i - ].cy = this.radius * Math.sin(theta) * Math.sin(phi);
            this.mcList[i - ].cz = this.radius * Math.cos(phi);
            this.aA[i - ].style.webkitTransform = `translate(${this.mcList[i - ].cx +
                this.oDiv.offsetWidth /  -
                this.mcList[i - ].offsetWidth /  +
                'px'},${this.mcList[i - ].cy +
                this.oDiv.offsetHeight /  -
                this.mcList[i - ].offsetHeight /  +
                'px'}) scale(${this.mcList[i - ].scale || })`;
        }
    }
    doPosition() {
        var l = this.oDiv.offsetWidth / ,
            t = this.oDiv.offsetHeight / ;
        for (var i = ; i < this.mcList.length; i++) {
            this.aA[i].style.webkitTransform = `translate(${this.mcList[i].cx +
                l -
                this.mcList[i].offsetWidth /  +
                'px'},${this.mcList[i].cy + t - this.mcList[i].offsetHeight /  + 'px'}) scale(${this.mcList[i]
                .scale})`;
            this.aA[i].style.opacity = this.mcList[i].alpha;
        }
    }
    sineCosine(a, b, c) {
        this.sa = Math.sin(a * this.dtr);
        this.ca = Math.cos(a * this.dtr);
        this.sb = Math.sin(b * this.dtr);
        this.cb = Math.cos(b * this.dtr);
        this.sc = Math.sin(c * this.dtr);
        this.cc = Math.cos(c * this.dtr);
    }
}
 
export default TagCould;

tagCould3d 移动端优化版的更多相关文章

  1. 【gRPC】C++异步服务端优化版,多服务接口样例

    官方的C++异步服务端API样例可读性并不好,理解起来非常的费劲,各种状态机也并不明了,整个运行过程也容易读不懂,因此此处参考网上的博客进行了重写,以求顺利读懂. C++异步服务端实例,详细注释版 g ...

  2. 全面解密QQ红包技术方案:架构、技术实现、移动端优化、创新玩法等

    本文来自腾讯QQ技术团队工程师许灵锋.周海发的技术分享. 一.引言 自 2015 年春节以来,QQ 春节红包经历了企业红包(2015 年).刷一刷红包(2016 年)和 AR 红包(2017 年)几个 ...

  3. 通用PE工具箱 4.0精简优化版

    通用PE工具箱 4.0精简优化版 经用过不少 WinPE 系统,都不是很满意,普遍存在篡改主页.添加广告链接至收藏夹.未经允许安装推广软件等流氓行为,还集成了诸多不常用的工具,令人头疼不已.那么今天给 ...

  4. GHOST WIN7系统64位经典优化版 V2016年

    来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统32,64位经典优化版 V2016年3月 系统概述 深度技术ghost win7系统64位经典优化版适用于笔 ...

  5. Chrome 扩展程序 CrxMouse 优化版 v3.0.1

    说明 CrxMouse 原版更新至 v2.7.8,跟进升级优化版至 v3.0.1. 改动说明: 1. 去除可能存在的后台数据上传隐患: 2. 解决鼠标右键拖动时的轨迹漂移问题. 3. 加入部分默认设置 ...

  6. POJ-2533最长上升子序列(DP+二分)(优化版)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Acc ...

  7. 【译】Optimize for mobile-移动端优化

    移动端优化 由于移动设备有限的CPU处理能力,移动网络的高往返时间,以及移动应用的快速增长,对移动端的理解以及优化相对于PC端来讲变得更加重要.Page Speed Insights 现在可以让你轻松 ...

  8. 搜狗拼音输入法 V9.1.0.2589 最新去广告精简优化版

    搜狗拼音输入法9.0 正式版例行发布,最新版字母代号b,详细版本号为v9.1.0.2589:搜狗拼音输入法是电脑装机必备软件,版本有传统版和智慧版之分,其打字超准.词库超大.速度飞快.外观漂亮,因此使 ...

  9. 【转】Appium 优化版

    Appium 开源分享优化版 之前分享过PageObject+Python+Appium 本版本是对上次版本较大改版,主要解决了: 失败重连一次(默认一次)可配置多次 基于appium1.7.1 ui ...

随机推荐

  1. 救救孩子吧,到现在还搞不懂TCP的三次握手四次挥手

    本文在个人技术博客同步发布,详情可用力戳 亦可扫描屏幕右侧二维码关注个人公众号,公众号内有个人联系方式,等你来撩...   前几天发了一个朋友圈,发现暗恋已久的女生给我点了个赞,于是我当晚辗转反侧.彻 ...

  2. CentOS ISO 下载地址

    x86_64:https://wiki.centos.org/Download ARM:http://mirror.nsc.liu.se/centos-store/altarch/ http://dl ...

  3. Java数据结构——红黑树

    红黑树介绍红黑树(Red-Black Tree),它一种特殊的二叉查找树.执行查找.插入.删除等操作的时间复杂度为O(logn). 红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点 ...

  4. CODING DevOps 代码质量实战系列第二课: PHP 版

    讲师介绍 杨周 CODING DevOps 架构师 CODING 布道师 连续创业者.DIY/Linux 玩家.知乎小 V,曾在创新工场.百度担任后端开发.十余年一线研发和带队经验,经历了 ToB.T ...

  5. python练习 数字不同数之和+人名最多数统计

    数字不同数之和 描述 获得用户输入的一个整数N,输出N中所出现不同数字的和.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬ ...

  6. 构建docker私有仓库+k8s-pod应用

    环境版本系统:centos7.4docker-compose version 1.26.2docker-py version: 4.3.0CPython version: 2.7.5docker-ve ...

  7. 单表千万行数据库 LIKE 搜索优化手记

    我们经常在数据库中使用 LIKE 操作符来完成对数据的模糊搜索,LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 如果需要查找客户表中所有姓氏是“张”的数据,可以使用下面的 SQL 语句 ...

  8. codeblocks显示:不支持的16位应用程序 解决办法

    我是win10 64位系统,写c++运行就会显示不兼容16位应用程序.以前编出来的exe还能用,今天编出的就炸了. 试了用vs编译.vs能用. 试了网上找的各种解决方案, 360修复, 注册表, 重构 ...

  9. 蒲公英 · JELLY技术周刊 Vol.20: Vue3 极致优化——分析 Vue3 Compiler 告诉你为什么这么快

    蒲公英 · JELLY技术周刊 Vol.20 性能优化是一条无尽的路,我们总是可以找到各种途径去提升体验,不论是响应时间还是按需加载,亦或是根据框架或者组件有针对性的优化都会是不错的方法.如果你在使用 ...

  10. 【转载】pandas常用函数

    原文链接:https://www.cnblogs.com/rexyan/p/7975707.html 一.import语句 import pandas as pd import numpy as np ...