HTML5 - Canvas动画效果的例子

1,样例说明

(1)在没有鼠标介入的情况下,这些球就像有磁性一样拼成“Google”字样。
(2)在鼠标移动到其中后,小球像是受到了排斥,向画布的四周扩散,然后不规则地反弹回来。

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      canvas {
        cursor: pointer;
        border: 1px solid black;
        margin:20px 20px 20px 20px;
    }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      window.requestAnimFrame = (function(callback) {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
          window.setTimeout(callback, 1000 / 60);
        };
      })();
 
      function initBalls() {
        balls = [];
 
        var blue = '#3A5BCD';
        var red = '#EF2B36';
        var yellow = '#FFC636';
        var green = '#02A817';
 
        // G
        balls.push(new Ball(173, 63, 0, 0, blue));
        balls.push(new Ball(158, 53, 0, 0, blue));
        balls.push(new Ball(143, 52, 0, 0, blue));
        balls.push(new Ball(130, 53, 0, 0, blue));
        balls.push(new Ball(117, 58, 0, 0, blue));
        balls.push(new Ball(110, 70, 0, 0, blue));
        balls.push(new Ball(102, 82, 0, 0, blue));
        balls.push(new Ball(104, 96, 0, 0, blue));
        balls.push(new Ball(105, 107, 0, 0, blue));
        balls.push(new Ball(110, 120, 0, 0, blue));
        balls.push(new Ball(124, 130, 0, 0, blue));
        balls.push(new Ball(139, 136, 0, 0, blue));
        balls.push(new Ball(152, 136, 0, 0, blue));
        balls.push(new Ball(166, 136, 0, 0, blue));
        balls.push(new Ball(174, 127, 0, 0, blue));
        balls.push(new Ball(179, 110, 0, 0, blue));
        balls.push(new Ball(166, 109, 0, 0, blue));
        balls.push(new Ball(156, 110, 0, 0, blue));
 
        // O
        balls.push(new Ball(210, 81, 0, 0, red));
        balls.push(new Ball(197, 91, 0, 0, red));
        balls.push(new Ball(196, 103, 0, 0, red));
        balls.push(new Ball(200, 116, 0, 0, red));
        balls.push(new Ball(209, 127, 0, 0, red));
        balls.push(new Ball(223, 130, 0, 0, red));
        balls.push(new Ball(237, 127, 0, 0, red));
        balls.push(new Ball(244, 114, 0, 0, red));
        balls.push(new Ball(242, 98, 0, 0, red));
        balls.push(new Ball(237, 86, 0, 0, red));
        balls.push(new Ball(225, 81, 0, 0, red));
 
        // O
        var oOffset = 67;
        balls.push(new Ball(oOffset + 210, 81, 0, 0, yellow));
        balls.push(new Ball(oOffset + 197, 91, 0, 0, yellow));
        balls.push(new Ball(oOffset + 196, 103, 0, 0, yellow));
        balls.push(new Ball(oOffset + 200, 116, 0, 0, yellow));
        balls.push(new Ball(oOffset + 209, 127, 0, 0, yellow));
        balls.push(new Ball(oOffset + 223, 130, 0, 0, yellow));
        balls.push(new Ball(oOffset + 237, 127, 0, 0, yellow));
        balls.push(new Ball(oOffset + 244, 114, 0, 0, yellow));
        balls.push(new Ball(oOffset + 242, 98, 0, 0, yellow));
        balls.push(new Ball(oOffset + 237, 86, 0, 0, yellow));
        balls.push(new Ball(oOffset + 225, 81, 0, 0, yellow));
 
        // G
        balls.push(new Ball(370, 80, 0, 0, blue));
        balls.push(new Ball(358, 79, 0, 0, blue));
        balls.push(new Ball(346, 79, 0, 0, blue));
        balls.push(new Ball(335, 84, 0, 0, blue));
        balls.push(new Ball(330, 98, 0, 0, blue));
        balls.push(new Ball(334, 111, 0, 0, blue));
        balls.push(new Ball(348, 116, 0, 0, blue));
        balls.push(new Ball(362, 109, 0, 0, blue));
        balls.push(new Ball(362, 94, 0, 0, blue));
        balls.push(new Ball(355, 128, 0, 0, blue));
        balls.push(new Ball(340, 135, 0, 0, blue));
        balls.push(new Ball(327, 142, 0, 0, blue));
        balls.push(new Ball(325, 155, 0, 0, blue));
        balls.push(new Ball(339, 165, 0, 0, blue));
        balls.push(new Ball(352, 166, 0, 0, blue));
        balls.push(new Ball(367, 161, 0, 0, blue));
        balls.push(new Ball(371, 149, 0, 0, blue));
        balls.push(new Ball(366, 137, 0, 0, blue));
 
        // L
        balls.push(new Ball(394, 49, 0, 0, green));
        balls.push(new Ball(381, 50, 0, 0, green));
        balls.push(new Ball(391, 61, 0, 0, green));
        balls.push(new Ball(390, 73, 0, 0, green));
        balls.push(new Ball(392, 89, 0, 0, green));
        balls.push(new Ball(390, 105, 0, 0, green));
        balls.push(new Ball(390, 118, 0, 0, green));
        balls.push(new Ball(388, 128, 0, 0, green));
        balls.push(new Ball(400, 128, 0, 0, green));
 
        // E
        balls.push(new Ball(426, 101, 0, 0, red));
        balls.push(new Ball(436, 98, 0, 0, red));
        balls.push(new Ball(451, 95, 0, 0, red));
        balls.push(new Ball(449, 83, 0, 0, red));
        balls.push(new Ball(443, 78, 0, 0, red));
        balls.push(new Ball(430, 77, 0, 0, red));
        balls.push(new Ball(418, 82, 0, 0, red));
        balls.push(new Ball(414, 93, 0, 0, red));
        balls.push(new Ball(412, 108, 0, 0, red));
        balls.push(new Ball(420, 120, 0, 0, red));
        balls.push(new Ball(430, 127, 0, 0, red));
        balls.push(new Ball(442, 130, 0, 0, red));
        balls.push(new Ball(450, 125, 0, 0, red));
 
        return balls;
      }
      function getMousePos(canvas, evt) {
        // get canvas position
        var obj = canvas;
        var top = 0;
        var left = 0;
        while(obj.tagName != 'BODY') {
          top += obj.offsetTop;
          left += obj.offsetLeft;
          obj = obj.offsetParent;
        }
 
        // return relative mouse position
        var mouseX = evt.clientX - left + window.pageXOffset;
        var mouseY = evt.clientY - top + window.pageYOffset;
        return {
          x: mouseX,
          y: mouseY
        };
      }
      function updateBalls(canvas, balls, timeDiff, mousePos) {
        var context = canvas.getContext('2d');
        var collisionDamper = 0.3;
        var floorFriction = 0.0005 * timeDiff;
        var mouseForceMultiplier = 1 * timeDiff;
        var restoreForce = 0.002 * timeDiff;
 
        for(var n = 0; n < balls.length; n++) {
          var ball = balls[n];
          // set ball position based on velocity
          ball.y += ball.vy;
          ball.x += ball.vx;
 
          // restore forces
          if(ball.x > ball.origX) {
            ball.vx -= restoreForce;
          }
          else {
            ball.vx += restoreForce;
          }
          if(ball.y > ball.origY) {
            ball.vy -= restoreForce;
          }
          else {
            ball.vy += restoreForce;
          }
 
          // mouse forces
          var mouseX = mousePos.x;
          var mouseY = mousePos.y;
 
          var distX = ball.x - mouseX;
          var distY = ball.y - mouseY;
 
          var radius = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
 
          var totalDist = Math.abs(distX) + Math.abs(distY);
 
          var forceX = (Math.abs(distX) / totalDist) * (1 / radius) * mouseForceMultiplier;
          var forceY = (Math.abs(distY) / totalDist) * (1 / radius) * mouseForceMultiplier;
 
          if(distX > 0) {// mouse is left of ball
            ball.vx += forceX;
          }
          else {
            ball.vx -= forceX;
          }
          if(distY > 0) {// mouse is on top of ball
            ball.vy += forceY;
          }
          else {
            ball.vy -= forceY;
          }
 
          // floor friction
          if(ball.vx > 0) {
            ball.vx -= floorFriction;
          }
          else if(ball.vx < 0) {
            ball.vx += floorFriction;
          }
          if(ball.vy > 0) {
            ball.vy -= floorFriction;
          }
          else if(ball.vy < 0) {
            ball.vy += floorFriction;
          }
 
          // floor condition
          if(ball.y > (canvas.height - ball.radius)) {
            ball.y = canvas.height - ball.radius - 2;
            ball.vy *= -1;
            ball.vy *= (1 - collisionDamper);
          }
 
          // ceiling condition
          if(ball.y < (ball.radius)) {
            ball.y = ball.radius + 2;
            ball.vy *= -1;
            ball.vy *= (1 - collisionDamper);
          }
 
          // right wall condition
          if(ball.x > (canvas.width - ball.radius)) {
            ball.x = canvas.width - ball.radius - 2;
            ball.vx *= -1;
            ball.vx *= (1 - collisionDamper);
          }
 
          // left wall condition
          if(ball.x < (ball.radius)) {
            ball.x = ball.radius + 2;
            ball.vx *= -1;
            ball.vx *= (1 - collisionDamper);
          }
        }
      }
      function Ball(x, y, vx, vy, color) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.color = color;
        this.origX = x;
        this.origY = y;
        this.radius = 10;
      }
      function animate(canvas, balls, lastTime, mousePos) {
        var context = canvas.getContext('2d');
 
        // update
        var date = new Date();
        var time = date.getTime();
        var timeDiff = time - lastTime;
        updateBalls(canvas, balls, timeDiff, mousePos);
        lastTime = time;
 
        // clear
        context.clearRect(0, 0, canvas.width, canvas.height);
 
        // render
        for(var n = 0; n < balls.length; n++) {
          var ball = balls[n];
          context.beginPath();
          context.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI, false);
          context.fillStyle = ball.color;
          context.fill();
        }
 
        // request new frame
        requestAnimFrame(function() {
          animate(canvas, balls, lastTime, mousePos);
        });
      }
      var canvas = document.getElementById('myCanvas');
      var balls = initBalls();
      var date = new Date();
      var time = date.getTime();
      /*
       * set mouse position really far away
       * so the mouse forces are nearly obsolete
       */
      var mousePos = {
        x: 9999,
        y: 9999
      };
 
      canvas.addEventListener('mousemove', function(evt) {
        var pos = getMousePos(canvas, evt);
        mousePos.x = pos.x;
        mousePos.y = pos.y;
      });
 
      canvas.addEventListener('mouseout', function(evt) {
        mousePos.x = 9999;
        mousePos.y = 9999;
      });
      animate(canvas, balls, time, mousePos);
 
    </script>
  </body>
</html>

原文出自: 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1059.html

时间: 2024-09-19 20:43:42

HTML5 - Canvas动画效果的例子的相关文章

8个经典炫酷的HTML5 Canvas动画欣赏

HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮助. 1.HTML5 Canvas可拖动的弹性大树摇摆动画 今天让我们继续来分享一个炫酷的HTML5动画,它是一款基于HTML5 Canvas的大树摇摆动画,这款HTML5动画的特点是我们可以拖拽树枝,从而让整棵树摇摆起来,这样就真实地模拟了大树从摇摆到静止的整个过程,相当逼真. 在线演示     

HTML5 Canvas开发迷宫游戏例子

本文介绍如何使用Canvas画布制作一个简单的迷宫游戏:用户按下方向键时,笑脸图标会沿相应的方向移动(移动时有动画效果),遇到墙壁时(碰撞检测)就会停下来. 1,在线样例加载困难迷宫 加载简单迷宫 开发迷宫游戏例子-html5 canvas绘图例子"> 2,实现原理(1)迷宫的生成:我们这里是使用一幅迷宫图片,然后把整幅图绘制到画布上. (2)通过设置 window.onkeydown 的响应函数 processKey(),当用户按下方向键的时候,根据键码调整笑脸各方向上的速度. (3)d

HTML5 Canvas弹跳小球小例子

1,Canvas实现动画的原理 (1)首先设置一个定时器,反复调用绘图函数(一般每秒30~40次).每次调用,都会重绘整个画布.完成后的效果就像动画一样,每一帧间过渡会平滑而流畅. (2)有两种方法都可以实现重复绘制:setTimeout()和setInterval().用哪个可以自行决定. (3)setInterval() 能保证精确地按时重绘,当又可能因此牺牲性能(如果绘图代码执行时间比设定时间还要长,浏览器将很难跟上,随着绘图代码的连续执行,页面会出现短暂地停顿) (4)本例使用 setT

HTML5 Canvas画图板的例子

功能很简单,原理其实和拖放是类似的,主要是三个事件: 在canvas 上绑定mousedown 事件以标志绘画的开始(调用moveTo 移动画笔) 在document 上绑定mousemove 事件来处理绘画时的行为(调用lineTo 以及stroke 进行绘画) 在document 上绑定mouseup 事件以标志绘画的结束(解绑document 上的两个事件) 实现时需特别注意的一点是调用moveTo 以及lineTo 方法时如何传递正确的坐标值,这个坐标值应该是光标相对于canvas 左上

HTML5 Canvas draw方法制作动画效果示例

HTML5 Canvas动画效果演示 主要思想: 首先要准备一张有连续帧的图片,然后利用HTML5 Canvas的draw方法在不同的时间间隔绘制不同的帧,这样看起来就像动画在播放. 关键技术点: JavaScript 函数setTimeout()有两个参数,第一个是参数可以传递一个JavaScript方法, 另外一个参数代表间隔时间,单位为毫秒数.代码示例: setTimeout( update, 1000/30); Canvas的API-drawImage()方法,需要指定全部9个参数: c

HTML5 Canvas 超炫酷烟花绽放动画教程

这是一个很酷的HTML5 Canvas动画,它将模拟的是我们现实生活中烟花绽放的动画特效,效果非常逼真,但是毕竟是电脑模拟,带女朋友看就算了,效果还是差了点,呵呵.这个 HTML5 Canvas动画有一点比较出色,就是其性能,Chrome上基本没有卡的感觉,就算你放出很多烟花也一样. 你可以在这里看在线演示 下面我们来简单分析一下实现这款HTML5烟花特效的过程及代码,主要由HTML代码.CSS代码以及Javascript代码组成,当然javascript代码是最重要的. HTML代码: <di

jQuery实现动画效果的简单实例_jquery

一.需求原因目前jQuery已经是一个比较成熟的框架了,而且基于他的插件也有上百种,本例我手动用jQuery实现一个动画效果的例子. 二.具体实现 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv

HTML5 实现小车动画效果(Canvas/CSS3/JQuery)

HTML5正在变得越来越流行.在这个移动设备日益增长的时代,对来自Adobe的Flash插件的改造需求也正在快速增长.因为就在最近,Adobe宣布Flash将不再支持移动设备.这意味着,Adobe自身也认为对移动设备来讲HTML5是一项重要的技术.而桌面系统的改变也是迟早的事. HTML的一大劣势就是对于多媒体技术支持的缺乏.在HTML中,你无法直接显示一个视频或在屏幕上绘画.在HTML5中,随着<video>与<canvas>元素的引进.这些元素给予开发者直接使用"纯粹

javascript+HTML5的Canvas实现Lab单车动画效果_javascript技巧

本文实例讲述了javascript+HTML5的Canvas实现Lab单车动画效果.分享给大家供大家参考.具体如下: 这里运用HTML5实现的单车动画,运行一下效果你就明白了,不过不要在IE8下测试,没效果的.请在支持最新HTML3和CSS3的浏览器下测试,祝您好运. 运行效果截图如下: 具体代码如下: <html> <head> <title>Canvas Lab单车动画,HTML5动画</title> </head> <script t