作一個360度看車的效果玩玩(web)

前幾天在 Lexus 官網看到有這樣的一個效果:http://www.lexus.com.cn/models/es/360css

因而順手打開控制檯看了下他們是怎麼作的,發現使用的技術仍是比較簡單的,經過背景圖的切換來完成全景的效果。node

心血來潮本身作了一個優化一點的版本,先上 DEMO 和 源碼。(因爲圖片資源較大,加載時間較長,請耐心等待)git

 

接下來分享下個人製做流程。首先觀察下他們的圖片連接:github

http://img.lexus.do2014.cn/images/es/car/spoke10a/Sonic_Quartz/36.jpg!t1024x450.jpg服務器

標紅的位置爲圖片的序號,在拖拽時經過改變 url 來完成全景的效果。每款車的每一個顏色都有60張圖,序號爲0-59。優化

如今想把這60張圖下載下來,一張張扣天然太low,小小的運用下 node.js 幫咱們實現這個需求:依次請求圖片,並經過 fileSystem 將圖片寫到本地。ui

download.jsthis

var http = require("http"),
    fs = require("fs");

var imgPath = 'http://img.lexus.do2014.cn/images/es/car/spoke10a/Red_Mica_Crystal_Shine/';

fs.mkdir('./downloadImg', (err) => {
    if (err && err.code != 'EEXIST') return;
    downloadImg();
});

function downloadImg() {
    for (var i = 0; i < 60; i ++) {
        var url = imgPath + i + ".jpg!t1024x450.jpg";
        // console.log(url);
        ((i) => {
             http.get(url, (res) => {
                var out = fs.createWriteStream('./downloadImg/'+i+'.jpg', {
                    encoding: 'binary'
                });

                res.on('data', (chunk) => {
                    out.write(chunk);
                });

                res.on('end', () => {
                    console.log('Image_'+i+' download complete.');
                    out.end('');
                });
            });
        })(i);
    }
}
$ node download.js

這樣60張圖片就已經下載下來了,接下來想經過 CSS 精靈來實現這個圖片切換的效果,因此須要將這些圖片拼成一整張,我這裏使用 sketch 來完成。url

在同等的服務器條件下,一張確定要比多張效率高。不過即便是壓縮以後的圖,也有着上M的大小。若是有 CDN 進行加速的話,那是再好不過的了。spa

準備工做已經完成了,接下來進行代碼的編寫。

 

首先建立一個方法用來生成矩陣,矩陣中用來保存每輛車在雪碧圖中的座標。

{   
  // ...   I:
0,   J: 0,   rowNum: 10,   colNum: 6,   max: 60,   conWidth: 1024,   conHeight: 450,
//...   createMatrix() { this.matrix = []; var arr = []; for (var i = 0; i < this.max; i ++) { var position = '-' + this.I * this.conWidth + ' -' + this.J * this.conHeight; arr.push(position); this.I ++; if ((i+1) % this.colNum == 0) { this.matrix.push(arr); arr = []; this.I = 0; this.J ++; } } // console.log(this.matrix); this.I = 0; this.J = 0; } }

生成的座標矩陣以下

因爲這些座標最後是應用於 backgroundPostion 屬性上的,因此直接在前面添加了 「-」 號。

接下來建立一個改變圖片行列序號的方法,同時將座標設置到 backgroundPosition 上。

{
       //...
    this.$container: document.querySelector('#container'),
    this.I: 0,
    this.J: 0,
    this.rowNum: 10,
    this.colNum: 6,
       //...
    rotate(type) { //clockwise: 順時針, anti: 逆時針 
        if (type == 'clockwise') {
            this.I ++;

            if (this.I >= this.colNum) {
                this.I = 0;
                this.J ++;
                if (this.J >= this.rowNum) this.J = 0;
            }

        } else if (type == 'anti') {
            this.I --;

            if (this.I < 0) {
                this.I = this.colNum - 1;
                this.J --;
                if (this.J < 0) this.J = this.rowNum - 1;
            }
        }
        // console.log(this.I, this.J);
        this.changePosition();
    },

    changePosition() {
        // console.log(this.matrix[this.J][this.I]);
        this.$container.style.backgroundPosition = this.matrix[this.J][this.I];
    },
}

最後使用 hammer.js 來完成手勢的操做

var hammer = new Hammer(this.$container);

hammer.on('pan', function(e) {
  if ([向右拖動]) {
    this.rotate('anti');
  } else {
    this.rotate('clockwise');
  }
});

這樣,一個全景觀車的效果就完成了。

 

感謝你的瀏覽,但願有所幫助。

相關文章
相關標籤/搜索