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

JS+Canvas實(shí)現(xiàn)滿屏愛心和文字動(dòng)畫的制作

 更新時(shí)間:2022年11月19日 11:14:51   作者:zkj  
Canvas?適合繪制大數(shù)據(jù)量圖形元素的圖表(如熱力圖、地理坐標(biāo)系或平行坐標(biāo)系上的大規(guī)模線圖或散點(diǎn)圖等),也適合實(shí)現(xiàn)某些視覺特效。本文就來利用Canvas實(shí)現(xiàn)滿屏愛心和文字動(dòng)畫的制作,感興趣的可以了解一下

介紹

<canvas> 最早由 Apple 引入 WebKit,用于 Mac OS X 的 Dashboard,隨后被各個(gè)瀏覽器實(shí)現(xiàn)。如今,所有主流的瀏覽器都支持它。Canvas API 提供了一個(gè)通過 JavaScript 和 HTML 的 <canvas> 元素來繪制圖形的方式。它可以用于動(dòng)畫、游戲畫面、數(shù)據(jù)可視化、圖片編輯以及實(shí)時(shí)視頻處理等方面。Canvas 適合繪制大數(shù)據(jù)量圖形元素的圖表(如熱力圖、地理坐標(biāo)系或平行坐標(biāo)系上的大規(guī)模線圖或散點(diǎn)圖等),也適合實(shí)現(xiàn)某些視覺特效。它還能能夠以 png、jpg 或 webp 格式保存圖像。Canvas 提供了強(qiáng)大的 Web 繪圖能力,所以我們要學(xué)會使用它。

效果如下:

步驟

準(zhǔn)備一個(gè) canvas 元素。

<canvas id="drawHeart"></canvas>

獲取 canvas 對象和上下文,初始化變量:窗口寬高、愛心和文字總數(shù)量、包含愛心和文字的數(shù)組,定義愛心圖片,圖片 src 可以是 base64 字符串類型或者本地圖片文件和網(wǎng)絡(luò)圖片鏈接。

const canvas = document.getElementById('drawHeart');
const ctx = canvas.getContext('2d');
let wW = window.innerWidth;
let wH = window.innerHeight;
const num = 100;
const hearts = [];
const heartImage = new Image();
heartImage.src = 'data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><path id="heart" d="M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z" fill="red"/></svg>';

定義一個(gè) Heart 類,構(gòu)造函數(shù)參數(shù) type 標(biāo)識用來判斷實(shí)例為愛心圖片還是文字類型,定義重繪方法 draw 和更新方法 update。

class Heart {
  constructor(type) {
    this.type = type;
    // 初始化生成范圍
    this.x = Math.random() * wW;
    this.y = Math.random() * wH;
    this.opacity = Math.random() * .5 + .5;
    // 偏移量
    this.vel = {
      x: (Math.random() - .5) * 5,
      y: (Math.random() - .5) * 5
    }
    this.initialW = wW * .5;
    this.initialH = wH * .5;
    // 縮放比例
    this.targetScale = Math.random() * .15 + .02; // 最小0.02
    this.scale = Math.random() * this.targetScale;
    // 文字位置
    this.fx = Math.random() * wW;
    this.fy = Math.random() * wH;
    this.fs = Math.random() * 10;
    this.text = getText();
    this.fvel = {
      x: (Math.random() - .5) * 5,
      y: (Math.random() - .5) * 5,
      f: (Math.random() - .5) * 2
    }
  }
  draw() {
    ctx.save();
    ctx.globalAlpha = this.opacity;
    ctx.drawImage(heartImage, this.x, this.y, this.width, this.height);
    // ctx.scale(this.scale + 1, this.scale + 1);
    if (!this.type) {
      // 設(shè)置文字屬性
      ctx.fillStyle = getColor();
      ctx.font = 'italic ' + this.fs + 'px sans-serif';
      // 填充字符串
      ctx.fillText(this.text, this.fx, this.fy);
    }
    ctx.restore();
  }
  update() {
    this.x += this.vel.x;
    this.y += this.vel.y;
    if (this.x - this.width > wW || this.x + this.width < 0) {
      // 重新初始化位置
      this.scale = 0;
      this.x = Math.random() * wW;
      this.y = Math.random() * wH;
    }
    if (this.y - this.height > wH || this.y + this.height < 0) {
      // 重新初始化位置
      this.scale = 0;
      this.x = Math.random() * wW;
      this.y = Math.random() * wH;
    }
    // 放大
    this.scale += (this.targetScale - this.scale) * .1;
    this.height = this.scale * this.initialH;
    this.width = this.height * 1.4;
    // -----文字-----
    this.fx += this.fvel.x;
    this.fy += this.fvel.y;
    this.fs += this.fvel.f;
    if (this.fs > 50) {
      this.fs = 2;
    }
    if (this.fx - this.fs > wW || this.fx + this.fs < 0) {
      // 重新初始化位置
      this.fx = Math.random() * wW;
      this.fy = Math.random() * wH;
    }
    if (this.fy - this.fs > wH || this.fy + this.fs < 0) {
      // 重新初始化位置
      this.fx = Math.random() * wW;
      this.fy = Math.random() * wH;
    }
  }
}

定義一個(gè)獲取隨機(jī)文字的方法,用來動(dòng)態(tài)渲染屏幕文字。

function getText() {
  const val = Math.random() * 10;
  if (val > 1 && val <= 3) {
    return 'always';
  } else if (val > 3 && val <= 5) {
    return 'zzy';
  } else if (val > 5 && val <= 8) {
    return 'taylor swift';
  } else {
    return 'I Love You';
  }
}

定義一個(gè)獲取隨機(jī)顏色的方法,用來動(dòng)態(tài)渲染屏幕文字顏色。

function getColor() {
  const val = Math.random() * 10;
  if (val > 0 && val <= 1) {
    return '#00f';
  } else if (val > 1 && val <= 2) {
    return '#f00';
  } else if (val > 2 && val <= 3) {
    return '#0f0';
  } else if (val > 3 && val <= 4) {
    return '#368';
  } else if (val > 4 && val <= 5) {
    return '#666';
  } else if (val > 5 && val <= 6) {
    return '#333';
  } else if (val > 6 && val <= 7) {
    return '#f50';
  } else if (val > 7 && val <= 8) {
    return '#e96d5b';
  } else if (val > 8 && val <= 9) {
    return '#5be9e9';
  } else {
    return '#d41d50';
  }
}

定義渲染和初始化方法,添加 resize 事件,在窗口調(diào)整大小時(shí)自動(dòng)適應(yīng)。

function init() {
  canvas.width = wW;
  canvas.height = wH;
  for (let i = 0; i < num; i++) {
    hearts.push(new Heart(i % 5));
  }
  render();
}

function render() {
  ctx.clearRect(0, 0, wW, wH);
  for (let i = 0; i < hearts.length; i++) {
    hearts[i].draw();
    hearts[i].update();
  }
  setTimeout(render, 60);
}

init();
window.addEventListener('resize', function() {
  canvas.width = wW = window.innerWidth;
  canvas.height = wH = window.innerHeight;
});

效果圖

到此這篇關(guān)于JS+Canvas實(shí)現(xiàn)滿屏愛心和文字動(dòng)畫的制作的文章就介紹到這了,更多相關(guān)JS Canvas愛心文字動(dòng)畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

饶平县| 南部县| 深圳市| 依安县| 青铜峡市| 宁河县| 红原县| 郴州市| 武山县| 峨边| 伊宁县| 汤阴县| 榆社县| 阆中市| 建瓯市| 西畴县| 肥东县| 屏山县| 西青区| 营口市| 柞水县| 玛纳斯县| 灵川县| SHOW| 根河市| 门源| 栾城县| 岐山县| 昭觉县| 文成县| 灵璧县| 神农架林区| 辉县市| 河间市| 历史| 山丹县| 进贤县| 桂阳县| 和林格尔县| 抚顺县| 垦利县|