国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

JavaScript實現(xiàn)液體流動效果和鼠標交互

這篇具有很好參考價值的文章主要介紹了JavaScript實現(xiàn)液體流動效果和鼠標交互。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

在現(xiàn)代Web應用中,動態(tài)效果和交互性已經(jīng)成為了不可或缺的元素。在這篇博客中,我們將使用JavaScript創(chuàng)建一個液體流動效果,并添加鼠標交互功能,讓用戶能夠與頁面進行互動。

創(chuàng)建畫布和粒子
首先,我們需要創(chuàng)建一個畫布元素,用于繪制我們的液體流動效果。在HTML中添加以下代碼:

<canvas id="canvas"></canvas>
接著,在JavaScript中獲取該元素,并設置其大小與瀏覽器窗口相同:

const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

接下來,我們需要創(chuàng)建液體粒子。在本例中,我們將使用一個Particle類來表示每個粒子。在類中,我們需要定義粒子的位置、大小、顏色和速度等屬性。在這里,我們將定義隨機速度和加速度,以使每個粒子的運動軌跡都不同。以下是Particle類的示例代碼:

class Particle {
  constructor(x, y, size, color) {
    this.x = x;
    this.y = y;
    this.size = size;
    this.color = color;
    this.vx = Math.random() * 2 - 1;
    this.vy = Math.random() * 2 - 1;
    this.ax = 0;
    this.ay = 0;
  }

  update() {
    this.vx += this.ax;
    this.vy += this.ay;
    this.x += this.vx;
    this.y += this.vy;
  }

  draw(ctx) {
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
    ctx.fill();
  }
}

接下來,我們需要創(chuàng)建一些粒子,并將它們存儲在數(shù)組中。在本例中,我們將創(chuàng)建200個粒子,并將它們分布在畫布的隨機位置上。以下是示例代碼:

const numParticles = 200;
const particles = [];

for (let i = 0; i < numParticles; i++) {
  const x = Math.random() * canvas.width;
  const y = Math.random() * canvas.height;
  const size = Math.random() * 10 + 5;
  const particleColor = `rgba(255, 255, 255, ${Math.random()})`;
  particles.push(new Particle(x, y, size, particleColor));
}

現(xiàn)在,我們已經(jīng)創(chuàng)建了畫布和粒子,并將它們存儲在數(shù)組中。接下來,我們需要繪制它們。

繪制液體
在本例中,我們將使用canvas的漸變功能來繪制液體。我們將定義兩種顏色,以在液體的頂部和底部之間創(chuàng)建漸變。以下是繪制液體的函數(shù):

function drawLiquid() {
  const liquidWidth = canvas.width;
  const liquidHeight = 200;
  const liquidBottom = canvas.height - liquidHeight;
  const liquidColor1 = '#0A2463';
  const liquidColor2 = '#C0C0C0';

  const liquidGradient = ctx.createLinearGradient(0, liquidBottom, 0, canvas.height);
  liquidGradient.addColorStop(0, liquidColor1);
  liquidGradient.addColorStop(1, liquidColor2);

  ctx.fillStyle = liquidGradient;
  ctx.fillRect(0, liquidBottom, liquidWidth, liquidHeight);
}

在這里,我們定義了液體的寬度、高度和顏色,并使用createLinearGradient()方法創(chuàng)建了一個垂直漸變。最后,我們使用fillRect()方法繪制了液體矩形。

繪制粒子
現(xiàn)在,我們已經(jīng)繪制了液體,接下來我們需要繪制粒子。我們將使用Particle類中定義的draw()方法來繪制每個粒子。以下是繪制粒子的函數(shù):

function drawParticles() {
particles.forEach((particle) => {
particle.update();
particle.draw(ctx);
});
}

在這里,我們遍歷粒子數(shù)組,并調(diào)用每個粒子的update()和draw()方法來更新和繪制它們的位置。

現(xiàn)在,我們已經(jīng)實現(xiàn)了液體和粒子的繪制。接下來,我們將添加鼠標交互功能。

添加鼠標交互

在本例中,我們將創(chuàng)建一個鼠標粒子,它將跟隨鼠標的移動而移動,并與其他粒子產(chǎn)生吸引力。以下是鼠標粒子的示例代碼:

class MouseParticle extends Particle {
  constructor(x, y, size, color) {
    super(x, y, size, color);
    this.mass = 10;
  }

  update(mouseX, mouseY) {
    this.x = mouseX;
    this.y = mouseY;
  }

  attractionForce() {
    particles.forEach((particle) => {
      if (particle !== this) {
        const dx = particle.x - this.x;
        const dy = particle.y - this.y;
        const distance = Math.sqrt(dx * dx + dy * dy);
        const force = (this.mass * particle.mass) / (distance * distance);
        const ax = force * dx / distance;
        const ay = force * dy / distance;
        particle.ax -= ax;
        particle.ay -= ay;
      }
    });
  }
}

const mouseParticle = new MouseParticle(0, 0, 20, 'white');
在這里,我們繼承了Particle類,并重寫了update()方法,以便鼠標粒子可以跟隨鼠標的移動而移動。我們還添加了一個attractionForce()方法,該方法將計算鼠標粒子和其他粒子之間的引力,并將其應用于其他粒子的加速度。

接下來,我們需要在動畫循環(huán)中調(diào)用鼠標移動事件監(jiān)聽器和繪制鼠標粒子的方法。以下是動畫循環(huán)的示例代碼:

```javascript
let mouseX = canvas.width / 2;
let mouseY = canvas.height / 2;

canvas.addEventListener('mousemove', (event) => {
  mouseX = event.clientX;
  mouseY = event.clientY;
});

function loop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawLiquid();
  drawParticles();
  mouseParticle.update(mouseX, mouseY);
  mouseParticle.attractionForce();
  mouseParticle.draw(ctx);
  requestAnimationFrame(loop);
}

loop();

在這里,我們添加了一個鼠標移動事件監(jiān)聽器,并在每次鼠標移動時更新鼠標的坐標。接下來,我們在動畫循環(huán)中調(diào)用了繪制鼠標粒子的方法,并在每次循環(huán)中更新鼠標粒子的位置和其他粒子的加速度。

現(xiàn)在,我們已經(jīng)實現(xiàn)了一個具有液體流動效果和鼠標交互的動態(tài)效果。您可以將下面的代碼保存到文件,在瀏覽器打開查看效果。

總結(jié)
在本文中,我們使用JavaScript實現(xiàn)了一個液體流動效果和鼠標交互的動態(tài)效果。我們創(chuàng)建了畫布和粒子,并使用canvas的漸變功能繪制了液體。我們還添加了鼠標粒子,并在每次循環(huán)中更新了粒子的位置和加速度。這個例子展示了JavaScript在動態(tài)效果和交互性方面的強大能力,希望對您有所幫助。

完整代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-640188.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Liquid Flow Page</title>
  <style>
    body {
      background-color: #000;
      overflow: hidden;
    }

    canvas {
      position: absolute;
      top: 0;
      left: 0;
      z-index: -1;
    }

    h1 {
      font-size: 80px;
      color: #fff;
      text-align: center;
      margin-top: 200px;
    }
  </style>
</head>
<body>
  <h1>Liquid Flow Page</h1>
  <canvas id="canvas"></canvas>
  <script>
    // 創(chuàng)建canvas并設置大小
    const canvas = document.getElementById('canvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // 獲取canvas上下文
    const ctx = canvas.getContext('2d');

    // 定義液體粒子的數(shù)量
    const numParticles = 200;

    // 定義液體粒子的顏色
    const particleColor = '#ff00ff';

    // 定義液體粒子的速度和加速度
    const particleSpeed = 2;
    const particleAcceleration = 0.05;

    // 定義液體的顏色
    const liquidColor1 = '#ff00ff';
    const liquidColor2 = '#00ffff';

    // 定義液體的高度和波動幅度
    const liquidHeight = 200;
    const liquidAmplitude = 50;

    // 定義鼠標粒子對象
    class MouseParticle {
      constructor(x, y, size, color) {
        this.x = x;
        this.y = y;
        this.size = size;
        this.color = color;
        this.vx = 0;
        this.vy = 0;
      }

      // 更新粒子的位置和速度
      update(x, y) {
        this.vx = (x - this.x) / 10;
        this.vy = (y - this.y) / 10;
        this.x += this.vx;
        this.y += this.vy;
      }

      // 繪制粒子
      draw() {
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.fill();
      }
    }

    // 創(chuàng)建鼠標粒子對象
    const mouseParticle = new MouseParticle(canvas.width / 2, canvas.height / 2, 20, particleColor);

    // 創(chuàng)建液體粒子對象
    class Particle {
      constructor(x, y, size, color) {
        this.x = x;
        this.y = y;
        this.size = size;
        this.color = color;
        this.vx = Math.random() * particleSpeed - particleSpeed / 2;
        this.vy = Math.random() * particleSpeed - particleSpeed / 2;
      }

      // 更新粒子的位置和速度
      update() {
        this.x += this.vx;
        this.y += this.vy;
        this.vy += particleAcceleration;
      }

      // 繪制粒子
      draw() {
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.fill();
      }

      // 計算粒子和鼠標粒子之間的距離
      distanceToMouse() {
        const dx = this.x - mouseParticle.x;
        const dy = this.y - mouseParticle.y;
        return Math.sqrt(dx * dx + dy * dy);
      }

      // 計算粒子和鼠標粒子之間的吸引力
      attractionForce() {
        const distance = this.distanceToMouse();
        const maxDistance = 100;
        const minDistance = 20;
        if (distance < maxDistance) {
          const force = (maxDistance - distance) / (maxDistance - minDistance);
          this.vx += (mouseParticle.vx * force) / 2;
          this.vy += (mouseParticle.vy * force) / 2;
        }
      }
    }

    // 創(chuàng)建液體粒子數(shù)組
    const particles = [];
    for (let i = 0; i < numParticles; i++) {
      const x = Math.random() * canvas.width;
      const y = Math.random() * canvas.height;
      const size= Math.random() * 10 + 5;
particles.push(new Particle(x, y, size, particleColor));
}

// 繪制液體
function drawLiquid() {
  const waveOffset = performance.now() / 1000;
  const liquidWidth = canvas.width;
  const liquidBottom = canvas.height - liquidHeight;

  const liquidGradient = ctx.createLinearGradient(0, liquidBottom, 0, canvas.height);
  liquidGradient.addColorStop(0, liquidColor1);
  liquidGradient.addColorStop(1, liquidColor2);

  ctx.fillStyle = liquidGradient;
  ctx.beginPath();
  ctx.moveTo(0, liquidBottom);
  for (let x = 0; x <= liquidWidth; x += 10) {
    const waveX = x / liquidWidth * Math.PI * 2;
    const waveY = Math.sin(waveX + waveOffset) * liquidAmplitude + liquidBottom;
    ctx.lineTo(x, waveY);
  }
  ctx.lineTo(canvas.width, canvas.height);
  ctx.lineTo(0, canvas.height);
  ctx.closePath();
  ctx.fill();
}

// 繪制粒子和鼠標粒子
function drawParticles() {
  particles.forEach((particle) => {
    particle.update();
    particle.attractionForce();
    particle.draw();
  });
  mouseParticle.update(mouseX, mouseY);
  mouseParticle.draw();
}

// 動畫循環(huán)
let mouseX = canvas.width / 2;
let mouseY = canvas.height / 2;
canvas.addEventListener('mousemove', (event) => {
  mouseX = event.clientX;
  mouseY = event.clientY;
});

function loop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawLiquid();
  drawParticles();
  requestAnimationFrame(loop);
}

loop();
</script> </body> </html>

到了這里,關于JavaScript實現(xiàn)液體流動效果和鼠標交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【HTML+CSS+JavaScript】實現(xiàn)鼠標點擊煙花效果

    【HTML+CSS+JavaScript】實現(xiàn)鼠標點擊煙花效果

    本文主要講解三種煙花效果(爆炸型、心型、圓形),文章末尾附有完整的代碼和圖片獲取鏈接。 效果圖(一) - 心型 : 效果圖(二) - 圓型 : 效果圖(三) - 爆炸型 : (1) HTML部分代碼 (2) CSS部分代碼 (3) 內(nèi)部的JavaScript部分代碼 (1) HTML部分代碼 (2) CSS部分代碼 (3) 內(nèi)部的JavaScript部分

    2024年02月01日
    瀏覽(38)
  • UnityShader實現(xiàn)液體傾倒效果

    UnityShader實現(xiàn)液體傾倒效果

    先看一下效果圖 ?實現(xiàn)思路:在燒杯傾倒液體時,燒杯內(nèi)的液體始終不會超過燒杯口的高度,所以我們計算得到這個高度值,然后截取掉超過該高度的像素 首先準備燒杯內(nèi)液體的模型,掛載一下shader對應的材質(zhì)球 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?shader部分

    2024年02月13日
    瀏覽(23)
  • Unity地面交互效果——2、動態(tài)法線貼圖實現(xiàn)軌跡效果

    Unity地面交互效果——2、動態(tài)法線貼圖實現(xiàn)軌跡效果

    回到目錄 Unity引擎動態(tài)法線貼圖制作球滾動軌跡 ??大家好,我是阿趙。 ??之前說了一個使用局部UV采樣來實現(xiàn)軌跡的方法。這一篇在之前的基礎上,使用法線貼圖進行凹凸軌跡的繪制。 ??先來回顧一下,上一篇最終我們已經(jīng)繪制了一個軌跡的貼圖 ??可以思考一下,

    2024年02月06日
    瀏覽(55)
  • 用js實現(xiàn)元素跟著鼠標轉(zhuǎn)動效果

    用js實現(xiàn)元素跟著鼠標轉(zhuǎn)動效果

    ?當鼠標移入body時,元素跟著鼠標旋轉(zhuǎn),所有的元素都看向鼠標的位置。 在一個shell中,總共有 36 個item。 這里對shell盒子采用css3的網(wǎng)格布局,對它的子元素進行分成6行6列的形式,寬和高都為50px, 然后利用雙偽元素來畫它的小眼睛。 然后每個小方塊都化成這個樣子。 原理:

    2024年02月09日
    瀏覽(24)
  • JS創(chuàng)建DIV,實現(xiàn)鼠標拖拽效果

    題目:用原生js動態(tài)創(chuàng)建一個div,大小隨意,掛在body上,實現(xiàn)鼠標拖拽效果 需要用到的鼠標事件: 1.鼠標按下(onmousedown), 2.鼠標移動(onmousemove) 3.鼠標抬起(onmouseup) 第一步:創(chuàng)建容器(每個頁面,最大的容器是body對象,所有dom對象創(chuàng)建后默認都會放到body) 第二步,

    2024年02月13日
    瀏覽(20)
  • vue中通過JavaScript實現(xiàn)web端鼠標橫向滑動&觸控板滑動效果-demo

    vue中通過JavaScript實現(xiàn)web端鼠標橫向滑動&觸控板滑動效果-demo

    JavaScript實現(xiàn)web端鼠標橫向滑動觸控板滑動效果? 支持鼠標拖動滑動觸控板滑動效果 web端實現(xiàn)滑動,就是對鼠標按下、鼠標松開、鼠標移動事件進行監(jiān)聽 在Vue中實現(xiàn)鼠標橫向滑動觸控板滑動效果可以通過以下步驟實現(xiàn): 首先在Vue中創(chuàng)建一個父組件,在該組件中引入子組件或者

    2024年02月15日
    瀏覽(21)
  • 【JavaScript】原生js實現(xiàn)省市區(qū)聯(lián)動效果

    【JavaScript】原生js實現(xiàn)省市區(qū)聯(lián)動效果

    ??博主:初映CY的前說(前端領域) ,??本文核心:用原生js實現(xiàn)省市區(qū)聯(lián)動 【前言】今日在復習省市縣三級聯(lián)動的時候,有點忘了原生的js應該怎么樣處理省市縣的聯(lián)動,特此寫下來再次復習下 1.獲取相對應的DOM對象 2.寫省市縣接口獲取到接口信息 3.寫下change事件,有變化時調(diào)

    2023年04月24日
    瀏覽(22)
  • three.js實現(xiàn)鼠標點擊控制物體移動(帶動畫效果,位置精確)

    three.js實現(xiàn)鼠標點擊控制物體移動(帶動畫效果,位置精確)

    通過查閱各種資料,最終確定解決方案,動畫效果使用gsap組件庫實現(xiàn),也可不用,代碼稍作修改即可。解決鼠標點擊坐標偏移問題,復制可直接運行。 完整代碼如下:

    2024年02月07日
    瀏覽(31)
  • 使用css實現(xiàn)邊框流動效果

    要實現(xiàn)一個邊框流動的效果,可以使用CSS動畫來實現(xiàn)。在HTML中,我們需要創(chuàng)建一個元素(例如div),并將其設置為具有一定寬度和高度的盒子。然后,我們可以使用CSS來定義該元素的邊框樣式、位置和動畫。 首先,我們需要在CSS中定義我們的元素。我們可以設置該元素的寬

    2024年02月09日
    瀏覽(23)
  • ArcGIS API for JavaScript 4.x 實現(xiàn)動態(tài)脈沖效果

    ArcGIS API for JavaScript 4.x 實現(xiàn)動態(tài)脈沖效果

    主要通過定時刷新,每一次的脈沖渲染圈不停的放大,并且透明度縮小,直到達到一定的大小再退回0。 這個文件拿去可以直接使用,下面是引入的方式: 然后可以調(diào)用提供的方法實現(xiàn)動態(tài)點的添加,動畫的暫停和啟動。

    2024年02月09日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包