Home > Web Front-end > HTML Tutorial > Getting Started with Canvas (2): Graphic Gradient and Image Shape Transformation_html/css_WEB-ITnose

Getting Started with Canvas (2): Graphic Gradient and Image Shape Transformation_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:56:26
Original
1030 people have browsed it

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

1. Graphic gradient (all tested in the latest version of Google)

1. Draw linear gradient

   1: // 获取canvas 的ID
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
   3:     if (canvas == null)
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
   5:         return false;
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
   7:     // 获取上下文
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
   9:     // 获取渐变对象
Copy after login
Copy after login
  10:     var g1 = context.createLinearGradient(0,0,0,300);
Copy after login
  11:     // 添加渐变颜色
Copy after login
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 g2 = context.createLinearGradient(0,0,300,0);
Copy after login
  17:     g2.addColorStop(0,'rgba(0,0,255,0.5)');
Copy after login
  18:     g2.addColorStop(1,'rgba(255,0,0,0.5)');
Copy after login
  19:     for(var i = 0; i<10;i++)
Copy after login
  20:     {
Copy after login
  21:         context.beginPath();
Copy after login
  22:         context.fillStyle=g2;
Copy after login
  23:         context.arc(i*25, i*25, i*10, 0, Math.PI * 2, true);
Copy after login
  24:         context.closePath();
Copy after login
  25:         context.fill();
Copy after login
  26:     }
Copy after login

createLinearGradient(x1,y1,x2,y2): The parameters represent the horizontal and vertical coordinates of the starting position and end position of the gradient respectively

addColorStop(offset,color): offset represents the set color The offset from the starting position of the gradient. The value range is a floating point value from 0 to 1. The starting offset of the gradient is 0, and the ending offset of the gradient is 1. color is the color of the gradient.

Effect:

2. Draw radial gradient

   1: // 获取canvas 的ID
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
   3:     if (canvas == null)
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
   5:         return false;
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
   7:     // 获取上下文
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
   9:     // 获取渐变对象
Copy after login
Copy after login
  10:     var g1 = context.createRadialGradient(400,0,0,400,0,400);
Copy after login
  11:     // 添加渐变颜色
Copy after login
Copy after login
  12:     g1.addColorStop(0.1,'rgb(255,255,0)');
Copy after login
  13:     g1.addColorStop(0.3,'rgb(255,0,255)');
Copy after login
  14:     g1.addColorStop(1,'rgb(0,255,255)');
Copy after login
  15:     context.fillStyle = g1;
Copy after login
  16:     context.fillRect(0,0,400,300);
Copy after login
  17:     var g2 = context.createRadialGradient(250,250,0,250,250,300);
Copy after login
  18:     g2.addColorStop(1,'rgba(0,0,255,0.5)');
Copy after login
  19:     g2.addColorStop(0.7,'rgba(255,255,0,0.5)')
Copy after login
  20:     g2.addColorStop(0.1,'rgba(255,0,0,0.5)');
Copy after login
  21:     for(var i = 0; i<10;i++)
Copy after login
  22:     {
Copy after login
  23:         context.beginPath();
Copy after login
  24:         context.fillStyle=g2;
Copy after login
  25:         context.arc(i*25, i*25, i*10, 0, Math.PI * 2, true);
Copy after login
  26:         context.closePath();
Copy after login
  27:         context.fill();
Copy after login
  28:     }
Copy after login

createRadialGradient(x1,y1,radius1,x2,y2,radius2):x1,y1,radius1 are respectively The horizontal and vertical coordinates and radius of the center of the circle where the gradient starts. x2, y2, radius2 are the horizontal and vertical coordinates and radius of the center of the gradient end circle respectively.

Effect

2. Graphic transformation

1. Coordinate transformation: translation, scaling and rotation

   1: // 获取canvas 的ID
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
   3:     if (canvas == null)
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
   5:         return false;
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
   7:     // 获取上下文
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
   9:     context.fillStyle = '#eeeeff';
Copy after login
  10:     context.fillRect(0,0,400,300);
Copy after login
  11:     // 平移坐标原点
Copy after login
  12:     context.translate(200,50);
Copy after login
  13:     context.fillStyle = 'rgba(255,0,0,0.25)';
Copy after login
  14:     for(var i = 0; i<50;i++)
Copy after login
  15:     {
Copy after login
  16:         context.translate(25,25);
Copy after login
  17:         // 图形缩放
Copy after login
  18:         context.scale(0.95,0.95);
Copy after login
  19:         // 图形旋转
Copy after login
  20:         context.rotate(Math.PI / 10);
Copy after login
  21:         context.fillRect(0,0,100,50);
Copy after login
  22:     }
Copy after login

translate(x,y): Translate the origin, x,y represents how many units to move left and down. The default unit is pixels

scale(x,y): scaling, x,y represents the horizontal and vertical scaling size. If less than 1, it will be reduced, and if it is greater than 1, it will be enlarged.

rotate(angle): Rotation, angle is the rotation angle, the unit is radians. Greater than 0 means clockwise rotation, otherwise counterclockwise.

Effect:

2. Matrix transformation

   1: // 获取canvas 的ID
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
   3:     if (canvas == null)
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
   5:         return false;
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
   7:     // 获取上下文
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
   9:     // 定义颜色
Copy after login
  10:     var colors = ['red','orange','yellow','green','blue','navy','purple'];
Copy after login
  11:     // 定义线宽
Copy after login
  12:     context.lineWidth = 10;
Copy after login
  13:     // 矩阵变换
Copy after login
  14:     context.transform(1,0,0,1,100,0);
Copy after login
  15:     // 循环绘制圆弧
Copy after login
  16:     for (var i = 0; i < colors.length; i++)
Copy after login

17: { //Move the origin down 10 px each time

  18:          context.transform(1,0,0,1,0,10);
Copy after login
  19:         context.strokeStyle = colors[i];
Copy after login
  20:         context.beginPath();
Copy after login
  21:         context.arc(50,100,100,0,Math.PI,true);
Copy after login
  22:         context.stroke();
Copy after login
  23:     }
Copy after login

transform(m11,m12,m21,m22,dx,dy): Change the method to use a new transformation matrix Multiply with the current transformation matrix. dx, dy represent the units for moving the origin coordinates to the left and down, and the default is pixels.

The format of the transformation matrix is ​​as follows

m11 m12 dx

m21 m22 dy

0 0 1

Final effect:

Summary: All coordinate transformation methods can be replaced by transform(). The rules are as follows:

1. translate(x,y) <=> transform(1,0 ,0,1,dx,dy) or transform(0,1,1,0,dx,dy), the first four parameters indicate that the graphics will not be scaled.

2. scale(x,y) <=> transform(x,0,0,y,0,0) or transform(0,y,x,0,0,0), followed by Two parameters indicate no translation.

3. rotate(angle) <=> transform(Math.cos(angle*Math.PI/180),Math.sin(angle*Math.PI/180),- Math.sin( angle*Math.PI/180),Math.cos(angle*Math.PI/180),0,0) or

transform(-Math.sin(angle*Math.PI/180),Math .cos(angle*Math.PI/180),Math.cos(angle*Math.PI/180),Math.sin(angle*Math.PI/180),0,0)

Next article: Getting started with Canvas (3): Image processing and drawing text

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