Four gradient play button effect drawn with JavaScript and HTML5 canvas

Time:2022-6-4

<canvas></canvas> is a new tag in HTML5. Like all DOM objects, it has its own properties, methods and events. Among them, there is a drawing method. JS can call it to draw. This paper uses the canvas tag and JavaScript to draw a four-color gradually changing play button effect. The effect picture is as follows:
Four gradient play button effect drawn with JavaScript and HTML5 canvas

Implementation code:

Copy code

The codes are as follows:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gbk”>
<title> draw button </title>
</head>
<body>
<canvas 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 defined canvas*/
var ctx = canvas. getContext(‘2d’);/* Painting in a 2D 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);
//Structural line deformation
var yellowGrad=_context.createLinearGradient(30,0,0,30);
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, ‘#0e7adc’);
blueGrad.addColorStop(1, ‘#2a459e’);
var redGrad=_ context. createLinearGradient(30,0,0,30);// Rotate through rotate
redGrad.addColorStop(0, ‘#d0372f’);
redGrad.addColorStop(0.5, ‘#e0675e’);
redGrad.addColorStop(1, ‘#ce392d’);
var greenGrad=_ context. createLinearGradient(30,0,0,30);// Rotate through 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);
//Inner circle 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();// Inner circle
_context.fillStyle=lingrad;
_context.arc(0,0,nRadius-10,0,Math.PI*2,true);
_context.fill();
_context.closePath();
_context.restore();
//Draw 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 sector
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 one up
_ context. arc(0,0,_nRadius-10,Math.PI/2,0,true); // Draw an inner arc counterclockwise
_context.fill();
_context.closePath();
_context.restore();
}
</script>
</body>
</html>