Effect of the four gradient playback buttons drawn using javascript and HTML5 Canvas

Source: Internet
Author: User

Comments: This article mainly introduces the effect of the four gradient playback buttons drawn by using javascript and HTML5 Canvas. For more information, see

<Canvas> </canvas> is a new tag in html5. Like all dom objects, it has its own attributes, methods, and events. There is a plotting method, js can call it for plotting. In this article, the canvas label and Javascript are used together to draw a playback button with a four-color gradient effect ,:

Implementation Code:


The Code is as follows: <! Doctype html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk">
<Title> draw button </title>
</Head>
<Body>
<Canvas id = "myCanvas" width = "600" height = "400"> your browser does not support Canvas. Please upgrade your browser! </Canvas>
<Script type = "text/javascript">

Var canvas = document. getElementById ('mycanvas ');/* Get the defined canvas */
Var ctx = canvas. getContext ('2d ');/* painting in a two-dimensional environment */
DrawPlayButton (ctx, 200,200 );
DrawPlayButton (ctx, 400,200 );
DrawPlayButton (ctx, 300,200 );
Function drawPlayButton (_ context, x, y ){
Var nRadius = 30; // radius
_ Context. save ();
_ Context. translate (x, y );
// Constructor
Var yellowGrad = _ context. createLinearGradient );
YellowGrad. addColorStop (0, '# fccb02 ');
YellowGrad. addColorStop (0.5, '# fbf14d ');
YellowGrad. addColorStop (1, '# ffcb02 ');
Var blueGrad = _ context. createLinearGradient (30, 0, 0, 30 );
BlueGrad. addColorStop (0, '# 2a459c ');
BlueGrad. addColorStop (0.5, '# 0e7ads ');
BlueGrad. addColorStop (1, '# 2a459e ');
Var redGrad = _ context. createLinearGradient (,); // rotate by rotate
RedGrad. addColorStop (0, '# d0372f ');
RedGrad. addColorStop (0.5, '# e0675e ');
RedGrad. addColorStop (1, '# ce392d ');
Var greenGrad = _ context. createLinearGradient (30,0,); // rotate by rotate
GreenGrad. addColorStop (0, '#059700 ');
GreenGrad. addColorStop (0.5, '#02e003 ');
GreenGrad. addColorStop (1, '#019a02 ');

// Draw the angle between two arcs
DrawCake (_ context, 0, yellowGrad, nRadius );
DrawCake (_ context, Math. PI/2, blueGrad, nRadius );
DrawCake (_ context, Math. PI, redGrad, nRadius );
DrawCake (_ context, 3 * Math. PI/2, greenGrad, nRadius );
// Incircle color
Var lingrad = _ context. createLinearGradient (-30,-30,30, 30 );
Lingrad. addColorStop (0, '# 4672df ');
Lingrad. addColorStop (0.2, '#6188e5 ');
Lingrad. addColorStop (0.5, '# 98b1ef ');
Lingrad. addColorStop (0.8, '# b1c3f2 ');
Lingrad. addColorStop (1, '# eaedfc ');
_ Context. save ();
_ Context. beginPath (); // incircle
_ Context. fillStyle = lingrad;
_ Context. arc (, nRadius-10, 0, Math. PI * 2, true );
_ Context. fill ();
_ Context. closePath ();
_ Context. restore ();
// Draw a triangle
Var trianglerad = _ context. createLinearGradient (-6,-10,-6, 10 );
Trianglerad. addColorStop (0, '# 99d4ea ');
Trianglerad. addColorStop (0.2, '# 5e8fdd ');
Trianglerad. addColorStop (0.5, '#0f17a1 ');
Trianglerad. addColorStop (0.8, '# 4c65cc ');
Trianglerad. addColorStop (1, '#7299e5 ');
_ Context. beginPath ();
_ Context. fillStyle = trianglerad;
_ Context. moveTo (12, 0 );
_ Context. lineTo (-6, 10 );
_ Context. lineTo (-6,-10 );
_ Context. fill ();
_ Context. restore ();
}
// Draw a slice
Function drawCake (_ context, _ nRotateAngle, _ fillColor, _ nRadius ){
_ Context. save ();
_ Context. beginPath ();
_ Context. fillStyle = _ fillColor;
_ Context. rotate (_ nRotateAngle );
_ Context. moveTo (_ nRadius-10, 0 );
_ Context. lineTo (_ nRadius, 0); // draw a line to the right
_ Context. arc (0, 0, _ nRadius, 0, Math. PI/2, false );
_ Context. lineTo (0, _ nRadius-10); // draw
_ Context. arc (, _ nRadius-10, Math. PI/, true); // draw the Inner arc counterclockwise
_ Context. fill ();
_ Context. closePath ();
_ Context. restore ();
}
</Script>
</Body>
</Html>


Related Article

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.