C4D搭配动画效果(二)

[复制链接]
音乐大虾 发表于 2021-7-9 09:42 | 显示全部楼层 |阅读模式
上一章讲述了怎样操做C4D图切割制做闪光效果。本章将讲述怎样在上章文章描述的效果根底上参加椭圆动态效果以及闪动控制。
如下图所示,椭圆运动重要分成两部门:外圈运动、内圈运动。此中内圈运动看似一个椭圆,实际上是由两个椭圆不同角度组成的。红色指向的是运动的小球,绿箭头是小球运动的标的目的。下面将讲述怎样展示圆球运动,重要是运用canvas画布制做的,探测小球的运动轨迹,以及到点则控制对应块的闪烁。

C4D搭配动画效果(二)-1.jpg
一、椭圆运动函数


先理解一下椭圆运动函数circleRunEllipse整体,重要使用函数:
    ellipseRun — 椭圆运动执行drawCircle — 绘画指定位置的运动小球点clearRun — 停止小球运动drawEllipse — 画椭圆运动轨迹,用于调试婚配设想稿drawPoint — 起始点、终止点调试
  1. /**
  2. * 首页运维与效劳后台图动画
  3. * @param canvasID 画布ID
  4. * @param ellipseHeight 椭圆运动高度
  5. * @param ellipseWidth 椭圆运动宽度
  6. * @param type 哪一个圈,大、中、小--【1 | 2 | 3】
  7. */export function circleRunEllipse(canvasID, ellipseHeight, ellipseWidth, type) {    // 根底变量
  8.     let canvas: any;    if (document.getElementById(canvasID)) {
  9.         canvas = document.getElementById(canvasID);
  10.     } else {        return ;
  11.     }    let context = canvas.getContext('2d');    let width = canvas.width = 469;    let height = canvas.height = 1017;    let animationFrame = null; // 记录执行的动画,用于关掉
  12.     // 椭圆运动
  13.     let circleX = width / 2 - 127; // 椭圆运动中心点
  14.     let circleY = height / 2;    let ellipseA = ellipseWidth; // 长轴a
  15.     let ellipseB = ellipseHeight; // 短轴b
  16.     let speed = 0.012; // 控制运动速度
  17.     let ellipseTime = 0; // 控制运动时间变革
  18.     /**
  19.      * 椭圆运动执行
  20.      */
  21.     function ellipseRun() {        if (animationFrame) {            window.cancelAnimationFrame(animationFrame);
  22.         }
  23.         animationFrame = window.requestAnimationFrame(ellipseRun);
  24.         context.clearRect(0, 0, width, height);        // drawEllipse(circleX, circleY);
  25.         drawCircle(circleX + ellipseA * Math.cos(ellipseTime), circleY + ellipseB * Math.sin(ellipseTime));        if (type === 1) {
  26.             context.clearRect(0, 80, 35, 55); // 右侧点(25,80),高宽
  27.             context.clearRect(0, 880, 10, 5); // 左侧点(0,885)
  28.             // 完毕点21.09
  29.             ellipseTime += speed;            if (ellipseTime > 21.09) {                if (!clearAnimationFrame()) {                    document.getElementsByClassName('service-cloud1')[0].className += ' service-cloud';                    document.getElementsByClassName('service-cloud4')[0].className = 'service-cloud4';
  30.                 } else {                    return ;
  31.                 }
  32.                 ellipseTime = 16.8;
  33.                 clearRun(5);
  34.             }
  35.         } else if (type === 2) {
  36.             context.clearRect(0, 720, 210, 200); // 左侧点(208,720),高宽
  37.             context.clearRect(0, 308, 20, 415); // 右侧点(2,308),高宽
  38.             // 完毕点16.29
  39.             ellipseTime -= speed;            if (ellipseTime < 16.29) {                if (!clearAnimationFrame()) {                    document.getElementsByClassName('service-cloud2')[0].className = 'service-cloud2';
  40.                 } else {                    return ;
  41.                 }
  42.                 ellipseTime = 19.45;
  43.                 clearRun(5);
  44.             }
  45.         } else {
  46.             context.clearRect(0, 300, 43, 400); // 左侧点(40,700),高宽
  47.             context.clearRect(0, 0, 175, 307); // 右侧点(175,305),高宽
  48.             context.clearRect(142, 716, 30, 62); // 效劳器右侧点(142,778),高宽,左侧点(169,716)
  49.             // 完毕点18.08
  50.             ellipseTime -= speed;            if (ellipseTime < 18.08) {                if (!clearAnimationFrame()) {                    document.getElementsByClassName('service-cloud3')[0].className += ' service-cloud';
  51.                 } else {                    return ;
  52.                 }
  53.                 ellipseTime = 21.24;
  54.                 clearRun(5);
  55.             }
  56.         }
  57.     }    /**
  58.      * 画实体圆,描述位置
  59.      */
  60.     function drawCircle(x, y) {
  61.         context.save();
  62.         context.fillStyle = '#0ff';
  63.         context.globalAlpha = 0.92;
  64.         context.beginPath();
  65.         context.arc(x, y, 3.5, 0, Math.PI * 2); // 半径3
  66.         context.closePath();
  67.         context.fill();
  68.         context.restore();
  69.     }    /**
  70.      * 画椭圆,用于婚配设想稿途径
  71.      * 1、画椭圆,使用lineTo,把椭圆分割许多片段
  72.      * 2、椭圆的三角函数表达式 x = a*cos(t), y = b * sin(t);
  73.      */
  74.     function drawEllipse(x, y) {        // 这样能够使得每次循环所绘制的途径(弧线)接近1像素
  75.         let step = (ellipseA > ellipseB) ? 1 / ellipseA : 1 / ellipseB;
  76.         context.save();
  77.         context.strokeStyle = 'blue';
  78.         context.beginPath();
  79.         context.moveTo(x + ellipseA, y);        for (let i = 0; i < Math.PI * 2; i += step) {
  80.             context.lineTo(x + ellipseA * Math.cos(i), y + ellipseB * Math.sin(i));
  81.         }
  82.         context.closePath();
  83.         context.stroke();
  84.         context.restore();
  85.     }    /**
  86.      * 定点,用于消除隐藏多余途径
  87.      */
  88.     function drawPoint(x, y) {
  89.         context.save();
  90.         context.fillStyle = 'red';
  91.         context.globalAlpha = 0.95;
  92.         context.beginPath();
  93.         context.arc(x, y, 3, 0, Math.PI * 2);
  94.         context.closePath();
  95.         context.fill();
  96.         context.restore();
  97.     }    /**
  98.      * 停止运动
  99.      * @param time 时间,单元【秒】
  100.      */
  101.     function clearRun(time) {
  102.         context.clearRect(0, 0, width, height);        window.cancelAnimationFrame(animationFrame);        let timer = null;        // 提早一秒执行闪动动画
  103.         let restartTimer = setTimeout(function() {            if (clearAnimationFrame()) {
  104.                 clearTimeout(restartTimer);
  105.                 clearTimeout(timer);                return ;
  106.             }            if (type === 1) {                document.getElementsByClassName('service-cloud1')[0].className = 'service-cloud1';                document.getElementsByClassName('service-cloud4')[0].className += ' service-cloud';
  107.             } else if (type === 2) {                document.getElementsByClassName('service-cloud2')[0].className += ' service-cloud';
  108.             } else {                document.getElementsByClassName('service-cloud3')[0].className = 'service-cloud3';
  109.             }
  110.             clearTimeout(restartTimer);
  111.         }, (time - 1) * 1000);
  112.         timer = setTimeout(function() {
  113.             animationFrame = window.requestAnimationFrame(ellipseRun);
  114.             clearTimeout(timer);
  115.         }, time * 1000);
  116.     }    /**
  117.      * 肃清运动
  118.      */
  119.     function clearAnimationFrame() { ... }    // 指定初步点执行椭圆运动
  120.     if (type === 1) {
  121.         ellipseTime = 16.8;        // ellipseTime = 21.09; // 完毕点
  122.         if (document.getElementsByClassName('service-cloud4').length > 0) {            document.getElementsByClassName('service-cloud4')[0].className += ' service-cloud';
  123.         }
  124.     } else if (type === 2) {
  125.         ellipseTime = 19.45;        // ellipseTime = 16.29; // 完毕点
  126.         if (document.getElementsByClassName('service-cloud2').length > 0) {            document.getElementsByClassName('service-cloud2')[0].className += ' service-cloud';
  127.         }
  128.     } else if (type === 3) {
  129.         ellipseTime = 21.24;        // ellipseTime = 18.08; // 完毕点
  130.     }    let startTimer = setTimeout(function() {
  131.         animationFrame = ellipseRun();
  132.         clearTimeout(startTimer);
  133.     }, 1000);
  134. }
复制代码
二、椭圆运动实现

(1)从图上能够知道每个椭圆大小、倾斜角度均不同,那先得筹备三个不同的画布,能够不同的椭圆运动。在上一章的代码根底上加上轨迹canvans的html代码,如下:
  1. <div class="servicMainOut">
  2.     ......   
  3.     <canvas id="homeCanvasBig" style="transform: rotate(90deg);position: absolute;top: -115px; left: 274px;"></canvas>
  4.     <canvas id="homeCanvasMid" style="transform: rotate(90deg);position: absolute;top:-139px;left:276px;"></canvas>
  5.     <canvas id="homeCanvasSmall" style="transform: rotate(90deg);position: absolute;top:-156px;left: 282px;"></canvas></div>
复制代码
(2) 调用椭圆运动函数:
  1. // 首页效劳与运维动画jscircleRunEllipse('homeCanvasBig', 487, 169, 1);
  2. circleRunEllipse('homeCanvasMid', 369, 123, 2);
  3. setTimeout(function() {
  4.     circleRunEllipse('homeCanvasSmall', 288, 92, 3);
  5. }, 6014);
复制代码
编写这些代码就能够详细实现了完好的小球沿椭圆运动及到点则闪烁及运动了。
三、实现原理

下面以外圈homeCanvasBig为例讲述制做重要原理。
(1) 轨迹探测
        上面的代码已详细实现了整个C4D效果了。但在初步椭圆运动之前,其实是先要探究婚配好整个椭圆的运动轨迹。开启测试的婚配轨迹函数drawEllipse(),暂时关闭小球运动drawCircle()以及注释clearRun(5)以便于调试。

C4D搭配动画效果(二)-2.jpg
效果如下:

C4D搭配动画效果(二)-3.jpg

怎样婚配椭圆轨迹,一般只要逐程序整以下变量:
    ellipseHeight:调整椭圆长袖,椭圆的宽度变革。值越大,圆越大。ellipseWidth:调整椭圆向北倾斜度。值越大,越倾向北。

C4D搭配动画效果(二)-4.jpg
一般还搭配top跟left样式调整。

C4D搭配动画效果(二)-5.jpg

(2) 起始点、终止点定位
        每个椭圆运动都有起始点和终止点,知道这两点的位置才能有利于我们控制隐藏不须要的轨迹以及闪烁效果。其实每个椭圆的初步都是有完好的轨迹的,通过drawPoint(x,  y)函数定义到每一个点,这样就能够使用context.clearRect()函数关掉不存在的轨迹。譬如:

C4D搭配动画效果(二)-6.jpg

(3) 闪烁控制
        通过上面的起点、终点的定位,我们能够console.log出对应的ellipseTime的值。根据ellipseTime的值则可控制能否应当闪烁。而且ellipseTime增值可控制小球顺时针运动,减值则控制逆时针运动。

C4D搭配动画效果(二)-7.jpg

C4D搭配动画效果(二)-8.jpg
睿江云官网链接:https://www.eflycloud.com/home?from=RJ0027
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

菜鸟C4D推荐上一条 /9 下一条

菜鸟C4D与你一起从零开始!
菜鸟C4D微信公众号

( 浙ICP备13033195号-2 )Copyright 2013-2022;菜鸟C4DPowered by Discuz! 技术支持:菜鸟C4D