Home > Web Front-end > HTML Tutorial > Getting Started with Canvas (3): Image Processing and Drawing Text_html/css_WEB-ITnose

Getting Started with Canvas (3): Image Processing and Drawing Text_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-24 11:56:23
Original
1271 people have browsed it

Source: http://www.ido321.com/997.html

1. Image processing (unless otherwise specified, all results are from the latest version of Google)

In HTML 5, the Canvas API can not only be used to draw graphics, but can also be used to process image files on the network or disk, and then draw them in the canvas. When drawing an image, you need to use the drawImage() method:

drawImage(image,x,y): image is an image reference, x,y is the starting position in the canvas when drawing the image

drawImage(image,x,y,w,h): The first three are the same as above, w and h are the width and height of the image when drawing, which can be used to scale the image

drawImage(image,sx,sy, sw,sh,dx,dy,dw.dh): Copy all or part of the drawn image in the canvas to another location on the canvas. sx, sy, sw, sh are respectively the starting position, width and height of the copied area in the original image, and dx, dy, dw, dh represent the starting position, height and width of the copied image in the canvas.

   1: // 获取canvas 的ID
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: var canvas = document.getElementById('canvas');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   3: if (canvas == null)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   4: {
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   5: return false;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   6: }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   7: // 获取上下文
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   8: var context = canvas.getContext('2d');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   9: context.fillStyle = '#eeeeff';
Copy after login
Copy after login
  10: context.fillRect(0,0,400,300);
Copy after login
Copy after login
  11: var image = new Image();
Copy after login
Copy after login

12: image.src = 'my.jpg';

// onload event implements loading while drawing

  13: image.onload = function()
Copy after login
  14: {
Copy after login
  15: drawImage(context,image);
Copy after login
  16: };
Copy after login
  17: function drawImage(context,image)
Copy after login
  18: {
Copy after login
  19: for (var i = 0; i < 7; i++) {
Copy after login
  20: context.drawImage(image,0+i*50,0+i*25,100,100);
Copy after login
  21: };
Copy after login
Copy after login
  22: }
Copy after login

Effect:

1. Image tiling

   1: // 获取canvas 的ID
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: var canvas = document.getElementById('canvas');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   3: if (canvas == null)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   4: {
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   5: return false;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   6: }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   7: // 获取上下文
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   8: var context = canvas.getContext('2d');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   9: context.fillStyle = '#eeeeff';
Copy after login
Copy after login
  10: context.fillRect(0,0,400,300);
Copy after login
Copy after login
  11: var image = new Image();
Copy after login
Copy after login
  12: image.src = 'my.jpg';
Copy after login
  13: // onload事件实现边绘制边加载
Copy after login
  14: image.onload = function()
Copy after login
  15: {
Copy after login
  16: drawImage(canvas,context,image);
Copy after login
  17: };
Copy after login
  18: function drawImage(canvas,context,image)
Copy after login
  19: {
Copy after login
Copy after login
  20: // 平铺比例
Copy after login
  21: var scale = 5;
Copy after login
  22: // 缩小图像后宽度
Copy after login
  23: var n1 = image.width / scale;
Copy after login
  24: // 缩小图像后高度
Copy after login
  25: var n2 = image.height / scale;
Copy after login
  26: // 横向平铺个数
Copy after login
  27: var n3 = canvas.width / n1;
Copy after login
  28: // 纵向平铺个数
Copy after login
  29: var n4 = canvas.height / n2;
Copy after login
  30: for(var i = 0; i < n3; i++)
Copy after login
  31: {
Copy after login
  32: for(var j=0; j < n4; j++)
Copy after login
  33: {
Copy after login
  34: context.drawImage(image,i*n1,j*n2,n1,n2);
Copy after login
  35: }
Copy after login
  36: }
Copy after login
  37: }
Copy after login

Effect:

In HTML 5, tiling can also be achieved using context.createPattern(image,type), and the type value is the same The tiling values ​​of background-image are the same.

   1: image.onload = function()
Copy after login
   2: {
Copy after login
   3: // drawImage(canvas,context,image);
Copy after login
   4: var ptrn = context.createPattern(image,'repeat');
Copy after login
   5: context.fillStyle = ptrn;
Copy after login
   6: context.fillRect(0,0,400,300);
Copy after login
   7: };
Copy after login

can also achieve tiling (the image is not scaled, so It is the original image size and tiled)

2. Image cropping

   1: // 获取canvas 的ID
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: var canvas = document.getElementById('canvas');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   3: if (canvas == null)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   4: {
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   5: return false;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   6: }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   7: // 获取上下文
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   8: var context = canvas.getContext('2d');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   9: // 获取渐变对象
Copy after login
  10: var g1 = context.createLinearGradient(0,400,300,0);
Copy after login
  11: // 添加渐变颜色
Copy after login
  12: g1.addColorStop(0,'rgb(255,255,0)');
Copy after login
  13: g1.addColorStop(1,'rgb(0,255,255)');
Copy after login
  14: context.fillStyle = g1;
Copy after login
  15: context.fillRect(0,0,400,300);
Copy after login
  16: var image = new Image();
Copy after login
  17: // onload事件实现边绘制边加载
Copy after login
  18: image.onload = function()
Copy after login
  19: {
Copy after login
Copy after login
  20: drawImage(context,image);
Copy after login
  21: };
Copy after login
Copy after login
  22: image.src = 'my.jpg';
Copy after login
  23: function drawImage(context,image)
Copy after login
  24: {
Copy after login
  25: create5StarClip(context);
Copy after login
  26: context.drawImage(image,-50,-150,300,300);
Copy after login
  27: }
Copy after login
  28: function create5StarClip(context)
Copy after login
  29: {
Copy after login
  30: var dx = 100;
Copy after login
  31: var dy = 0;
Copy after login
  32: var s = 150;
Copy after login
  33: // 创建路径
Copy after login
  34: context.beginPath();
Copy after login
  35: context.translate(100,150);
Copy after login
  36: var x = Math.sin(0);
Copy after login
  37: var y = Math.cos(0);
Copy after login
  38: var dig = Math.PI/5 *4;
Copy after login
  39: // context.moveTo(dx,dy);
Copy after login
  40: for (var i = 0; i < 5; i++) {
Copy after login
  41: var x = Math.sin(i * dig);
Copy after login
  42: var y = Math.cos(i * dig);
Copy after login
  43: context.lineTo(dx+x*s,dy+y*s);
Copy after login
  44: }
Copy after login
  45: context.clip();
Copy after login
  46: }
Copy after login

Effect:

3. Pixel processing

   1: // 获取canvas 的ID
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: var canvas = document.getElementById('canvas');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   3: if (canvas == null)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   4: {
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   5: return false;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   6: }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   7: // 获取上下文
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   8: var context = canvas.getContext('2d');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   9: var image = new Image();
Copy after login
  10: image.src = 'my.jpg';
Copy after login
  11: // onload事件实现边绘制边加载
Copy after login
  12: image.onload = function()
Copy after login
  13: {
Copy after login
  14: context.drawImage(image,0,0);
Copy after login
  15: // 获取原图像素
Copy after login
  16: var imageData = context.getImageData(0,0,image.width,image.height);
Copy after login
  17: for (var i = 0,n= imageData.data.length; i <n; i += 4) {
Copy after login
  18: // red
Copy after login
  19: imageData.data[i+0] = 255-imageData.data[i+0];
Copy after login
  20: // green
Copy after login
  21: imageData.data[i+1] = 255-imageData.data[i+2];
Copy after login
  22: // blue
Copy after login
  23: imageData.data[i+2] = 255-imageData.data[i+1];
Copy after login
  24: };
Copy after login
  25: // 将调整后的像素应用到图像
Copy after login
  26: context.putImageData(imageData,0,0);
Copy after login
  27: };
Copy after login

getImageData(sx,sy,sw,sh) : Indicates getting the starting coordinates and height and width of the pixel area, and returning a CanvasPixelArray object with attributes such as width, height, and data. The data attribute stores an array of pixel data, in the shape of [r1, g1, b1, a1, r2, g2, b2, a2...], r1, g1, b1, a1 are the red, green and blue values ​​​​and transparency of the first pixel respectively, and so on.

putImageData(imagedata,dx,dy[,dirtyx,dirtyy,dirtyWidth,dirtyHeight]): Redraw pixel data onto the image. imagedata is a pixel array, dx, dy represent the starting position of redrawing, and the next four parameters give the upper left corner coordinates and height and width of a rectangle.

The pixel operation of Canvas API is only supported by some browsers, and the screenshot effect comes from the new version of Firefox

2. Draw text

   1: // 获取canvas 的ID
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: var canvas = document.getElementById('canvas');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   3: if (canvas == null)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   4: {
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   5: return false;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   6: }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   7: // 获取上下文
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   8: var context = canvas.getContext('2d');
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   9: context.fillStyle = '#00f';
Copy after login
  10: // 设置文字属性
Copy after login
  11: context.font = 'italic 30px sans-serif';
Copy after login
  12: context.textBaseline = 'top';
Copy after login
  13: // 填充字符串
Copy after login
  14: context.fillText('Canvas绘制文字',0,0);
Copy after login
  15: context.font = 'bold 30px sans-serif';
Copy after login
  16: // 轮廓字符串
Copy after login
  17: context.strokeText('改变位置了',50,50);
Copy after login

fillText(string,x,y[,maxwidth]): The first three are not explained. maxwidth represents the maximum width of displayed text, which can prevent text from overflowing

strokeText(string ,x,y[,maxwidth]: Same as above.

Text attribute settings

font: Set font

textAlign: horizontal alignment, the value can be start/end/left /right/center. The default is start

textBaseline: vertical alignment, the value can be top/hanging/middle/alphabetic/ideographic/bottom. The default is alphabetic

Final effect


Next article: 9 JQuery and 5 JavaScript classic interview questions

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template