html5 用canvas实现图片自动滑动切换

转自:http://blog.csdn.net/iamke1987/article/details/9886707

图片自动滑动效果很多网站都要用,最近在学html5就拿这个练练手,发现用canvas实现起来其实很简单。代码比较粗糙,有很多改进的地方,不过还是先记录一下。

一. html文件

[html]  view plain copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8"/>  
  5.     <title>HTML5 Images Slider</title>  
  6.     <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>  
  7.     <script src="test.js"></script>  
  8.     <link href="style.css" rel="stylesheet" />  
  9. </head>  
  10. <body>  
  11. <div id="container">  
  12.     <canvas id="imgs" width="500" height="300" onclick="canvasClick()"></canvas>  
  13. </div>  
  14. <div class="imgGallery">  
  15.     <span class="cover"><img src="1.jpg" id="img1" width="125" height="150" onclick="iconClick(this.id)"></span>  
  16.     <img src="2.jpg" id="img2" width="125" height="150" onclick="iconClick(this.id)">  
  17.     <img src="3.jpg" id="img3" width="125" height="150" onclick="iconClick(this.id)">  
  18.     <img src="4.jpg" id="img4" width="125" height="150" onclick="iconClick(this.id)">  
  19. </div>  
  20. </body>  
  21. <footer>  
  22. </footer>  
  23. </html>  

二. js文件,名字test.js

[javascript]  view plain copy
  1. var images = new Array();  
  2. var cIndex = 0;  
  3. var speed = 5;  
  4. var context;  
  5. var canvas;  
  6. var currentImage;  
  7. var width=300;  
  8. var height=300;  
  9. var stopX = 95;  
  10. var stopY = 0;  
  11. var autoTimeout;  
  12. var manuTimeout;  
  13. var interval;  
  14. var img1;  
  15. var img2;  
  16. var img3;  
  17. var img4;  
  18. var timeoutInterval = 5;  
  19.   
  20. function slideImage(id,x,y,imgObj){  
  21.     this.speed = speed;  
  22.     this.preImage = null;  
  23.     this.nextImage = null;  
  24.     this.imgObj=imgObj;  
  25.     this.x=x;  
  26.     this.y=y;  
  27.     this.direction="right";  
  28.     this.id = id;  
  29. }  
  30.   
  31. function buttonNext(x,y,bwidth,bheight){  
  32.     this.x = x;  
  33.     this.y = y;  
  34.     this.width = bwidth;  
  35.     this.height = bheight;  
  36. }  
  37.   
  38. $(document).ready(  
  39.     function(){  
  40.         InitImages();  
  41.         canvas = document.getElementById("imgs");  
  42.         context = canvas.getContext("2d");  
  43.         //移动图片  
  44.         context.drawImage(currentImage.imgObj,currentImage.x,currentImage.y,width,height);  
  45.         context.drawImage(currentImage.preImage.imgObj,currentImage.preImage.x,currentImage.preImage.y,width,height);  
  46.         context.drawImage(currentImage.nextImage.imgObj,currentImage.nextImage.x,currentImage.nextImage.y,width,height);  
  47.         context.fillStyle="rgba(100,150,185,0.5)";  
  48.         context.fillRect(0,0,100,height);  
  49.         context.fillRect(400,0,100,height);  
  50.         interval = setTimeout("intervalSlide()", 20);  
  51.     }  
  52. );  
  53.   
  54. function drawFrame(){  
  55.     context.clearRect(0,0,canvas.width,canvas.height);  
  56.     //调用beginPath()确保不会接着上次绘制的图形绘制  
  57.     context.beginPath();  
  58.     context.drawImage(currentImage.imgObj,currentImage.x,currentImage.y,width,height);  
  59.     context.drawImage(currentImage.preImage.imgObj,currentImage.preImage.x,currentImage.preImage.y,width,height);  
  60.     context.drawImage(currentImage.nextImage.imgObj,currentImage.nextImage.x,currentImage.nextImage.y,width,height);  
  61.     context.drawImage(currentImage.preImage.preImage.imgObj,currentImage.preImage.preImage.x,currentImage.preImage.preImage.y,width,height);  
  62.     //遮罩  
  63.     context.fillStyle="rgba(100,150,185,0.5)";  
  64.     context.fillRect(0,0,100,height);  
  65.     context.fillRect(400,0,100,height);  
  66.     //每一帧的位置变动  
  67.     currentImage.x -= speed;  
  68.     currentImage.preImage.x -= speed;  
  69.     currentImage.nextImage.x -= speed;  
  70.     currentImage.preImage.preImage.x -= speed;  
  71.       
  72.     if(currentImage.x == 200){  
  73.         currentImage.nextImage.x = 500;  
  74.     }  
  75.     //到达指定位置停止变动  
  76.     if(currentImage.x != stopX){  
  77.         autoTimeout = setTimeout("drawFrame()",timeoutInterval);  
  78.     }  
  79.     else{  
  80.           
  81.     }  
  82. }  
  83.   
  84. function InitImages(){  
  85.     img1 = new slideImage("img1",-200,0,document.getElementById("img1"));  
  86.     img2 = new slideImage("img2",100,0,document.getElementById("img2"));  
  87.     img3 = new slideImage("img3",400,0,document.getElementById("img3"));  
  88.     img4 = new slideImage("img4",700,0,document.getElementById("img4"));  
  89.     img1.preImage = img4;  
  90.     img1.nextImage = img2;  
  91.     img2.preImage= img1;  
  92.     img2.nextImage= img3;  
  93.     img3.preImage=img2;  
  94.     img3.nextImage=img4;  
  95.     img4.preImage = img3;  
  96.     img4.nextImage = img1;  
  97.     currentImage=img2;  
  98.     hilightSelectedImg();  
  99. }  
  100.   
  101. function canvasClick(){  
  102.     currentImage = currentImage.nextImage;  
  103.     manuTimeout = setTimeout("drawFrame()",timeoutInterval);  
  104. }  
  105.   
  106. function intervalSlide(){  
  107.     currentImage = currentImage.nextImage;  
  108.     hilightSelectedImg();  
  109.     autoTimeout = setTimeout("drawFrame()", timeoutInterval);  
  110.     setTimeout("intervalSlide()", 5000)  
  111. }  
  112.   
  113. function iconClick(obj){  
  114.     if(obj == "img1"){  
  115.         currentImage = img1;  
  116.     }  
  117.     else if(obj == "img2"){  
  118.         currentImage = img2;  
  119.     }  
  120.     else if(obj == "img3"){  
  121.         currentImage = img3;  
  122.     }  
  123.     else if(obj == "img4"){  
  124.         currentImage = img4;  
  125.     }  
  126.     currentImage.preImage.x = 100;  
  127.     currentImage.preImage.preImage.x = -200;  
  128.     currentImage.x = 400;  
  129.     hilightSelectedImg();  
  130.     manuTimeout = setTimeout("drawFrame()",timeoutInterval);  
  131. }  
  132.   
  133. function hilightSelectedImg(){  
  134.     img1.imgObj.width = 125;  
  135.     img1.imgObj.height = 150;  
  136.     img1.imgObj.style.opacity = 0.5;  
  137.     img2.imgObj.width = 125;  
  138.     img2.imgObj.height = 150;  
  139.     img2.imgObj.style.opacity = 0.5;  
  140.     img3.imgObj.width = 125;  
  141.     img3.imgObj.height = 150;  
  142.     img3.imgObj.style.opacity = 0.5;  
  143.     img4.imgObj.width = 125;  
  144.     img4.imgObj.height = 150;  
  145.     img4.imgObj.style.opacity = 0.5  
  146.     currentImage.imgObj.width = 150;  
  147.     currentImage.imgObj.height = 175;  
  148.     currentImage.imgObj.style.opacity = 1;  
  149. }  

三. css文件。style.css

[css]  view plain copy
  1. canvas {  
  2.     border:1px dashed black;  
  3. }  
  4.   
  5. .imgGallery{  
  6.     width:550px;  
  7.     position:absolute;  
  8.     height:170px  
  9. }  
  10.   
  11. img{  
  12.     opacity:0.5;  
  13. }  

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以遵循以下步骤使用 canvas 实现图片绕画布中心点自动旋转: 1. 获取 canvas 元素和 2D 上下文。 2. 创建一个 Image 对象,设置它的 src 属性为你想要旋转的图片地址。 3. 在 Image 对象的 onload 事件中,设置画布的中心点,然后使用 setInterval() 方法以一定的时间间隔重绘画布。 4. 在重绘画布时,先清除画布,然后将画布中心点设置为旋转中心点,使用 translate() 方法将画布移动到旋转中心点处,使用 rotate() 方法旋转画布,最后使用 drawImage() 方法将图片绘制在画布上。 以下是一个示例代码: ```javascript // 获取 canvas 元素和 2D 上下文 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); // 创建 Image 对象 var img = new Image(); img.src = 'image.jpg'; // 在 Image 对象的 onload 事件中,设置画布的中心点和重绘画布 img.onload = function() { var centerX = canvas.width / 2; var centerY = canvas.height / 2; var angle = 0; setInterval(function() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 将画布移动到旋转中心点处 ctx.translate(centerX, centerY); // 旋转画布 ctx.rotate(angle * Math.PI / 180); // 将图片绘制在画布上 ctx.drawImage(img, -img.width / 2, -img.height / 2); // 恢复画布状态 ctx.rotate(-angle * Math.PI / 180); ctx.translate(-centerX, -centerY); // 更新角度 angle += 1; }, 10); }; ``` 这段代码将每 10 毫秒重绘画布一次,并以每次 1 度的速度旋转画布,从而实现图片绕画布中心点自动旋转的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值