<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      代碼改變世界

      react動(dòng)畫(huà)難寫(xiě)?試試react版transformjs

      2016-12-19 09:37  【當(dāng)耐特】  閱讀(3840)  評(píng)論(0)    收藏  舉報(bào)

      簡(jiǎn)介

      transformjs在非react領(lǐng)域用得風(fēng)生水起,那么react技術(shù)棧的同學(xué)能用上嗎?答案是可以的。junexie童鞋已經(jīng)造了個(gè)react版本

      動(dòng)畫(huà)實(shí)現(xiàn)方式

      傳統(tǒng) web 動(dòng)畫(huà)的兩種方式

      1. 純粹的CSS3 :如:transition/animation+transform(大名鼎鼎的animate.css)
      2. JS + CSS3 transition或者animation:這里第一種一樣,只是通過(guò)js里add class和remove class去增加或者移除對(duì)應(yīng)的動(dòng)畫(huà)
      3. 純粹JS控制時(shí)間軸:第一和第二種都是自帶時(shí)間軸,使用 setInterval / setTimeout / requestAnimationFrame 不斷地修改 DOM 的 style 屬性產(chǎn)生動(dòng)畫(huà)

      對(duì)應(yīng)在react中

      使用CSS3

      • 使用 ReactCSSTransitionGroup 來(lái)實(shí)現(xiàn)
      • 相關(guān)動(dòng)畫(huà)的class都有對(duì)應(yīng)的state,修改state相當(dāng)于增加或者移除class,也就相當(dāng)于js里add class和remove class去增加或者移除對(duì)應(yīng)的動(dòng)畫(huà)

      純粹JS控制時(shí)間軸

      • 仍然使用 setInterval / setTimeout / requestAnimationFrame,修改某個(gè) state 值,然后映射到 component 的 style 上。

      這里很明顯,方案1和方案2可應(yīng)對(duì)簡(jiǎn)單場(chǎng)景(如沒(méi)有prop change 回調(diào)等),方案3可編程性最大,最靈活,可以適合復(fù)雜的動(dòng)畫(huà)場(chǎng)景或者承受復(fù)雜的交互場(chǎng)景。

      安裝

      npm install css3transform-react
      

      API

      //set "translateX", "translateY", "translateZ", "scaleX", "scaleY", "scaleZ", "rotateX", "rotateY", "rotateZ", "skewX", "skewY", "originX", "originY", "originZ"
      render() {
          return (
              <Transform
                translateX={100}
                scaleX={0.5}
                originX={0.5}>
                <div>sth</div>
              </Transform>
          );
      }
      
      // you can also use other porps, such as "className" or "style"
      render() {
          return (
              <Transform
                translateX={100}
                className="ani"
                style={{width: '100px', background: 'red'}}
                <div>sth</div>
              </Transform>
          );
      }
      

      通過(guò)上面的聲明,就可以設(shè)置或者讀取"translateX", "translateY", "translateZ", "scaleX", "scaleY", "scaleZ", "rotateX", "rotateY", "rotateZ", "skewX", "skewY", "originX", "originY", "originZ"!

      方便吧!

      使用姿勢(shì)

      import React, { Component } from 'react';
      import { render } from 'react-dom';
      
      import Transform from '../../transform.react.js';
      
      class Root extends Component {
      
        constructor(props, context) {
          super(props, context);
      
          this.state = {
            el1: {rotateZ: 0},
            el2: {rotateY: 0}
          };
      
          this.animate = this.animate.bind(this);
        }
      
        animate() {
          this.setState({
            el1: {rotateZ: this.state.el1.rotateZ + 1},
            el2: {rotateY: this.state.el2.rotateY + 1}
          }, () => {
            requestAnimationFrame(this.animate);
          });
      
        }
      
        componentDidMount() {
          setTimeout(this.animate, 500);
        }
      
        render() {
          return (
            <div>
              <Transform rotateZ={this.state.el1.rotateZ} className="test" style={{'backgroundColor': 'green'}}>
                transformjs
              </Transform>
      
              <Transform rotateY={this.state.el2.rotateY} className="test" style={{'backgroundColor': 'red', 'left': '200px'}}>
                transformjs
              </Transform>
      
            </div>
          );
        }
      }
      
      render(
      	<Root />,
      	document.getElementById('root')
      );
      

      更加復(fù)雜的詳細(xì)的使用代碼可以看這里:https://github.com/AlloyTeam/AlloyTouch/blob/master/transformjs/react/example/src/index.jsx

      在線演示

      http://alloyteam.github.io/AlloyTouch/transformjs/react/example/

      性能對(duì)比

      因?yàn)閞eact版本會(huì)有diff過(guò)程,然后apply diff to dom的過(guò)程,state改變不會(huì)整個(gè)innerHTML全部替換,所以對(duì)瀏覽器渲染來(lái)說(shuō)還是很便宜,但是在js里diff的過(guò)程的耗時(shí)還是需要去profiles一把,如果耗時(shí)嚴(yán)重,不在webworker里跑還是會(huì)卡住UI線程導(dǎo)致卡頓,交互延緩等。所以要看一看CPU的耗時(shí)還是很有必要的。
      主要是那上面的演示和傳統(tǒng)的直接操作dom的方式對(duì)比。就是下面這種傳統(tǒng)的方式:

      var element1 = document.querySelector("#test1");
      Transform(element1);
      ...
      ...
      function animate() {
          element1.rotateZ++;
          ...
          requestAnimationFrame(animate);
      }
      
      animate();
      

      對(duì)兩種方式使用chrome profiles了一把。
      先看總耗時(shí)對(duì)比

      react:

      傳統(tǒng)方式:

      • react在8739秒內(nèi)CPU耗時(shí)花費(fèi)了近似1686ms
      • 傳統(tǒng)方式在9254ms秒內(nèi)CPU耗時(shí)花費(fèi)近似700ms

      在不進(jìn)行profiles就能想象到react是一定會(huì)更慢一些,因?yàn)閟tate的改變要走把react生命周期走一遍,但是可以看到react的耗時(shí)還是在可以接受的范圍。但是,我們還是希望找到拖慢的函數(shù)來(lái)。
      那么在使用transformjs react版本中,哪個(gè)函數(shù)拖了后腿?展開(kāi)profiles tree可以看到:

      就是它了。

      /**
      	   * Reconciles the properties by detecting differences in property values and
      	   * updating the DOM as necessary. This function is probably the single most
      	   * critical path for performance optimization.
      	   *
      	   * TODO: Benchmark whether checking for changed values in memory actually
      	   *       improves performance (especially statically positioned elements).
      	   * TODO: Benchmark the effects of putting this at the top since 99% of props
      	   *       do not change for a given reconciliation.
      	   * TODO: Benchmark areas that can be improved with caching.
      	   *
      	   * @private
      	   * @param {object} lastProps
      	   * @param {object} nextProps
      	   * @param {?DOMElement} node
      	   */
      	  _updateDOMProperties: function (lastProps, nextProps, transaction) {
      

      打開(kāi)對(duì)應(yīng)的代碼可以看到。注釋里已經(jīng)寫(xiě)了這是優(yōu)化重點(diǎn)。

      開(kāi)始使用吧

      官方網(wǎng)站:http://alloyteam.github.io/AlloyTouch/transformjs/

      Github地址:https://github.com/AlloyTeam/AlloyTouch/tree/master/transformjs
      任何問(wèn)題和意見(jiàn)歡迎new issue給我們。

      主站蜘蛛池模板: 中文激情一区二区三区四区| 中文字幕av无码免费一区| 国产精品亚洲а∨天堂2021| 四虎成人精品在永久免费| AV最新高清无码专区| 国产白丝jk捆绑束缚调教视频| 日本一区二区三区专线| 成av免费大片黄在线观看| AV人摸人人人澡人人超碰| 国产卡一卡二卡三免费入口| 国产亚洲av产精品亚洲| 美女胸18下看禁止免费视频| 99久久er这里只有精品18| 精品国产一区二区三区久| 国产99视频精品免费观看9| 国内精品自线在拍| 亚洲第一区二区快射影院| 苍井空毛片精品久久久| 国产色无码专区在线观看| 桐乡市| 亚洲精品自拍在线视频| 国产女人18毛片水真多1| 久久综合久中文字幕青草| 国产精品看高国产精品不卡| 无码国产偷倩在线播放老年人| 国产成人精品2021欧美日韩| 狠狠爱五月丁香亚洲综| 最近2019中文字幕大全第二页| 亚洲人成亚洲人成在线观看| 91老肥熟女九色老女人| 日韩人妻无码精品久久| 国产一区二区三区乱码| 1024你懂的国产精品| 亚洲精品日韩精品久久| 久爱无码精品免费视频在线观看| 国产日韩综合av在线| 亚洲国产亚洲国产路线久久 | 国产仑乱无码内谢| 永久免费无码av网站在线观看| 国产精品制服丝袜无码| 亚洲国产精品第一二三区|