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

基于JavaScript繪制動(dòng)態(tài)花束的示例代碼

 更新時(shí)間:2022年06月24日 08:47:58   作者:肥學(xué)  
p5.js 是一個(gè)JavaScript的函數(shù)庫(kù),它在制作之初就和Processing有同樣的目標(biāo)。本文將利用p5.js 制作出一束動(dòng)態(tài)花束,感興趣的可以嘗試一下

演示

技術(shù)棧

這次用到了一個(gè)名叫p5.js的框架:

p5.js 是一個(gè)JavaScript的函數(shù)庫(kù),它在制作之初就和Processing有同樣的目標(biāo)。就是讓藝術(shù)家,設(shè)計(jì)師,教育工作者和編程初學(xué)者能夠很容易,很輕松地學(xué)習(xí)和使用程序設(shè)計(jì)。并且它也能給現(xiàn)在的網(wǎng)頁(yè)帶來(lái)一些新的東西

它的結(jié)構(gòu):

 //初始化,只運(yùn)行一次
 function setup () {}

 //繪制圖形,每一幀都讀取
 function draw() {}

一個(gè)小案例

function setup() {
  createCanvas(400, 400);
}
function draw() {
  if(mouseIsPressed){
    stroke(220);
  }else{
    stroke(0);
  }
  line(300,300,mouseX,mouseY);
}

效果是這樣的

源碼

css

* { margin:0; padding:0; } 
html, body { width:100%; height:100%; overflow: hidden; background:black;} 
canvas { display:block; }
#controls {
  z-index: 2;
  margin: 20px;
  position: absolute;
  top: 0; left: 0;
  color: white;
}

js

let size = 150;

class Flower{
  constructor(s, a1, a2){
    let l = pow(random(), 2)*size;
    let l2 = random();
    this.l2 = l2;
    this.x = cos(a1)*(l + l2*size/2);
    this.y = sin(a1)*(l + l2*size/2);
    
    this.hue = random();
    
    this.goalX = this.x;
    this.goalY = this.y;
    
    this.dx = 10;
    this.dy = 10;
    
    this.s = s*(l2*.5 + .5);
    this.a = a1;
    this.squash = 1 - (l/size)*.8;
    this.weights = [];
    for (let i = 0; i < 5; i++){
      this.weights[i] = random()*.5 + .5;
    }
  }
  update(){
    this.x += this.dx;
    this.y += this.dy;
    
    this.dx *= .95;
    this.dy *= .95;
    
    let mx = (width/2 + this.x) - mouseX;
    let my = (height/2 + this.y) - mouseY;
   
    let d = dist(width/2 + this.x, height/2 + this.y, mouseX, mouseY);
    let a = atan2(my, mx);
    
    if (d > 1){
      this.dx += cos(a)*size/d;
      this.dy += sin(a)*size/d;
    }
    
    this.x = (this.goalX + this.x)/2;
    this.y = (this.goalY + this.y)/2;
  }
  renderStem(){
    pushPop(() => {
      noFill();
      strokeWeight(this.l2*2 + 1);
      stroke(.35, 1, this.l2*.5 + .5);
      translate(this.x, size);
      let a = PI;
      if (this.x <= 0) a += PI/2;
      arc(0, 0, this.x*2, (this.y)*2 - size*2, a, a+PI/2);
    })
  }
  render(){
    pushPop(() => {
      noStroke();
      fill(this.l2*.2 + .8);
      translate(this.x, this.y);
      rotate(this.a);
      scale(this.s*this.squash, this.s);
      
      for (let j = 0; j < 2; j++)
      for (let i = 0; i < 5; i++){
        let a = i*TAU/5;
        let s = this.weights[i];
        let b = (this.l2*.2 + .8)*(s*.1 + .9)
        fill(this.hue, .1*this.l2, b);
        if (j == 0){
          s += .05;
          fill(0);
        }
        ellipse(cos(a)*.7*s, sin(a)*.7*s, 1);
      }
      fill(0);
      ellipse(0, 0, .7*(this.weights[0] + .05));
      fill(.15, 1, 1*(this.l2*.2 + .8));
      ellipse(0, 0, .7*this.weights[0]);
    })
  }
}

function setup (){
  pixelDensity(1);
  createCanvas();
  colorMode(HSB, 1, 1, 1);
  windowResized();
}

function init(){
  flowers = [];
  for (let i = 0; i < 50; i++){
    flowers.push(
      new Flower(random(20) + 20, random(PI*.8) + PI + PI*.1)
    );
  }
  flowers = flowers.sort((a, b) => a.s - b.s);
}

function draw(){
  background(0);
  translate(width/2, height/2);
  flowers.map(f => f.update());
  flowers.map(f => f.renderStem());
  flowers.map(f => f.render());
}

function windowResized(){
  resizeCanvas(windowWidth, windowHeight); 
  init();
}

let pushPop = f => {push();f();pop();}

以上就是基于JavaScript繪制動(dòng)態(tài)花束的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaScript繪制花束的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • JavaScript實(shí)現(xiàn)自定義拖拽排序列表

    JavaScript實(shí)現(xiàn)自定義拖拽排序列表

    在Web開(kāi)發(fā)中,拖拽排序是一個(gè)常見(jiàn)的需求,它允許用戶(hù)通過(guò)拖拽的方式重新排列列表項(xiàng)的順序,本文將介紹如何使用原生JavaScript實(shí)現(xiàn)這一功能,需要的可以了解下
    2024-01-01
  • localStorage設(shè)置有效期和過(guò)期時(shí)間的簡(jiǎn)單方法

    localStorage設(shè)置有效期和過(guò)期時(shí)間的簡(jiǎn)單方法

    眾所周知前端三大緩存,cookie,sessionStorage,localStorage,下面這篇文章主要給大家介紹了關(guān)于localStorage設(shè)置有效期和過(guò)期時(shí)間的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • JavaScript手寫(xiě)數(shù)組的常用函數(shù)總結(jié)

    JavaScript手寫(xiě)數(shù)組的常用函數(shù)總結(jié)

    這篇文章主要給大家介紹了關(guān)于JavaScript手寫(xiě)數(shù)組常用函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • uni-app小程序分享功能實(shí)現(xiàn)方法舉例

    uni-app小程序分享功能實(shí)現(xiàn)方法舉例

    這篇文章主要給大家介紹了關(guān)于uni-app小程序分享功能實(shí)現(xiàn)方法的相關(guān)資料,uni-app中有分享的API接口,但是需要現(xiàn)在QQ或者微信等開(kāi)發(fā)者平臺(tái)上注冊(cè)賬號(hào),驗(yàn)證公司信息,而且只能分享圖片或者文本等內(nèi)容,需要的朋友可以參考下
    2023-07-07
  • js自定義回調(diào)函數(shù)

    js自定義回調(diào)函數(shù)

    這篇文章主要介紹了javascript自定義回調(diào)函數(shù),感興趣的小伙伴們可以參考一下
    2015-12-12
  • javascript動(dòng)態(tài)加載實(shí)現(xiàn)方法一

    javascript動(dòng)態(tài)加載實(shí)現(xiàn)方法一

    這兩天,沒(méi)什么太多的事情,好吧,我承認(rèn),是我這兩天不想做公司的項(xiàng)目,因?yàn)槲彝话l(fā)奇想,其實(shí)也不算突發(fā)奇想,算是對(duì)以前的想法的實(shí)現(xiàn),就是把JS當(dāng)做Java來(lái)寫(xiě)
    2012-08-08
  • JavaScript實(shí)現(xiàn)數(shù)字?jǐn)?shù)組正序排列的方法

    JavaScript實(shí)現(xiàn)數(shù)字?jǐn)?shù)組正序排列的方法

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)數(shù)字?jǐn)?shù)組正序排列的方法,涉及javascript中sort方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Ajax同步與異步傳輸?shù)氖纠a

    Ajax同步與異步傳輸?shù)氖纠a

    這篇文章主要是對(duì)Ajax同步與異步傳輸?shù)氖纠a進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-11-11
  • jquery庫(kù)文件略龐大用純js替換jquery的方法

    jquery庫(kù)文件略龐大用純js替換jquery的方法

    jquery庫(kù)文件略龐大,因此在某些情況下就需要用純js替換jquery,需要的朋友可以參考下
    2014-08-08
  • 基于jQuery的模仿新浪微博時(shí)間的組件

    基于jQuery的模仿新浪微博時(shí)間的組件

    廢話(huà)不多說(shuō),實(shí)現(xiàn)原理主要是處理table,生成tr td,其中最重要的是如何找出每月第一天是星期幾,然后就能對(duì)應(yīng)出這個(gè)月的余下天數(shù).
    2011-10-10

最新評(píng)論

上犹县| 汨罗市| 景泰县| 萨嘎县| 盱眙县| 西青区| 苏尼特右旗| 永丰县| 新巴尔虎右旗| 鹿邑县| 东台市| 西林县| 双城市| 石阡县| 通城县| 黎城县| 济宁市| 永修县| 新绛县| 乐亭县| 仙游县| 永福县| 古丈县| 武强县| 武夷山市| 巨野县| 微山县| 秭归县| 舞钢市| 沧州市| 罗甸县| 南郑县| 亳州市| 东安县| 禄丰县| 那曲县| 柳州市| 新营市| 紫金县| 舒城县| 临沂市|