HTML5Canvas image processing skills-

Source: Internet
Author: User
This article is translated from the HTML5CanvasTutorial-DisplayingImages article. Canvas tags were added to the HTML5 standard as a new HTML Tag Member many years ago. Before that, people can only use third-party applications to implement dynamic web pages ...,.

This article is translated from HTML 5 Canvas Tutorial-Displaying Images.


Canvas tags were added to the HTML5 standard as a new HTML Tag Member many years ago. Before that, if we want to implement dynamic web applications, we can only use third-party plug-ins, such as Flash or Java. After the Canvas tag is introduced, people directly open the door to the magical Dynamic Application Web page. The content of this tutorial only covers a small part, but it is very important to show and process the canvas labels.

Image Source

The most common way to draw images on a canvas is to use Javascript Image objects. The supported source image formats depend on the support of browsers. However, some typical image formats (such as png, jpg, and gif) are basically correct.

Images can be captured from the loaded elements in the DOM or created as needed.

// Capture the existing images on the page. MyImage = new Image (); myImage. src = paiimage.png ";

In the current version of most browsers that support canvas marking, if you want to draw an image before it is loaded, nothing will happen. That is to say, if you want to draw an image, you need to wait until it is fully loaded. You can use the onload function of the image object for judgment.

// Create an image.myImage = new Image ();myImage.onload = function () {// Draw image.myImage.src = “image.png”;

In all the examples below, our image source will use this 256x256 gorilla.

Basic painting

In the most basic Drawing operation, all you need is the position where the image appears (x and y coordinates ). The position of the image is determined relative to the upper left corner. Using this method, the image can be easily painted on the canvas with its original size.

drawImage (image, x, y)var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 50, 50);ctx.drawImage (myImage, 125, 125);ctx.drawImage (myImage, 210, 210);

Scaling and resizing

To change the image size, you need to use the reloaded drawImage function to provide the desired width and height parameters.

drawImage (image, x, y, width, border=”1″ Height)var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 50, 50, 100, 100);ctx.drawImage (myImage, 125, 125, 200, 50);ctx.drawImage (myImage, 210, 210, 500, 500);

This example demonstrates how to draw an image smaller than the original image, an image with different aspect ratios, and a method that is larger than the original image.

Image Cropping

The last drawImage method is used to crop images.

drawImage (image,sourceX,sourceY,sourceWidth,sourceborder=”1″ Height,destX,destY,destWidth,destborder=”1″ Height)

There are many parameters, but you can basically think of it as a rectangular area from the source image, and then draw it into the target area on the canvas.

var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 0, 0, 50, 50, 25, 25, 100, 100);ctx.drawImage (myImage, 125, 125, 100, 100, 125, 125, 150, 150);ctx.drawImage (myImage, 80, 80, 100, 100, 250, 250, 220, 220); 

The above is the content of HTML5 Canvas image processing techniques. For more information, see PHP Chinese website (www.php1.cn )!

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.