HTML
<!DOCTYPE html> <html> <head> <title>canvas</title> </head> <body> <image src="bd_logo1.png" width='50' height='50' /> <canvas id='drawing' width="800" height="800" style="border:1px solid #c3c3c3;">A drawing of something.</canvas> <canvas id="drawing2" width="800" height="300"></canvas> <script type="text/javascript" src="canvas.js" ></script> </body> </html>
JS
//15.1 basic usage var = document.getElementById Drawing ( 'Drawing'); // IF (drawing.getContext) { // var context = drawing.getContext ( '2D'); // var imgURI = drawing.toDataURL ( ' Image / PNG '); // Image var = document.createElement (' IMG '); //'s image.src = imgURI; // document.body.appendChild (Image); //} //15.2 2D context //15.2 .1 fills and strokes // determine browser supports <canvas> element IF (drawing.getContext) { var = drawing.getContext context ( "2D"); context.strokeStyle = 'Red'; context.fillStyle = '# 0000FF '; context.stroke (); } //15.2.2 draw the rectangle if (drawing.getContext) { drawing.getContext context = var ( '2D'); / * * The document Mozilla * HTTP: //developer.mozilla.org/en/docs/Cavans_tutorial: Basic_usage * / // Draw a red rectangle context.strokeStyle = '# FF0000 '; context.strokeRect (10, 10, 50, 50); // draw a translucent blue rectangle context.strokeStyle =' RGBA (0,0,255,0.5) '; context.strokeRect (30, 30, 50, 50); // Clear small rectangle where two small rectangles overlap context.clearRect (40, 40, 10, 10); } //15.2.3 drawing a path // arc (x, y, radius , startAngle, endAngle , counterclocwise) // in (x, y) to draw an arc of a circle, radius of curvature radius of the start and end angle (in radians) are startAngle and endAngle. // denotes startAngle counterclockwise and counterclockwise whether endAngle calculating a value of false for clockwise // arcTo (x1, y1, x2 , y2, radius) // drawn from an arc start point to (x2, y2) so far, and through a given radius radius (X1, Y1) // the working bezierCurveTo ( c1x, c1y, c2x, c2y, x, y) // draw a start point from the curve, the (x, y) so far, and in (c1x, c1y) and (c2x, c2y) control point // lineTo ( x, y) from the start point to draw a straight line, the (x, y) so far. // moveTo (x, y) to move the cursor to the drawing (x, y), does not draw a line // quedraticCurveTo (cx, cy, x , y) from the start point to draw a quadratic curve, the (x, y) So far, and in (cx, cy) as control points // rect (x, y, width , height) from the point (x, y) starts to draw a rectangle, the width and height are the width and height specified, IF (drawing. the getContext) { var = drawing.getContext context ( "2D"); // start path context.beginPath (); // draw outer context.arc (100, 100, 99, 0, Math.PI * 2, false) ; // draw the circle context.moveTo (194,100); context.arc (100,100,94,0,2 * Math.PI, to false); // draw minute context.moveTo (100,100); context.lineTo (100, 15); // draw hour context.moveTo (100,100); context.lineTo (35,100); // stroke path context.strokeStyle = '# FF0000'; context. Stroke (); } //15.2.4 draw text IF (drawing.getContext) { var = drawing.getContext context ( "2D"); context.font = 'Arial Bold 14px'; context.textAlign = 'Center'; context = .textBaseLine 'Middle'; context.fillText ( '12 is', 100,20); // start point aligned context.textAlign = 'Start'; context.fillText ( '12 is', 100,40); // end aligned with context .textAlign = 'end'; context .fillText ( '12', 100,60) ; } //15.2.5 transform // rotate (angle) image rotation angle in radians about the origin scaled image // scale (scaleX, scaleY), multiplied by sacelX in the x direction, the y direction by multiplying scaleY, sacelX, scaleY default value is 1.0 // Translate (x, y) to move to the origin of the coordinate (X, y), just after the transformation, the coordinates before becomes a (X, y) point corresponds to translational transform // transform (m1_1, m1_m2, m2_1.m2_2 , dx, dy ) directly modify the transformation matrix multiplied with the following matrix // m1_1 m1_2 DX // M2_1 M2_2 Dy // 0 0. 1; // The above is a rotary + translation matrix, in fact, is to rotate at the origin, then the above-described translation dx dy is a 3 * 3 matrix // Y1 siNA DX @ X1 Cosa Dy @ 0 0. 1 this is probably somewhat forgotten matrix // assumed that the x-axis x1: 100, y1: 0 rotated clockwise by 90 ° trigonometric seek X2, Y2 // = the Math.sqrt X2 (X1 * Y1 + 2 * 2) * Math.sqrt Cosa Y2 = (X1 + Y1 * 2 * 2) * siNA // rotation 90 just x2 = 0 y2 = 100 // translation transform using the drawing if (drawing.getContext) { drawing.getContext context = var ( "2D"); context.translate (200,200); // use painted before the first translational avoiding, to distinguish. Translation is superimposed. context.moveTo (0,0); // start path context.beginPath (); // Painter cylindrical context.arc (100,100,99,0,2 * Math.PI, false) ; // location of the center is actually 200 + 100 within // Draw a circle context .moveTo (194,100); // brush starting point for translational direction x 6 units context.arc (100,100,94,0,2 * Math.PI, false) ; // center unchanged, drawing concentric circles, radius and narrow brush 6 position coincide. // conversion origin context.translate (100,100); // center position becomes 200 + 100 + 100 = 400 // rotating hands context.rotate (1); // rotation of 90 degrees, behind the painting coordinate unfinished normal after rotating 90 // draw minute context.moveTo (0,0); // here lineTo (0,0) actually go to the absolute origin of 300, 300 context.lineTo (0, -85); // draw hour context.lineTo (-65,0); // stroke path context.stroke (); //context.restore (); then save the results and return disposed forward } //15.2.6 drawn image var Image = the document.images [0]; //context.drawImage(image,10,10); // draw a picture to which actual position is then rotated by 90 and 310, 310 because of the above rotated 90 context.drawImage (image, 10,10,50,50); // specified WH //context.drawImage () source image up to nine parameters to be rendered, the source image coordinates x, y coordinates of the source image, the source image width, the height of the source image, the target image of a target image x y, the width of the target image, the target image height //15.2.7 shadow var drawing2 = document.getElementById ( 'drawing2'); var = context2 drawing2.getContext ( '2D') ; // set the shadow context2.shadowOffsetX =. 5; context2.shadowOffsetY =. 5; context2.shadowBlur =. 4; context2.shadowColor = 'RGBA (0,0,0,0.5)'; // draw a blue rectangle context2.fillStyle = 'rgba (0,0,255,1)' ; // draw a red rectangle = context2.fillStyle '# FF0000'; context2.fillRect (10,10,50,50); // Image.width=50; context2.fillRect (30,30,50,50); //15.2.8 gradient var gradient1 = context2.createLinearGradient (50,50,70,70); gradient1.addColorStop (0, 'White'); gradient1.addColorStop ( . 1, 'Black'); var = gradient2 context2.createRadialGradient (125,125,10,125,125,30); // gradient concentrically outwardly gradient2.addColorStop (0, 'White'); gradient2.addColorStop (. 1, 'Black'); // draw a rectangle gradient context2.fillStyle = gradient1; context2.fillRect (50,50,50,50); context2.fillStyle = gradient2; context2.fillRect (100,100,50,50); //context2.stroke (); / /15.2.9 mode var Image = the document.images [0]; // image.height = 50; context2.createPattern pattern = var (Image, 'REPEAT'); // REPEAT REPEAT REPEAT-X-Y-NO REPEAT context2.fillStyle = pattern; context2.fillRect (200,200,150,150); //15.2.10 using image data var imageData = context2.getImageData (100,100,50,50); // coordinate position acquired region width and length of the data var = data imageData.data; var = Red data [0], data = Green [. 1], data = Blue [2 ], Alpha = data [. 3]; data [0] = 255; // set the image data imageData.data = data; context2.putImageData (the imageData, 0,0); // write the image data to the position //15.2.11 synthesis context2.globaAlpha = 0.5;