码迷,mamicode.com
首页 > 编程语言 > 详细

javascript canvas九宫格小程序

时间:2014-12-24 01:17:16      阅读:958      评论:0      收藏:0      [点我收藏+]

标签:

 

js核心代码

技术分享
 1 /*
 2  *canvasid:html canvas标签id
 3  *imageid:html img 标签id
 4  *gridcountX:x轴图片分割个数
 5  *gridcountY:y轴图片分割个数
 6  *gridspace:宫格空隙
 7  *offsetX:x*y宫格相对canvas(0,0)X坐标偏移量
 8  **offsetX:x*y宫格相对canvas(0,0)Y坐标偏移量
 9  *isanimat:是否启用动画显示
10  */
11 function ImageGrid(canvasid, imageid, gridcountX, gridcountY, gridspace, offsetX, offsetY, isanimat) {
12     var image = new Image();
13     var g = document.getElementById(canvasid).getContext("2d");
14     var img=document.getElementById(imageid);
15     image.src=img.getAttribute("src");
16     g.drawImage(image, 0, 0);
17     var imagedata = g.getImageData(0, 0, image.width, image.height);
18     var grid_width = imagedata.width / gridcountX;
19     var grid_height = imagedata.height / gridcountY;
20     //动画
21     if (isanimat) {
22         var x = 0,
23             y = 0;
24         var inter = setInterval(function() {
25             g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
26             x < gridcountX ? x++ : x = 0;
27             if (x == 0) {
28                 y < gridcountY ? y++ : y = 0;
29             }
30         }, 200);
31         y == gridcountY ? clearInterval(inter) : null;
32     } else { //非动画
33         for (var y = 0; y < gridcountY; y++) {
34             for (var x = 0; x < gridcountX; x++) {
35                 g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
36             }
37         }
38     }
39 }
View Code

 http://files.cnblogs.com/veiny/image_grid.xml

下载该xml文件后缀名改为html,打开浏览器观看。

 

javascript canvas九宫格小程序

标签:

原文地址:http://www.cnblogs.com/veiny/p/4181480.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!