# HTML5 canvas 5

 网页制作Webjx文章简介：Canvas 相当有趣的一项功能就是可以引入图像，它可以用于图片合成或者制作背景等。而目前仅可以在图像中加入文字（标准说明中并没有包含绘制文字的功能）。只要是 Gecko 支持的图像（如 PNG，GIF，JPEG等）都可以引入到 canvas 中，而且其它的 canvas 元素也可以作为图

# 应用图像 Using images

One of the more fun features of the canvas is the abillity to use images. These can be used to do dynamic photo compositing or used as backdrops of graphs etc. It's currently also the only way to add text to them (The specification does not contain any functions to draw text). External images can be used in any format supported by Gecko (e.g. PNG, GIF or JPEG format). Other canvas elements on the same page can also be used as the source.

Canvas 相当有趣的一项功能就是可以引入图像，它可以用于图片合成或者制作背景等。而目前仅可以在图像中加入文字（标准说明中并没有包含绘制文字的功能）。只要是 Gecko 支持的图像（如 PNG，GIF，JPEG等）都可以引入到 canvas 中，而且其它的 canvas 元素也可以作为图像的来源。

## 引入图像 Importing images

Importing images is basically a two step process:

• Firstly we need a reference to a JavaScript Image object or other canvas element as a source. It isn't possible to use images by simply providing a URL/path to them.
• Secondly we draw the image on the canvas using the drawImage function.
• 第一当然是来源图片，不是简单的 URL 路径，但可以是一个 JavaScript 的 Image 对象引用，又或者其它的 canvas 元素。
• 然后用 drawImage 方法将图像插入到 canvas 中。

Let's look at step one first. There are basically four options available:

#### 引用页面内的图片 Using images which are on the same page

We can access all images on a page by using either the document.images collection, thedocument.getElementsByTagName method, or if we know the ID attribute of the image, thedocument.getElementById method.

#### 使用其它 canvas 元素 Using other canvas elements

Just as with normal images we access other canvas elements using either thedocument.getElementsByTagName method or the document.getElementById method. Make sure you've drawn something to the source canvas before using it in your target canvas.

One of the more practical uses of this would be to use a second canvas element as a thumbnail view of the other larger canvas.

<

#### 由零开始创建图像 Creating an image from scratch

Another option is to create new Image objects in our script. The main disadvantage of this approach is that if we don't want our script to halt in the middle because it needs to wait for an image to load, we need some form of image preloading.

Basically to create a new image object we do this:

1. var img = new Image();   // Create new Image object
2. img.src = 'myImage.png'// Set source path
var img = new Image();   // Create new Image object
img.src = 'myImage.png'; // Set source path

When this script gets executed, the image starts loading. If loading isn't finished when a drawImage statement gets executed, the script halts until the image is finished loading. If you don't want this to happen, use an onload event handler:

1. var img = new Image();   // Create new Image object
3.   // execute drawImage statements here
4. }
5. img.src = 'myImage.png'// Set source path
var img = new Image();   // Create new Image object
// execute drawImage statements here
}
img.src = 'myImage.png'; // Set source path

If you're only using one external image this can be a good approach but once you need to track more than one we need to resort to something more cunning. It's beyond the scope of this tutorial to look at image preloading tactics but you can check out JavaScript Image Preloader for a complete solution.

#### 通过 data: url 方式嵌入图像 Embedding an image via data: url

Another possible way to include images is via the data: url. Data urls allow you to completely define an image as a Base64 encoded string of characters directly in your code. One advantage of data urls is that the resulting image is available immediately without another round trip to the server. ( Another advantage is that it is then possible to encapsulate in one file all of your CSS, Javascript, HTML, and images, making it more portable to other locations. ) Some disadvantages of this method are that your image is not cached, and for larger images the encoded url can become quite long:

1. var img_src = 'data:image/gif;base64,R0lGODlhCwALAIAAAAAA3pn/ZiH5BAEAAAEALAAAAAALAAsAAAIUhA+hkcuO4lmNVindo7qyrIXiGBYAOw==';
var img_src = 'data:image/gif;base64,R0lGODlhCwALAIAAAAAA3pn/ZiH5BAEAAAEALAAAAAALAAsAAAIUhA+hkcuO4lmNVindo7qyrIXiGBYAOw==';

## drawImage

Once we have a reference to our source image object we can use the drawImage method to render it to the canvas. As we we'll see later the drawImage method is overloaded and has three different variants. In its most basic form it looks like this.

drawImage(image, x, y)

Where image is a reference to our image or canvas object. x and y form the coordinate on the target canvas where our image should be placed.

#### drawImage 示例 1

In the following example I will be using an external image as the backdrop of a small line graph. Using backdrops can make your script considerably smaller because we don't need to draw an elaborate background. I'm only using one image here so I use the image object's onload event handler to execute the drawing statements. The drawImage method places the backdrop on the coordinate (0,0) which is the top left corner of the canvas.

function draw() {

1.     var ctx = document.getElementById('canvas').getContext('2d');
2.     var img = new Image();
4.       ctx.drawImage(img,0,0);
5.       ctx.beginPath();
6.       ctx.moveTo(30,96);
7.       ctx.lineTo(70,66);
8.       ctx.lineTo(103,76);
9.       ctx.lineTo(170,15);
10.       ctx.stroke();
11.     }
12.     img.src = 'images/backdrop.png';
13.   }
function draw() {
var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image();
ctx.drawImage(img,0,0);
ctx.beginPath();
ctx.moveTo(30,96);
ctx.lineTo(70,66);
ctx.lineTo(103,76);
ctx.lineTo(170,15);
ctx.stroke();
}
img.src = 'images/backdrop.png';
}

## 缩放 Scaling

The second variant of the drawImage method adds two new parameters and it allows us to place scaled images on the canvas.

drawImage 方法的又一变种是增加了两个用于控制图像在 canvas 中缩放的参数。

drawImage(image, x, y, width, height)

Where width and height is the image's size on the target canvas. 当中 width 和 height 分别是图像在 canvas 中显示大小。

#### drawImage 示例 2

In this example I'm going to use an image as a wallpaper and repeat it several times on the canvas. This is done simply by looping and placing the scaled images at different positions. In the code below the first for loops through the rows the second for loop the columns. The image is scaled one third of its original size which is 50x38 pixels. We'll see how this could also have been achieved, by creating a custom pattern, later in this tutorial.

Note: Images can become blurry when scaling up or grainy if they're scaled down too much. Scaling is probably best not done if you've got some text in it which needs to remain legible.

function draw() {

1.     var ctx = document.getElementById('canvas').getContext('2d');
2.     var img = new Image();
4.       for (i=0;i<4;i++){
5.         for (j=0;j<3;j++){
6.           ctx.drawImage(img,j*50,i*38,50,38);
7.         }
8.       }
9.     }
10.     img.src = 'images/rhino.jpg';
11.   }
function draw() {
var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image();
for (i=0;i<4;i++){
for (j=0;j<3;j++){
ctx.drawImage(img,j*50,i*38,50,38);
}
}
}
img.src = 'images/rhino.jpg';
}

## 切片 Slicing

The third and last variant of the drawImage method has eight new parameters. We can use this method to slice parts of a source image and draw them to the canvas.

drawImage 方法的第三个也是最后一个变种有8个新参数，用于控制做切片显示的。

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

The first parameter image, just as with the other variants, is either a reference to an image object or a reference to a different canvas element. For the other eight parametes it's best to look at the image on the right. The first four parameters define the location and size of the slice on the source image. The last four parameters define the position and size on the destination canvas.

Slicing can be a useful tool when you want to make compositions. You could have all elements in a single image file and use this method to composite a complete drawing. For instance, if you want to make a chart you could have a PNG image containing all the necessary text in a single file and depending on your data could change the scale of your chart without very much diffculty. Another advantage is that you don't need to load every image individually.

#### drawImage 示例 3

In this example I'm going to use the same rhino as we've seen above, but now I'm going to slice its head out and composite it into a picture frame. The image of the picture frame includes a dropshadow which has been saved as a 24-bit PNG image. Because 24-bit PNG images include a full 8-bit alpha channel, unlike GIF and 8-bit PNG images, I can place it onto any background and don't have to worry about a matte color.

I took a different approach to the loading of the images than the example above. I just placed the images directly in my HTML document and used a CSS rule to hide them from view (display:none). I assigned both images an id attribute to make them easier to select. The script itself is very simple. I first draw the sliced and scaled image on the canvas (first drawImage statement), and then place the frame on top (second drawImagestatement).

1. function draw() {
2.   var canvas = document.getElementById('canvas');
3.   var ctx = canvas.getContext('2d');
4.
5.   // Draw slice
6.   ctx.drawImage(document.getElementById('source'),
7.                 33,71,104,124,21,20,87,104);
8.
9.   // Draw frame
10.   ctx.drawImage(document.getElementById('frame'),0,0);
11. }
function draw() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

// Draw slice
ctx.drawImage(document.getElementById('source'),
33,71,104,124,21,20,87,104);

// Draw frame
ctx.drawImage(document.getElementById('frame'),0,0);
}

## Art gallery example

In the final example of this chapter I've made a little art gallery. The gallery consists of a table containing several images. When the page is loaded, for each image in the page a canvas element is inserted and a frame is drawn arround it.

In my case, all images have a fixed width and height, and so does the frame that's drawn around it. You could enhance the script so that it uses the image's width and height to make the frame fit perfectly around it.

The code below should be self-explanatory. We loop through the images array and add new canvas elements accordingly. Probably the only thing to note, for those not so familar with the DOM, is the use of the insertBefore method. insertBefore is a method of the parent node (a table cell) of the element (the image) before which we want to insert our new node (the canvas element).

1. function draw() {
2.
3.   // Loop through all images
4.   for (i=0;i<document.images.length;i++){
5.
6.     // Don't add a canvas for the frame image
7.     if (document.images[i].getAttribute('id')!='frame'){
8.
9.       // Create canvas element
10.       canvas = document.createElement('CANVAS');
11.       canvas.setAttribute('width',132);
12.       canvas.setAttribute('height',150);
13.
14.       // Insert before the image
15.       document.images[i].parentNode.insertBefore(canvas,document.images[i]);
16.
17.       ctx = canvas.getContext('2d');
18.
19.       // Draw image to canvas
20.       ctx.drawImage(document.images[i],15,20);
21.
23.       ctx.drawImage(document.getElementById('frame'),0,0);
24.     }
25.   }
26. }

• 本文已收录于以下专栏：

## HTML5 Canvas(2nd) 无水印pdf

• 2017-10-05 18:04
• 28.97MB
• 下载

## HTML5 CANVAS基础教程

• 2015-08-21 18:01
• 20.77MB
• 下载

## HTML5 Canvas核心技术 源码

• 2015-07-24 16:34
• 17.41MB
• 下载

## 4款基于html5 canvas充满想象力的重力特效

• 2016-01-18 14:35
• 36KB
• 下载

## Html5 Canvas API

• 2015-03-12 16:52
• 30KB
• 下载

## HTML5 Canvas

• 2015-02-24 17:46
• 29.23MB
• 下载

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)