javascript+canvas制作九宫格小程序_javascript技巧
程序员文章站
2022-03-08 17:13:26
...
js核心代码
/*
*canvasid:html canvas标签id
*imageid:html img 标签id
*gridcountX:x轴图片分割个数
*gridcountY:y轴图片分割个数
*gridspace:宫格空隙
*offsetX:x*y宫格相对canvas(0,0)X坐标偏移量
**offsetX:x*y宫格相对canvas(0,0)Y坐标偏移量
*isanimat:是否启用动画显示
*/
function ImageGrid(canvasid, imageid, gridcountX, gridcountY, gridspace, offsetX, offsetY, isanimat) {
var image = new Image();
var g = document.getElementById(canvasid).getContext("2d");
var img=document.getElementById(imageid);
image.src=img.getAttribute("src");
g.drawImage(image, 0, 0);
var imagedata = g.getImageData(0, 0, image.width, image.height);
var grid_width = imagedata.width / gridcountX;
var grid_height = imagedata.height / gridcountY;
//动画
if (isanimat) {
var x = 0,
y = 0;
var inter = setInterval(function() {
g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
x if (x == 0) {
y }
}, 200);
y == gridcountY ? clearInterval(inter) : null;
} else { //非动画
for (var y = 0; y for (var x = 0; x g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
}
}
}
}
//eg...
ImageGrid("canvas1", "image1", 3, 3, 2, 220, 0, true); //3*3
ImageGrid("canvas1", "image1", 4, 4, 2, 440, 0, true); //4*4
ImageGrid("canvas1", "image1", 3, 4, 2, 660, 0, false); //3*4
复制代码 代码如下:
/*
*canvasid:html canvas标签id
*imageid:html img 标签id
*gridcountX:x轴图片分割个数
*gridcountY:y轴图片分割个数
*gridspace:宫格空隙
*offsetX:x*y宫格相对canvas(0,0)X坐标偏移量
**offsetX:x*y宫格相对canvas(0,0)Y坐标偏移量
*isanimat:是否启用动画显示
*/
function ImageGrid(canvasid, imageid, gridcountX, gridcountY, gridspace, offsetX, offsetY, isanimat) {
var image = new Image();
var g = document.getElementById(canvasid).getContext("2d");
var img=document.getElementById(imageid);
image.src=img.getAttribute("src");
g.drawImage(image, 0, 0);
var imagedata = g.getImageData(0, 0, image.width, image.height);
var grid_width = imagedata.width / gridcountX;
var grid_height = imagedata.height / gridcountY;
//动画
if (isanimat) {
var x = 0,
y = 0;
var inter = setInterval(function() {
g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
x if (x == 0) {
y }
}, 200);
y == gridcountY ? clearInterval(inter) : null;
} else { //非动画
for (var y = 0; y for (var x = 0; x g.putImageData(imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
}
}
}
}
html代码
复制代码 代码如下:
使用方法:
复制代码 代码如下:
//eg...
ImageGrid("canvas1", "image1", 3, 3, 2, 220, 0, true); //3*3
ImageGrid("canvas1", "image1", 4, 4, 2, 440, 0, true); //4*4
ImageGrid("canvas1", "image1", 3, 4, 2, 660, 0, false); //3*4
代码很简洁,效果却非常炫酷,小伙伴们学会了吗?
上一篇: 微信小程序中使用async/await语法的方法(代码示例)
下一篇: mysql ——读写分离
推荐阅读
-
原生js结合html5制作小飞龙的简易跳球_javascript技巧
-
基于JavaScript实现简单的随机抽奖小程序_javascript技巧
-
原生js结合html5制作小飞龙的简易跳球_javascript技巧
-
微信小程序中换行空格(多个空格)写法详解_javascript技巧
-
微信小程序中换行空格(多个空格)写法详解_javascript技巧
-
根据服务器时间作为起始,显示时钟的小程序_javascript技巧
-
js倒计时小程序_javascript技巧
-
javascript+canvas制作九宫格小程序_javascript技巧
-
javascript实现的平方米、亩、公顷单位换算小程序_javascript技巧
-
基于JavaScript实现简单的随机抽奖小程序_javascript技巧