[HTML5] Canvas simple image rendering tutorial,

Source: Internet
Author: User

[HTML5] Canvas simple image rendering tutorial,

The following small series will bring you a simple image tutorial for [HTML5] Canvas. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at the small Editor and wish you a pleasant game.

Get the Image object, new

Defines the src attribute of the Image object. Parameter: Image path

Defines the onload method of the Image object, and calls the drawImage () method of the context object. parameters: Image object, x coordinate, and y coordinate

Overload method: Call the drawImage () method of the context object. parameters: Image object, x coordinate, y coordinate, Image width, and height

  

 

Overload method, call the drawImage () method of the context object,

Parameters:

Image object, x coordinate on the Image, y coordinate on the Image, rectangular width, rectangular height, x coordinate on the canvas, y coordinate on the canvas, Image width, and Image Height

  

 

Call the getImageData () method of the context object to obtain the pixel color array. parameters: x coordinate, y coordinate, x width, and y width

Call the putImageData () method of the context object to set the image color. parameters: ImageData object, x coordinate, and y coordinate.

  

Var canvas = document. getElementById ('mycanvas '); 2. var context = canvas. getContext ("2d"); 3. // draw the image 4. var img = new Image (); 5. img. src = "1.jpg"; 6. img. onload = function () {7. // context. drawImage (img, 0, 0); 8. // context. drawImage (img, 0, 0, 100,100); 9. context. drawImage (img, 180,160,100,100, 100,100,); 10. var imagedata = context. getImageData (0, 0, 100,100); 11. for (var I = 0, n = imagedata. data. length; I <n; I + = 4) {12. imagedata. data [I + 0] = 255-imagedata. data [I + 0]; // red; 13. imagedata. data [I + 1] = 255-imagedata. data [I + 1]; // green 14. imagedata. data [I + 2] = 255-imagedata. data [I + 2]; 15 .} 16. context. putImageData (imagedata, 0, 0); 17 .}

Related Article

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.