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

JavaScript利用多彩線條擺出心形效果的示例代碼

 更新時間:2022年07月06日 11:12:40   作者:肥學  
這篇文章主要為大家詳細介紹了如何利用JavaScript語言實現(xiàn)多彩線條擺出心形效果,文中的實現(xiàn)步驟講解詳細,快跟隨小編一起動手嘗試一下吧

演示

源碼展示

創(chuàng)建畫布

 <canvas width="300" height="300" style="width:100%;height:100vh;" id="c"></canvas>

基礎樣式設置

overflow 語法: overflow:{1,2}= visible | hidden | scroll | auto

默認值:visible

取值:

visible:不剪切內容。hidden:將超出對象尺寸的內容進行裁剪,將不出現(xiàn)滾動條。scroll:將超出對象尺寸的內容進行裁剪,并以滾動條的方式顯示超出的內容。auto:在需要時剪切內容并添加滾動條,此為body對象和textarea的默認值。

padding:[ | ]{1,4}

默認值:看每個獨立屬性

相關屬性:[ padding-top ] || [ padding-right ] || [ padding-bottom ] || [padding-left ]

取值: :用長度值來定義內補白。不允許負值:用百分比來定義內補白。不允許負值

說明: 檢索或設置對象四邊的內部邊距。

如果提供全部四個參數(shù)值,將按上、右、下、左的順序作用于四邊。 如果只提供一個,將用于全部的四邊。

如果提供兩個,第一個用于上、下,第二個用于左、右。 如果提供三個,第一個用于上,第二個用于左、右,第三個用于下。

內聯(lián)對象可以使用該屬性設置左、右兩邊的內補??;若要設置上、下兩邊的內補丁,必須先使該對象表現(xiàn)為塊級或內聯(lián)塊級。

對應的腳本特性為padding。

 html,body{
            border: 0;
            padding: 0;
            margin: 0;
            overflow: hidden;
            background: #000;
}

.info{
  z-index:999;
  position : absolute;
  left:0;
  top:0;
  padding:10px;
  color:#fff;
  background: rgba(0,0,0,0.5);
  text-transform:capitalize;
}

用js來設計動畫效果

定義變量

var canvas = document.getElementById('c');
var ctx = canvas.getContext("2d");
var height = void 0,width = void 0,innerpoints = [],outerpoints = [],particles = [];

var noofpoints = 200,trashold = 10;
var x = void 0,y = void 0,p = void 0,n = void 0,point = void 0,dx = void 0,dy = void 0,color = void 0;
deltaangle = Math.PI * 2 / noofpoints,
r = Math.min(height, width) * 0.5;

var distance = function distance(x1, y1, x2, y2) {
  return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
};
var mapVal = function mapVal(num, in_min, in_max, out_min, out_max) {
  return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};

設置畫布resize

var resize = function resize() {
  height = ctx.canvas.clientHeight;
  width = ctx.canvas.clientWidth;

  if (ctx.canvas.clientWidth !== canvas.width ||
  ctx.canvas.clientHeight !== canvas.height)
  {
    console.log("resized");
    canvas.width = width;
    canvas.height = height;
    ctx.translate(canvas.width / 2, canvas.height / 2);
    ctx.rotate(-Math.PI);
    innerpoints = [];
    r = 10;
    for (var i = deltaangle; i <= Math.PI * 2; i += deltaangle) {
      x = r * 16 * Math.pow(Math.sin(i), 3);
      y = r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
      innerpoints.push({
        x: x,
        y: y });


      x = 10 * r * 16 * Math.pow(Math.sin(i), 3);
      y = 10 * r * (13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
      outerpoints.push({
        x: x,
        y: y });


      var step = random(0.001, 0.003, true);
      particles.push({
        step: step,
        x: x,
        y: y });

    }
  }
};

對線條設計

var draw = function draw() {
  ctx.fillStyle = "rgba(0,0,0,0.03)";
  ctx.fillRect(-width, -height, width * 2, height * 2);
  ctx.beginPath();

  for (var i = 0; i < innerpoints.length; i++) {
    s = outerpoints[i];
    d = innerpoints[i];
    point = particles[i];

    if (distance(point.x, point.y, d.x, d.y) > 10) {
      dx = d.x - s.x;
      dy = d.y - s.y;

      point.x += dx * point.step;
      point.y += dy * point.step;
      color = distance(0, 0, point.x, point.y);
      ctx.beginPath();
      ctx.fillStyle = "hsl(" + color % 360 + ",100%,50%)";
      ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
      ctx.closePath();
      ctx.fill();
    } else {
      point.x = d.x;
      point.y = d.y;
      ctx.beginPath();
      ctx.arc(point.x, point.y, 2, 0, Math.PI * 2, false);
      ctx.closePath();
      ctx.fill();
      particles[i].x = s.x;
      particles[i].y = s.y;
      particles[i].step = random(0.001, 0.003, true);
    }
  }

};

到此這篇關于JavaScript利用多彩線條擺出心形效果的示例代碼的文章就介紹到這了,更多相關JavaScript線條擺出心形內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

蓬莱市| 成安县| 丹阳市| 伊吾县| 长葛市| 卢龙县| 当涂县| 施甸县| 临洮县| 长春市| 突泉县| 白城市| 龙里县| 贵港市| 阿瓦提县| 武宣县| 长白| 信丰县| 商河县| 沛县| 新化县| 巢湖市| 秦皇岛市| 天长市| 平阳县| 宣城市| 青铜峡市| 崇明县| 尚志市| 肇东市| 阿勒泰市| 天等县| 东光县| 卢龙县| 清丰县| 屯门区| 科尔| 遵义县| 万载县| 安多县| 集安市|