js实现canvas进度条显示数字效果代码

代码语言:html

所属分类:进度条

代码描述:js实现canvas进度条显示数字效果代码

代码标签: 进度 显示 数字 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BFW NEW PAGE</title>

<script type="text/javascript">
var i = 0;
var res = 0;
var context = null;
var total_width = 300;
var total_height = 34;
var initial_x = 20;
var initial_y = 20;
var radius = total_height/2;
window.onload = function() {
	var elem = document.getElementById('myCanvas');
	if (!elem || !elem.getContext) {
		return;
	}

	context = elem.getContext('2d');
	if (!context) {
		return;
	}

	// set font
	context.font = "16px Verdana";

	// Blue gradient for progress bar
	var progress_lingrad = context.createLinearGradient(0,initial_y+total_height,0,0);
	progress_lingrad.addColorStop(0, '#4DA4F3');
	progress_lingrad.addColorStop(0.4, '#ADD9FF');
	progress_lingrad.addColorStop(1, '#9ED1FF');
	context.fillStyle = progress_lingrad;

	//draw();
	res = setInterval(draw, 30);
}

function draw() {
	i+=1;
	// Clear everything before drawing
	context.clearRect(initial_x-5,initial_y-5,total_width+15,total_height+15);
	progressLayerRect(context, initial_x, initial_y, total_width, total_height, radius);
	progressBarRect(context, initial_x, initial_y, i, total_height, radius, total_width);
	progressText(context, initial_x, initial_y, i, total_height, radius, total_width );
	if (i>=total_width) {
		clearInterval(res);
	}
}

/**
 * Draws a rounded rectangle.
 * @param {CanvasContext} ctx
 * @param {Number} x The top left x coordinate
 * @param {Number} y The top left y coordinate
 * @param {Number} width The width of the rectangle
 * @param {Number} height The height of the rectangle
 * @param {Number} radius The corner radius;
 */
function roundRect(ctx, x, y, width, height, radius) {
	ctx.beginPath();
	ctx.moveTo(x + radius, y);
	ctx.lineTo(x + width - radius, y);
	ctx.arc(x+width-radius, y+radius, radius, -Math.PI/2, Math.PI/2, false);
	ctx.lineTo(x + radius, y + height);
	ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
	ctx.closePath();
	ctx.fill();
}

/**
 * Draws a rounded rectangle.
 * @param {CanvasContext} ctx
 * @param {Number} x The top left x coordinate
 * @param {Number} y The top left y coordinate
 * @param {Number} width The width of the rectangle
 * @param {Number} height The height of the rectangle
 * @param {Number} radius The corner radius;
 */
function roundInsetRect(ctx, x, y, width, height, radius) {
	ctx.beginPath();
	// Draw huge anti-clockwise box
	ctx.moveTo(1000, 1000);
	ctx.lineTo(1000, -1000);
	ctx.lineTo(-1000, -1000);
	ctx.lineTo(-1000, 1000);
	ctx.lineTo(1000, 1000);
	ctx.moveTo(x + radius, y);
	ctx.lineTo(x + width - radius, y);
	ctx.arc(x+width-radius, y+radius, radius, -Math.PI/2, Math.PI/2, false);
	ctx.lineTo(x + radius, y + height);
	ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
	ctx.closePath();
	ctx.fill();
}

function progressLayerRect(ctx, x, y, width, height, radius) {
	ctx.save();
	// Set shadows to make some depth
	ctx.shadowOffsetX = 2;
	ctx.shadowOffsetY = 2;
	ctx.shadowBlur = 5;
	ctx.shadowColor = '#666';

	 // Create initial grey layer
	ctx.fillStyle = 'rgba(189,189,189,1)';
	roundRect(ctx, x, y, width, height, radius);

	// Overlay with gradient
	ctx.shadowColor = 'rgba(0,0,0,0)'
	var lingrad = ctx.createLinearGradient(0,y+h.........完整代码请登录后点击上方下载按钮下载查看

网友评论0