三分钟HTML5画布(Canvas)动画教程

此文下面的教程中将使用的是另外一个叫做kinetic的Web动画工具包。它们都是开源的。

把鼠标放到上面的小丑脸上,然后移开,就会有如下效果。
这里写图片描述

这里写图片描述

第一步,画五官

这个小丑没有耳朵和眉毛,所以只剩下三官,但它的两个眼睛我们要分别绘制,所以一共是四个部分。下面先看看代码。

绘制左眼的代码

var leftEye = new Kinetic.Line({
        x: 150,
        points: [0, 200, 50, 190, 100, 200, 50, 210],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10     
      });

绘制右眼的代码

var rightEye = new Kinetic.Line({
        x: sw - 250,
        points: [0, 200, 50, 190, 100, 200, 50, 210],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10   
      });
绘制鼻子的代码

var nose = new Kinetic.Line({
        points: [240, 280, sw/2, 300, sw-240,280],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10
      });

绘制嘴巴的代码

var mouth = new Kinetic.Line({
        points: [150, 340, sw/2, 380, sw - 150, 340, sw/2, sh],
        tension: 0.5,
        closed: true,
        stroke: 'red',
        strokeWidth: 10
      });

简单讲解一下上面的代码。Kinetic就是我们使用的js工具包。在页面的头部,我们需要这样引用它:

<script type="text/javascript" src="/js/kineticv5.0.1.min.js"></script>

其它几个分别是几个关键点,线条弹性,颜色,宽度等。这些都很容易理解。

第二步,让图动起来

这个动画之所以能吸引人,是因为它能响应你的鼠标动作,和用户有互动,这是一个成功的动画最关键的地方。如果你仔细观察,这个小丑五官的变化只是形状的变化,眼睛变大,嘴巴变大,鼻子变大,但特别的是这个变化不是瞬间变化,而是有过渡性的,这里面有一些算法,这就是最让人发愁的地方。幸运的是,这算法技术都非常的成熟,不需要我们来思考,在这些动画引擎库里都把这些技术封装成了非常简单方便的接口。下面我们来看看如何让动起来。

左眼的动画

var leftEyeTween = new Kinetic.Tween({
        node: leftEye,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [0, 200, 50, 150, 100, 200, 50, 200]
      });

右眼的动画

var rightEyeTween = new Kinetic.Tween({
        node: rightEye,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [0, 200, 50, 150, 100, 200, 50, 200]
      });

鼻子的动画

var noseTween = new Kinetic.Tween({
        node: nose,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [220, 280, sw/2, 200, sw-220,280]
      });

嘴巴的动画

var mouthTween = new Kinetic.Tween({
        node: mouth,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        points: [100, 250, sw/2, 250, sw - 100, 250, sw/2, sh-20]
      });

这些代码非常的简单,而且变量名能自释其意。稍微有点经验的程序员想看懂这些代码应该不难。基本每段代码都是让你提供一些点,指定动画动作的衰退模式和持续时间。

完整的动画代码

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #container {
        background-color: black;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="/js/lib/kinetic-v5.0.1.min.js"></script>
    <script defer="defer">
      var sw = 578;
      var sh = 400;
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 400
      });
      var layer = new Kinetic.Layer({
        y: -30 
      });

      var leftEye = new Kinetic.Line({
        x: 150,
        points: [0, 200, 50, 190, 100, 200, 50, 210],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10     
      });

      var rightEye = new Kinetic.Line({
        x: sw - 250,
        points: [0, 200, 50, 190, 100, 200, 50, 210],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10   
      });

      var nose = new Kinetic.Line({
        points: [240, 280, sw/2, 300, sw-240,280],
        tension: 0.5,
        closed: true,
        stroke: 'white',
        strokeWidth: 10
      });

      var mouth = new Kinetic.Line({
        points: [150, 340, sw/2, 380, sw - 150, 340, sw/2, sh],
        tension: 0.5,
        closed: true,
        stroke: 'red',
        strokeWidth: 10
      });

      layer.add(leftEye)
           .add(rightEye)
           .add(nose)
           .add(mouth);

      stage.add(layer);

      // tweens

      var leftEyeTween = new Kinetic.Tween({
        node: leftEye,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [0, 200, 50, 150, 100, 200, 50, 200]
      }); 

      var rightEyeTween = new Kinetic.Tween({
        node: rightEye,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [0, 200, 50, 150, 100, 200, 50, 200]
      });

      var noseTween = new Kinetic.Tween({
        node: nose,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        y: -100,
        points: [220, 280, sw/2, 200, sw-220,280]
      }); 

      var mouthTween = new Kinetic.Tween({
        node: mouth,
        duration: 1,
        easing: Kinetic.Easings.ElasticEaseOut,
        points: [100, 250, sw/2, 250, sw - 100, 250, sw/2, sh-20]
      }); 

      stage.getContainer().addEventListener('mouseover', function() {
        leftEyeTween.play();
        rightEyeTween.play();
        noseTween.play();
        mouthTween.play();
      });

      stage.getContainer().addEventListener('mouseout', function() {
        leftEyeTween.reverse();
        rightEyeTween.reverse();
        noseTween.reverse();
        mouthTween.reverse();
      });

    </script>
  </body>
</html>

转自:
http://www.webhek.com/html5-canvas-animation/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值