Javascript+canvas making Nine program _javascript skills

Source: Internet
Author: User

JS Core Code

Copy Code code as follows:

/*
*canvasid:html Canvas Tag ID
*imageid:html img Tag ID
*gridcountx:x axis Picture Split number
*gridcounty:y axis Picture Split number
*gridspace: Sudoku Gap
*offsetx:x*y Sudoku Relative Canvas (0,0) x coordinate offset
**offsetx:x*y Sudoku Relative canvas (0,0) y-coordinate offset
*isanimat: Animate display is enabled
*/
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;
Animation
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, G Rid_height);
x < Gridcountx? X + +: × = 0;
if (x = = 0) {
Y < Gridcounty? y++: y = 0;
}
}, 200);
y = = Gridcounty? Clearinterval (inter): null;
else {//non-animation
for (var y = 0; y < Gridcounty; y++) {
for (var x = 0; x < Gridcountx + +) {
G.putimagedata (ImageData, Gridspace * x + OffsetX, Gridspace * y + offsetY, grid_width * x, Grid_height * y, Grid_width, G Rid_height);
}
}
}
}

HTML code

Copy Code code as follows:

<canvas id= "CANVAS1" width= "900px" height= "550px" >canvas demo</canvas>

How to use:

Copy Code code as follows:

eg..
Imagegrid ("Canvas1", "Image1", 3, 3, 2,, 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

The code is very concise, the effect is very cool, the small friends have learned?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.