javascript canvas時(shí)鐘模擬器
canvas時(shí)鐘模擬器,供大家參考,具體內(nèi)容如下
主要功能
能夠顯示當(dāng)前的時(shí)間,也能夠切換夜晚模式和白天模式
主要代碼
h = h > 12 ? h : h - 12 // 下午時(shí)間修正
// 如果畫(huà)布狀態(tài)很混沌的話多使用ctx.restore()恢復(fù)到最初狀態(tài)而不要強(qiáng)行再用同樣的方法矯正狀態(tài),比如使用rotate順時(shí)針旋轉(zhuǎn)n度之后,再使用rotate以同樣的逆時(shí)針角度轉(zhuǎn)回去.
參考代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鐘表模擬器</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<canvas id="demo" width="1000px" height="600px">
您的瀏覽器不支持canvas,請(qǐng)升級(jí)您的瀏覽器
</canvas>
<div class="mode">
Night mode
</div>
<div id="fullscreen"></div>
</body>
<script>
/*
*
* 模擬鐘表
*
* */
window.onload = () => {
// 瀏覽器禁止在你剛剛進(jìn)入一個(gè)頁(yè)面的時(shí)候就變成全屏,這是為了用戶的安全和體驗(yàn)
// let elem = document.querySelector('#fullscreen')
//
// let event = new Event('myEvent')
//
// elem.addEventListener('myEvent', function (e) {
// console.log('ok')
// setTimeout(() => {
// let element = document.documentElement
// if (element.requestFullscreen) {
// element.requestFullscreen()
// } else if (element.msRequestFullscreen) {
// element.msRequestFullscreen()
// } else if (element.mozRequestFullScreen) {
// element.mozRequestFullScreen()
// } else if (element.webkitRequestFullscreen) {
// element.webkitRequestFullscreen()
// }
// }, 1000)
//
// }, false)
//
// elem.dispatchEvent(event)
// 切換夜晚模式和白天模式
let mode = document.getElementsByClassName('mode')
let nightMode = false
mode[0].onclick = () => {
nightMode = !nightMode
document.body.style.backgroundColor = nightMode === false ? '#fff' : '#000'
mode[0].innerHTML = nightMode === false ? 'Night mode' : 'exit'
if (nightMode) {
mode[0].style.color = '#000'
mode[0].style.backgroundColor = '#fff'
} else {
mode[0].style.color = '#fff'
mode[0].style.backgroundColor = '#000'
}
}
// 鼠標(biāo)進(jìn)入變色(可進(jìn)一步簡(jiǎn)潔)
mode[0].onmouseover = () => {
if (nightMode) {
mode[0].style.color = '#000'
mode[0].style.backgroundColor = '#fff'
} else {
mode[0].style.color = '#fff'
mode[0].style.backgroundColor = '#000'
}
}
// 鼠標(biāo)移出變色(可進(jìn)一步簡(jiǎn)潔)
mode[0].onmouseout = () => {
if (nightMode) {
mode[0].style.color = '#fff'
mode[0].style.backgroundColor = '#000'
} else {
mode[0].style.color = '#000'
mode[0].style.backgroundColor = '#fff'
}
}
doHidden()
//
// 在一秒之后把光標(biāo)去掉
function doHidden() {
let time = ''
document.body.onmousemove = () => {
document.body.style.cursor = 'default' // 恢復(fù)普通的光標(biāo)
console.log('ok')
if (time) {
clearTimeout(time)
}
// 一秒后鼠標(biāo)不動(dòng)自動(dòng)使光標(biāo)消失
time = setTimeout(() => {
console.log('ok2')
document.body.style.cursor = nightMode === false ? `url('./imgs/hidden-box2.ani'), default` : `url('./imgs/hidden-box.ani'), default` // 這里的光標(biāo)文件自己定義,最好是透明的空文件,找網(wǎng)上的圖標(biāo)文件轉(zhuǎn)換器轉(zhuǎn)換為ani文件
}, 1000)
}
}
let canvas = document.getElementById('demo')
let ctx = canvas.getContext('2d')
// 為了繪制時(shí)針,把坐標(biāo)軸原點(diǎn)轉(zhuǎn)移到畫(huà)布中心
ctx.translate(500, 300)
// 開(kāi)始正式繪制第一次
drew()
// 持續(xù)更新畫(huà)布
setInterval(() => {
drew()
}, 500)
// 核心方法
function drew() {
// 刷新畫(huà)布
ctx.fillStyle = nightMode === false ? '#fff' : '#000'
ctx.fillRect(-500, -300, 1000, 600)
// 時(shí)鐘的大框框
ctx.save()
ctx.lineWidth = 6
ctx.strokeStyle = '#FFD034'
ctx.lineCap = 'round' // 筆畫(huà)尖端為圓形
ctx.rotate(-90 * Math.PI / 180) // 十二點(diǎn)鐘方向
ctx.beginPath()
ctx.arc(0, 0, 240, 0, 360 * Math.PI / 180)
ctx.stroke()
// 時(shí)針的刻度
ctx.save()
ctx.lineWidth = 10
ctx.strokeStyle = nightMode === true ? '#fff' : '#000'
for (let i = 0; i <= 11; i++) {
ctx.beginPath()
ctx.moveTo(200, 0)
ctx.lineTo(222, 0)
ctx.stroke()
ctx.rotate(30 * Math.PI / 180)
}
ctx.restore()
// 分針的刻度
ctx.save()
ctx.lineWidth = 4
ctx.strokeStyle = '#9B71EA'
for (let i = 0; i < 60; i++) {
if (i % 5 === 0) {
ctx.rotate(6 * Math.PI / 180)
} else {
ctx.beginPath()
ctx.moveTo(205, 0)
ctx.lineTo(222, 0)
ctx.stroke()
ctx.rotate(6 * Math.PI / 180)
}
}
ctx.restore()
// 獲取時(shí)間,正式開(kāi)始繪制
let date = new Date()
let s = date.getSeconds()
let m = date.getMinutes() + s / 60
let h = date.getHours() + m / 60
h = h > 12 ? h : h - 12 // 下午時(shí)間修正
// 畫(huà)時(shí)針
ctx.save()
ctx.lineWidth = 18
ctx.strokeStyle = '#91FF99'
ctx.rotate(30 * h * Math.PI / 180) // 順時(shí)針旋轉(zhuǎn)的
ctx.beginPath()
ctx.moveTo(-20, 0)
ctx.lineTo(100, 0)
ctx.stroke()
ctx.restore()
// 畫(huà)分針
ctx.save()
ctx.lineWidth = 12
ctx.strokeStyle = '#D291FF'
ctx.rotate(6 * m * Math.PI / 180) // 順時(shí)針旋轉(zhuǎn)的
ctx.beginPath()
ctx.moveTo(-35, 0)
ctx.lineTo(138, 0)
ctx.stroke()
ctx.restore()
// 畫(huà)秒針
ctx.save()
ctx.lineWidth = 8
ctx.strokeStyle = '#FF8465'
ctx.rotate(6 * s * Math.PI / 180) // 順時(shí)針旋轉(zhuǎn)的
ctx.beginPath()
ctx.moveTo(-55, 0)
ctx.lineTo(115, 0)
ctx.stroke()
// 給秒針添加花樣
ctx.beginPath()
ctx.arc(130, 0, 15, 0, 360 * Math.PI / 180)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(145, 0)
ctx.lineTo(178, 0)
ctx.stroke()
// 最后給鐘添加最中心的一個(gè)`固定器`
ctx.beginPath()
ctx.arc(0, 0, 15, 0, 360 * Math.PI / 180)
ctx.fillStyle = '#FF8465'
ctx.fill()
ctx.restore()
ctx.restore() // 回到最初最初最初的狀態(tài)(主要是把為了畫(huà)時(shí)針而把畫(huà)布旋轉(zhuǎn)的角度矯正回去)
}
}
</script>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #fff;
text-align: center;
transition: 0.5s;
}
#demo {
margin-top: 140px;
background-color: white;
border-radius: 15px;
}
.mode {
font-family: Consolas, serif;
width: 150px;
margin: 25px auto;
padding: 15px 25px;
border: 2px solid #CCCCCC;
border-radius: 15px;
background-color: white;
user-select: none;
box-shadow: 1px 1px 2px #aaaaaa;
transition: 0.5s;
cursor: pointer;
}
</style>
</html>
顯示效果:
白天模式:

夜晚模式

切換模式

總結(jié):
其實(shí),沒(méi)有什么代碼做不出的效果,只有你想不到的效果。很多復(fù)雜的東西其實(shí),在本質(zhì)上,會(huì)是很多簡(jiǎn)單的東西的一種整合,只要用心去鉆研,一定會(huì)有收獲!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript編寫開(kāi)發(fā)動(dòng)態(tài)時(shí)鐘
- javascript實(shí)現(xiàn)簡(jiǎn)易數(shù)碼時(shí)鐘
- js實(shí)現(xiàn)時(shí)鐘定時(shí)器
- JS實(shí)現(xiàn)網(wǎng)頁(yè)時(shí)鐘特效
- js實(shí)現(xiàn)小時(shí)鐘效果
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)字時(shí)鐘效果
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- 五步輕松實(shí)現(xiàn)JavaScript HTML時(shí)鐘效果
- JavaScript實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘實(shí)例代碼
- js+css3實(shí)現(xiàn)炫酷時(shí)鐘
相關(guān)文章
javascript隨機(jī)將第一個(gè)dom中的圖片添加到第二個(gè)div中示例
此代碼的是一個(gè)簡(jiǎn)單的例子,將第一個(gè)div中的五張圖片中,提取隨機(jī)兩張顯示到第二個(gè)div中,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-10-10
jQuery EasyUI window窗口使用實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了jQuery EasyUI window窗口使用功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
JavaScript中常見(jiàn)的類型判斷方法和區(qū)別詳解
在JavaScript中,我們經(jīng)常需要對(duì)數(shù)據(jù)的類型進(jìn)行判斷,以便進(jìn)行相應(yīng)的處理,本文將介紹JavaScript中常見(jiàn)的類型判斷方法,包括typeof、instanceof、Object.prototype.toString() 以及Array.isArray(),并且會(huì)詳細(xì)解釋它們之間的區(qū)別,需要的朋友可以參考下2023-11-11
TypeScript使用函數(shù)重載確定返回類型的實(shí)現(xiàn)方法
這篇文章主要介紹了TypeScript使用函數(shù)重載確定返回類型的實(shí)現(xiàn)方法,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
js左側(cè)多級(jí)菜單動(dòng)態(tài)的解決方案
實(shí)現(xiàn)的效果很簡(jiǎn)單,就是點(diǎn)一下顯示,再點(diǎn)一下就隱藏,只不過(guò)是多了幾級(jí)的問(wèn)題。好,現(xiàn)在來(lái)說(shuō)說(shuō)我的設(shè)計(jì)思路,首先從第一級(jí)別開(kāi)始,添加如下代碼2010-02-02
微信小程序?qū)崿F(xiàn)滑動(dòng)翻頁(yè)效果(完整代碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)滑動(dòng)翻頁(yè)效果,本文通過(guò)效果圖展示實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

