//hml部分
<canvas ref="canvas" style="width: 320px; height: 320px; background-color: #000;"></canvas>
//以下为JS代码
const el = this.$refs.canvas;
const ctx = el.getContext('2d',{ antialias: true });
//绘制的准备工作--画布的中心点
const canvasX = JSON.stringify(el.style.width).replace(/[^0-9]/ig,"");
const canvasY = JSON.stringify(el.style.height).replace(/[^0-9]/ig,"");
const date = new Date();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
const hours = hour > 15 ? hour - 16 : hour + 8;
const hh = hours > 12 ? hours - 12 : hours;
ctx.lineWidth = 6;
ctx.strokeStyle = '#FFF';
ctx.beginPath();
// x,y,r,0~360,true/false:正反向画圆
ctx.arc(Number(canvasX/2), Number(canvasY/2), Number(canvasX/2 - 20), Number(canvasX/2 - 110), 0, true);
ctx.closePath();
ctx.stroke();
for (var i = 0; i < 60; i++) {
ctx.save();
ctx.lineWidth = 2;
if(i < this.second){
ctx.strokeStyle = 'rgba(6,235,0,0.5)';
}else if(i == this.second){
ctx.strokeStyle = '#06EB00';
}else{
ctx.strokeStyle = '#999';
}
ctx.translate(Number(canvasX/2), Number(canvasY/2))
ctx.rotate(i * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, Number(42 - canvasX/2));
ctx.lineTo(0, Number(30 - canvasX/2));
ctx.closePath();
ctx.stroke();
ctx.restore();
}
for (var index = 0; index < 12; index++) {
ctx.save();
ctx.lineWidth = 4;
if((index * 5) == this.second){
ctx.strokeStyle = '#06EB00';
}else{
ctx.strokeStyle = '#FFF';
}
ctx.translate(Number(canvasX / 2), Number(canvasY / 2));
ctx.rotate((index+30) * 30 * Math.PI / 180) //角度*3.14/180=弧度
ctx.beginPath();
ctx.moveTo(0, Number(canvasX / 2 - 50));
ctx.lineTo(0, Number(canvasY / 2 - 30));
ctx.closePath();
ctx.stroke();
ctx.restore();
}
ctx.save();
ctx.lineWidth = 6;
ctx.strokeStyle = "#FFF";
ctx.translate(Number(canvasX/2), Number(canvasY/2));
//如果时间不是整点,需要采用 h*6 + m*360 + s* 21600 ,来计算时针的偏移量
ctx.rotate(this.hour * (Math.PI / 6) + this.minute * (Math.PI / 360) + this.second * (Math.PI / 21600));
ctx.beginPath();
ctx.moveTo(0, Number(80 - canvasX/2));
ctx.lineTo(0, Number(canvasX/2 - 145));
ctx.closePath();
ctx.stroke();
ctx.restore();
ctx.save();
ctx.lineWidth = 4;
ctx.strokeStyle = "#FFF";
ctx.translate(Number(canvasX/2), Number(canvasY/2));
ctx.rotate(this.minute * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, Number(60 - canvasX/2));
ctx.lineTo(0, Number(canvasX/2 - 140));
ctx.closePath();
ctx.stroke();
ctx.restore();
ctx.save();
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
ctx.fillStyle = "red"
ctx.translate(Number(canvasX/2), Number(canvasY/2));
ctx.rotate(this.second * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, Number(62 - canvasX/2));
ctx.lineTo(0, Number(canvasX/2 - 130));
ctx.closePath();
ctx.stroke();
//绘制中心红点
ctx.beginPath();
ctx.arc(0,0,4,0,360 * Math.PI / 180)
ctx.fill();
//绘制秒针头部的圆圈
ctx.beginPath();
ctx.arc(0,Number(58 - canvasX/2),4,0,360 * Math.PI / 180);
ctx.stroke();
ctx.restore();
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |