最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用js實現(xiàn)動態(tài)背景

 更新時間:2021年11月25日 13:25:06   作者:jk小兔  
這篇文章主要為大家詳細介紹了使用js實現(xiàn)動態(tài)背景,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js實現(xiàn)動態(tài)背景的具體代碼,供大家參考,具體內(nèi)容如下

1.將下面的代碼復(fù)制并存為js文件

window.onload = function () {
    //定義body的margin由默認值8px->0px
    document.body.style.margin = "0";
    document.body.style.background = "#30333F";
    //創(chuàng)建canvas畫布
    document.body.appendChild(document.createElement('canvas'));
    var canvas = document.querySelector('canvas'),
        ctx = canvas.getContext('2d') //ctx返回一個在canvas上畫圖的api/dom
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    canvas.style.position = 'fixed';
    ctx.lineWidth = .3;
    ctx.strokeStyle = (new Color(150)).style;
    //定義鼠標(biāo)覆蓋范圍
    var mousePosition = {
        x: 30 * canvas.width / 100,
        y: 30 * canvas.height / 100
    };
    var dots = {
        nb: 1000,//Dot的總數(shù)
        distance: 50,
        d_radius: 100,
        array: []
    };
    //創(chuàng)建顏色類,Color類返回字符串型rgba(*,*,*,.8)
    function mixComponents(comp1, weight1, comp2, weight2) {
        return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
    }
    function averageColorStyles(dot1, dot2) {
        var color1 = dot1.color,
            color2 = dot2.color;

        var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
            g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
            b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
        return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
    }
    function colorValue(min) {
        return Math.floor(Math.random() * 255 + min);
    }
    function createColorStyle(r, g, b) {
        return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
    }
    function Color(min) {
        min = min || 0;
        this.r = colorValue(min);
        this.g = colorValue(min);
        this.b = colorValue(min);
        this.style = createColorStyle(this.r, this.g, this.b);
    }
    //創(chuàng)建Dot類以及一系列方法
    function Dot() {
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;

        this.vx = -.5 + Math.random();
        this.vy = -.5 + Math.random();

        this.radius = Math.random() * 2;

        this.color = new Color();
    }

    Dot.prototype = {
        draw: function () {
            ctx.beginPath();
            ctx.fillStyle = this.color.style;
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
            ctx.fill();
        }
    };
    function moveDots() {//Dot對象的移動
        for (i = 0; i < dots.nb; i++) {

            var dot = dots.array[i];

            if (dot.y < 0 || dot.y > canvas.height) {
                dot.vx = dot.vx;
                dot.vy = - dot.vy;
            }
            else if (dot.x < 0 || dot.x > canvas.width) {
                dot.vx = - dot.vx;
                dot.vy = dot.vy;
            }
            dot.x += dot.vx;
            dot.y += dot.vy;
        }
    }
    function connectDots() {//DOt對象的連接
        for (i = 0; i < dots.nb; i++) {
            for (j = i; j < dots.nb; j++) {
                i_dot = dots.array[i];
                j_dot = dots.array[j];

                if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance) {
                    if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius) {
                        ctx.beginPath();
                        ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                        ctx.moveTo(i_dot.x, i_dot.y);
                        ctx.lineTo(j_dot.x, j_dot.y);
                        ctx.stroke();//繪制定義的路線
                        ctx.closePath();//創(chuàng)建從當(dāng)前點回到起始點的路徑
                    }
                }
            }
        }
    }
    function createDots() {//創(chuàng)建nb個Dot對象
        for (i = 0; i < dots.nb; i++) {
            dots.array.push(new Dot());
        }
    }
    function drawDots() {//引用Dot原型鏈,使用draw方法,在canvas上畫出Dot對象
        for (i = 0; i < dots.nb; i++) {
            var dot = dots.array[i];
            dot.draw();
        }
    }
    function animateDots() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);//清除畫布,否則線條會連在一起
        moveDots();
        connectDots();
        drawDots();
        requestAnimationFrame(animateDots);
    }
    createDots();//使用創(chuàng)建Dot類函數(shù)
    requestAnimationFrame(animateDots);//使用canvas獨有的60Hz刷新屏幕畫布的方法

    document.querySelector('canvas').addEventListener('mousemove', function (e) {
        mousePosition.x = e.pageX;
        mousePosition.y = e.pageY;
    })

    document.querySelector('canvas').addEventListener('mouseleave', function (e) {//鼠標(biāo)離開時,連接自動返回到畫布中心
        mousePosition.x = canvas.width / 2;
        mousePosition.y = canvas.height / 2;
    })

}

2.然后在需要使用動態(tài)背景的html頁面引入js文件就可以了

效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入理解Javascript里的依賴注入

    深入理解Javascript里的依賴注入

    我喜歡引用這句話,“程序是對復(fù)雜性的管理”。計算機世界是一個巨大的抽象建筑群。我們簡單的包裝一些東西然后發(fā)布新工具,周而復(fù)始?,F(xiàn)在思考下,你所使用的語言包括的一些內(nèi)建的抽象函數(shù)或是低級操作符。這在JavaScript里是一樣的
    2014-03-03
  • 淺談es6中的元編程

    淺談es6中的元編程

    這篇文章主要介紹了淺談es6中的元編程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • js實現(xiàn)3d懸浮效果

    js實現(xiàn)3d懸浮效果

    本文主要分享了js實現(xiàn)3d懸浮效果的示例代碼,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • JavaScript如何動態(tài)創(chuàng)建table表格

    JavaScript如何動態(tài)創(chuàng)建table表格

    這篇文章主要介紹了JavaScript如何動態(tài)創(chuàng)建table表格,一些時候需要動態(tài)的創(chuàng)建和刪除表格,接下來的文章中將為大家介紹下javascript是如何做到的,感興趣的朋友不要錯過
    2015-11-11
  • 淺析hasOwnProperty方法的應(yīng)用

    淺析hasOwnProperty方法的應(yīng)用

    這篇文章主要是對hasOwnProperty方法的應(yīng)用進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • javascript實現(xiàn)des解密加密全過程

    javascript實現(xiàn)des解密加密全過程

    這篇文章主要介紹了javascript 實現(xiàn)des解密加密的過程,需要的朋友可以參考下
    2014-04-04
  • JS控制表單提交的方法

    JS控制表單提交的方法

    這篇文章主要介紹了JS控制表單提交的方法,可實現(xiàn)基于javascript提交表單信息的功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 利用js+canvas實現(xiàn)掃雷游戲

    利用js+canvas實現(xiàn)掃雷游戲

    這篇文章主要為大家詳細介紹了利用js+canvas實現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 微信小程序自定義帶價格顯示日歷效果

    微信小程序自定義帶價格顯示日歷效果

    這篇文章主要為大家詳細介紹了微信小程序自定義帶價格顯示日歷效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • amd、cmd、esmodule、commonjs區(qū)別詳解

    amd、cmd、esmodule、commonjs區(qū)別詳解

    本文主要介紹了amd、cmd、esmodule、commonjs區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評論

调兵山市| 金川县| 安塞县| 饶平县| 灵武市| 华阴市| 肥东县| 漾濞| 扎兰屯市| 肇庆市| 扎赉特旗| 临邑县| 迁安市| 灌南县| 武穴市| 达尔| 敖汉旗| 定襄县| 咸丰县| 南昌县| 酒泉市| 资溪县| 临澧县| 北流市| 濮阳市| 昌邑市| 柘荣县| 饶平县| 乌海市| 陆川县| 静宁县| 武陟县| 武山县| 滦平县| 丹凤县| 株洲县| 富川| 多伦县| 九江市| 新宁县| 河池市|