canvas-图像动画

59 篇文章 0 订阅
50 篇文章 1 订阅

canvas-图像动画 目录


前言

  • canvas开发手册里提到的图像动画效果

效果展示

在这里插入图片描述

代码展示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: rgba(100, 145, 250, 0.3);
        }

        #canvas {
            margin-left: 20px;
            margin-right: 0px;
            margin-bottom: 20px;
            border: thin solid #aaffcc;
            cursor: crosshair;
            padding: 0;
        }

        #controls {
            margin: 20px;
        }

        a {
            font: 18px Times Roman;
            text-decoration: none;
            margin-right: 15px;
        }
    </style>
</head>
<body>
    <div id="controls">
        <input type="button" id="fadeButton" value="Fade Out">
    </div>

    <canvas id="canvas" width="800" height="520">
        Canvas Not Supported!
    </canvas>

    <script src="main.js"></script>
</body>
</html>
// main.js
var image = new Image(),
    canvas = document.getElementById('canvas'),
    context = canvas.getContext('2d'),
    fadeButton = document.getElementById('fadeButton'),
    originalImageData = null,
    interval = null;

// Functions.....................................................

function increaseTransparency(imagedata, steps) {
   var alpha, currentAlpha, step, length = imagedata.data.length;

   for (var i=3; i < length; i+=4) { // For every alpha component
      alpha = originalImageData.data[i];

      if (alpha > 0 && imagedata.data[i] > 0) { // not totally transparent yet
         currentAlpha = imagedata.data[i];
         step = Math.ceil(alpha/steps);

         if (currentAlpha - step > 0) { // not too close to the end
            imagedata.data[i] -= step;  // increase transparency
         }
         else {
            imagedata.data[i] = 0; // end: totally transparent
         }
      }
   }
}

function fadeOut(context, imagedata, x, y,
                 steps, millisecondsPerStep) { 
   var frame = 0,
       length = imagedata.data.length;

   interval = setInterval(function () { // Once every millisecondsPerStep ms
      frame++;

      if (frame > steps) { // animation is over
          clearInterval(interval); // end animation
          animationComplete();     // put picture back in 1s
      }
      else {
        increaseTransparency(imagedata, steps);
         context.putImageData(imagedata, x, y);
      }
   }, millisecondsPerStep);
};

// Animation.....................................................

function animationComplete() {
   setTimeout(function() {
      context.drawImage(image,0,0,canvas.width,canvas.height);
   }, 1000);
}

// Initialization................................................

fadeButton.onclick = function() {
   fadeOut(context,
      context.getImageData(0, 0, canvas.width, canvas.height),
      0, 0, 20, 1000/60);
};

image.src = './log-crossing.png';
image.onload = function() {
   context.drawImage(image, 0, 0, canvas.width, canvas.height);
   originalImageData = context.getImageData(0, 0, canvas.width, canvas.height);
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值