html5在线考试倒计时,html5 canvas 实现倒计时 功能

function showTime(a) {

var b = {

id: "showtime", //canvasid

x: 60, //中心点坐标 X轴;

y: 60, //中心点坐标 Y轴;

radius: 60, //圆的半径

angle: 0, //角度 无需设置

linewidth: 6, //线的宽度

backround: "#d65554", //倒计时背景色

color: "#e4e4e4", //填充色

day: 0,

time: 0,

minute: 0,

seconds: 0

}

//若参数有更新则合并

if (a) {

b = $.extend(b, a);

}

this.total = 0;

this.id = b.id;

this.x = b.x;

this.y = b.y;

this.radius = b.radius;

this.angle = b.angle;

this.linewidth = b.linewidth;

this.backround = b.backround;

this.color = b.color;

this.time = b.time;

this.day = b.day;

this.minute = b.minute;

this.seconds = b.seconds;

var canvas = document.getElementById(this.id);

if (canvas == null) {

return false;

}

var ctx = canvas.getContext("2d");

this.creatText = function() {

ctx.fillStyle = "#000";

ctx.font = "13px Arial";

ctx.fillText("剩余时间", 32, 38);

ctx.fillStyle = "#333";

if (Number(this.day) > 0) {

ctx.font = "22px Arial";

ctx.fillStyle = "#000";

ctx.fillText(getStr(this.day), 13, 75);

ctx.font = "13px Arial";

ctx.fillStyle = "#333";

ctx.fillText("天", 38, 75);

ctx.font = "22px Arial";

ctx.fillStyle = "#000";

ctx.fillText(getStr(this.time), 58, 75);

ctx.font = "14px Arial";

ctx.fillStyle = "#333";

ctx.fillText("小时", 82, 75);

} else if (Number(this.time) > 0) {

ctx.font = "22px Arial";

ctx.fillStyle = "#000";

ctx.fillText(getStr(this.time), 11, 75);

ctx.font = "13px Arial";

ctx.fillStyle = "#333";

ctx.fillText("小时", 33, 75);

ctx.font = "22px Arial";

ctx.fillStyle = "#000";

ctx.fillText(getStr(this.minute), 61, 7

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 canvas 实现三秒钟倒计时的示例代码: HTML: ``` <canvas id="countdown" width="200" height="200"></canvas> ``` JavaScript: ``` var canvas = document.getElementById("countdown"); var context = canvas.getContext("2d"); var count = 3; // 倒计时秒数 var angle = 0; // 扇形角度 var radius = 80; // 扇形半径 var centerX = canvas.width / 2; // 心点 X 坐标 var centerY = canvas.height / 2; // 心点 Y 坐标 var font = "60px Arial"; // 字体 // 开始倒计时 var countdownInterval = setInterval(function() { angle = (360 / count) * Math.PI / 180; // 计算扇形角度 context.clearRect(0, 0, canvas.width, canvas.height); // 清除画布 context.beginPath(); context.moveTo(centerX, centerY); // 从心点开始绘制扇形 context.arc(centerX, centerY, radius, 0, angle, false); // 绘制扇形 context.closePath(); context.fillStyle = "#FF0000"; // 设置扇形颜色 context.fill(); // 填充扇形 context.font = font; // 设置字体 context.fillStyle = "#000000"; // 设置字体颜色 context.fillText(count, centerX - 30, centerY + 20); // 绘制倒计时数字 count--; // 倒计时减1 if (count < 0) { // 倒计时结束 clearInterval(countdownInterval); // 停止倒计时 context.clearRect(0, 0, canvas.width, canvas.height); // 清除画布 context.font = font; // 设置字体 context.fillStyle = "#000000"; // 设置字体颜色 context.fillText("GO!", centerX - 50, centerY + 20); // 绘制 GO! } }, 1000); // 每隔1秒执行一次 ``` 以上代码会在画布绘制一个红色的扇形和倒计时数字,每隔1秒更新一次数字和扇形角度,直到倒计时结束后绘制 "GO!" 字符串。您可以根据自己的需要修改颜色、字体、大小和倒计时秒数等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值