processing实现鼠标交互变形的小树效果代码

代码语言:html

所属分类:动画

代码描述:processing实现鼠标交互变形的小树效果代码

代码标签: processing 鼠标 交互 变形 小树

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
* {
  margin: 0;
  box-sizing: border-box;
  overflow: hidden;
}

body {
  background: #e6e2ca;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
body canvas {
  box-shadow: 0.2em 0.2em 2em #0008;
  border: none;
  outline: none;
}
</style>

  
  
</head>

<body translate="no">
  <canvas id="canvas"></canvas>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/processing.1.4.8.js"></script>
  
      <script>
var sketchProc = function(processingInstance) {
  with (processingInstance) {
    size(600, 600); 
    frameRate(60);    
    smooth();
    
    /**

    Fractal Tree (Interactive)
    Gray Wolf, January 2022

    Just playing around with some recursion :)

    Move the mouse to alter the tree/branch angles

    When you start to learn fractals the tree is likely one of the first ones you'll encounter.

    @Subscribe
    Want to be notified when I create a new program? Sub here:
    https://www.khanacademy.org/cs/gray-wolf/4876588413698048

    **/

    textAlign(CENTER, CENTER);
    textSize(26);
    strokeCap(PROJECT);

    var over = false;

    //define the tree object/properties
    var tree = {
        x: 300,
        y: 490,
        len: 150,
        depth: 10,
        angle: -90,
        rot: 70,
        branches: [],
        colors: {
            from: color(91, 217, 108),
            to: color(125, 97, 37)
        }
    };

    //the recursive function (will be called from within itself)
    var addBranch = function(x, y, depth, angle, len) {

        //if the depth is zero then end the recursion
        if(depth === 0) {
            return;
        }

        //calculate the end points of the branch
        var x2 = x + cos(angle*Math.PI/180) * len;
        var y2 = y + sin(angle*Math.PI/180) * len;

        //add the new branch to the tree
        tree.branches.push({
            x1: x,
            y1: y,
            x2: x2,
            y2: y2,
            depth: depth,
            color: lerpColor(tree.colors.from, tree.colors.to, map(depth, 1, tree.depth, 0, 1))
        });

       .........完整代码请登录后点击上方下载按钮下载查看

网友评论0