processing实现canvas飞龙在天动画效果代码

代码语言:html

所属分类:动画

代码描述:processing实现canvas飞龙在天动画效果代码,一直西方的龙在天空中飞翔。

代码标签: processing canvas飞龙 天空 飞翔 动画

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


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

<head>

  <meta charset="UTF-8">

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

body {
  background: #fff;
  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;
  border-radius: 1em;
}
</style>



</head>

<body  >
  <canvas id="canvas"></canvas>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/processing.min.js"></script>
  
      <script id="rendered-js" >
var sketchProc = function(processingInstance) {
     with (processingInstance) {
        size(600, 600); 
       
        frameRate(60);
       
        var scene;

        var Dragon = function() {
            this.x = 200;
            this.y = 200;
            this.w = 150;
            this.h = 100;
            this.vx = 0;
            this.vy = 0;
            this.segments = [];
            this.timer = 0;
            this.speed = 4;
            this.lift = 3;
            this.showLegs = true;
            this.wing = {
                x1: 0,
                y1: 0,
                x2: 0,
                y2: 0,
                x3: 0,
                y3: 0,
                x4: 0,
                y4: 0,
                spines: {
                    a: {
                        x1: 0,
                        y1: 0,
                        x2: 0,
                        y2: 0
                    },
                    b: {
                        x1: 0,
                        y1: 0,
                        x2: 0,
                        y2: 0
                    },
                    c: {
                        x1: 0,
                        y1: 0,
                        x2: 0,
                        y2: 0
                    },
                    d: {
                        x1: 0,
                        y1: 0,
                        x2: 0,
                        y2: 0
                    }
                }
            };
            this.nodes = [
                10,
                14,
                12,
                18,
                23,
                28,
                37,
                42,
                50,
                50,
                46,
                42,
                35,
                28,
                22,
                18,
                15,
                14,
                11,
                9,
                8,
                7,
                5,
                3,
                2,
                1,
                1,
                1
            ];
            this.m = 0;
            this.m2 = 0;
            this.diff = 0;
            this.colorThemes = {
                red: {
                    spikes: color(71, 14, 7),
                    black: color(56, 55, 46),
                    body: color(128, 14, 14),
                    wingArm: color(107, 8, 8),
                    wingArmSpike: color(76, 77, 76),
                    wingUpper: color(122, 32, 16),
                    wingLower: color(148, 46, 33),
                    wingSpines: color(71, 70, 71, 150),
                    legsBehind: color(110, 13, 13)
                },
                green: {
                    spikes: color(18, 46, 4),
                    black: color(120, 110, 20),
                    body: color(33, 82, 16),
                    wingArm: color(39, 77, 10),
                    wingArmSpike: color(76, 77, 76),
                    wingUpper: color(44, 82, 15),
                    wingLower: color(49, 110, 25),
                    wingSpines: color(71, 70, 71, 150),
                    legsBehind: color(30, 77, 14)
                },
                black: {
                    spikes: color(55, 56, 55),
                    black: color(99, 96, 67),
                    body: color(31, 29, 31),
                    wingArm: color(60, 71, 51),
                    wingArmSpike: color(76, 77, 76),
                    wingUpper: color(22, 23, 21),
                    wingLower: color(38, 46, 35),
                    wingSpines: color(64, 64, 64, 150),
                    legsBehind: color(3, 3, 3)
                }
            };
            this.colors = this.colorThemes.red;

            this.setup = function() {
                for(var i = 0; i <= 27; i++) {
                    this.segments.push({
                       x: this.x + this.w * i / 27,
                       y: this.y
                    });
                }
            };
            this.setup();
            this.update = function() {
                this.diff = this.segments[0].y - this.segments[1].y;

                this.timer+=0.018;
                this.m = sin(this.timer * this.speed) * this.lift;
                this.m2 = cos(this.timer * this.speed) * this.lift;

                //update each of the segments
                for(var i = 0; i < this.segments.length; i++) {
                    var segment = this.segments[i];

                    if(i === 0) {
                        segment.y += this.m;
                        segment.x = this.x;
                    }
                    else {
                        segment.y = lerp(segment.y, this.segments[i-1].y, 0.20);
                        segment.x = lerp(segment.x, this.segments[i-1].x, 0.20);
                    }
                }

                //move the wing
                this.wing.x1 = this.segments[7].x;
                this.wing.y1 = this.segments[7].y + 10;
                this.wing.x2 = this.segments[7].x + 20;
                this.wing.y2 = this.segments[7].y + 10 + this.m * 15;
                this.wing.x3 = this.segments[7].x - 20;
                this.wing.y3 = this.segments[7].y + 10 + this.m * 30;
                this.wing.x4 = this.segments[7].x - 27;
                this.wing.y4 = this.segments[7].y + 10 + this.m * 35;

                //1st (top) spine
                this.wing.spines.a.x1 = this.wing.x1;
                this.wing.spines.a.y1 = this.segments[7].y + 40 + this.m * 47 - this.m2 * 6;
                this.wing.spines.a.x2 = this.segments[7].x + 90;
                this.wing.spines.a.y2 = this.segments[7].y + 40 + this.m * 55 - this.m2 * 6;

                //2nd spine
                this.wing.spines.b.x1 = this.wing.x1 + 20;
                this.wing.spines.b.y1 = this.segments[7].y + 40 + this.m * 42;
                this.wing.spines.b.x2 = this.segments[7].x + 95;
                this.wing.spines.b.y2 = this.segments[7].y + 40 + this.m * 45;

                //3rd spine
                this.wing.spines.c.x1 = this.wing.x1 + 25;
                this.wing.spines.c.y1 = this.segments[7].y + 40 + this.m * 35;
                this.wing.spines.c.x2 = this.segments[7].x + 100;
                this.wing.spines.c.y2 = this.segments[7].y + 40 + this.m * 32;

                //4th spine
                this.wing.spines.d.x1 = this.wing.x1 + 35;
                this.wing.spines.d.y1 = this.segments[7].y + 40 + this.m * 17;
                this.wing.spines.d.x2 = this.segments[7].x + 80;
                this.wing.spines.d.y2 = this.segments[7].y + 30 + this.m * 15;

                this.x-= this.speed;
            };
            this.drawFrontLeg = function(back) {
                var segment = this.segments[8];
                var node = this.nodes[8];

                pushMatrix();
                    translate(this.segments[8].x, this.segments[8].y);
                    translate(-225, -340);

                    if(back) {
                        translate(-10, 0);
                        fill(this.colors.legsBehind);
                    }
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0