iceshrimp-legacy/src/web/app/desktop/tags/analog-clock.tag

106 lines
2.6 KiB
Plaintext
Raw Normal View History

2017-01-11 21:55:38 +01:00
<mk-analog-clock>
<canvas ref="canvas" width="256" height="256"></canvas>
2017-02-19 04:31:53 +01:00
<style>
2017-01-11 21:55:38 +01:00
:scope
> canvas
display block
width 256px
height 256px
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
</style>
<script>
2017-02-20 01:53:57 +01:00
this.on('mount', () => {
2017-01-11 21:55:38 +01:00
@draw!
2017-02-20 01:53:57 +01:00
this.clock = set-interval @draw, 1000ms
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
this.on('unmount', () => {
2017-01-11 21:55:38 +01:00
clear-interval @clock
2016-12-28 23:49:51 +01:00
2017-02-20 02:34:57 +01:00
this.draw = () => {
2017-01-11 21:55:38 +01:00
now = new Date!
s = now.get-seconds!
m = now.get-minutes!
h = now.get-hours!
2016-12-28 23:49:51 +01:00
2017-01-11 21:55:38 +01:00
vec2 = (x, y) ->
2017-02-20 01:53:57 +01:00
this.x = x
this.y = y
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
ctx = this.refs.canvas.get-context '2d'
canv-w = this.refs.canvas.width
canv-h = this.refs.canvas.height
2017-01-11 21:55:38 +01:00
ctx.clear-rect 0, 0, canv-w, canv-h
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// 背景
2017-01-11 21:55:38 +01:00
center = (Math.min (canv-w / 2), (canv-h / 2))
line-start = center * 0.90
line-end-short = center * 0.87
line-end-long = center * 0.84
for i from 0 to 59 by 1
angle = Math.PI * i / 30
uv = new vec2 (Math.sin angle), (-Math.cos angle)
ctx.begin-path!
ctx.line-width = 1
ctx.move-to do
(canv-w / 2) + uv.x * line-start
(canv-h / 2) + uv.y * line-start
if i % 5 == 0
ctx.stroke-style = 'rgba(255, 255, 255, 0.2)'
ctx.line-to do
(canv-w / 2) + uv.x * line-end-long
(canv-h / 2) + uv.y * line-end-long
else
ctx.stroke-style = 'rgba(255, 255, 255, 0.1)'
ctx.line-to do
(canv-w / 2) + uv.x * line-end-short
(canv-h / 2) + uv.y * line-end-short
ctx.stroke!
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// 分
2017-01-11 21:55:38 +01:00
angle = Math.PI * (m + s / 60) / 30
length = (Math.min canv-w, canv-h) / 2.6
2016-12-28 23:49:51 +01:00
uv = new vec2 (Math.sin angle), (-Math.cos angle)
ctx.begin-path!
2017-02-20 01:53:57 +01:00
ctx.stroke-style = '#ffffff'
2017-01-11 21:55:38 +01:00
ctx.line-width = 2
2016-12-28 23:49:51 +01:00
ctx.move-to do
2017-01-11 21:55:38 +01:00
(canv-w / 2) - uv.x * length / 5
(canv-h / 2) - uv.y * length / 5
ctx.line-to do
(canv-w / 2) + uv.x * length
(canv-h / 2) + uv.y * length
2016-12-28 23:49:51 +01:00
ctx.stroke!
2017-02-20 01:53:57 +01:00
// 時
2017-01-11 21:55:38 +01:00
angle = Math.PI * (h % 12 + m / 60) / 6
length = (Math.min canv-w, canv-h) / 4
uv = new vec2 (Math.sin angle), (-Math.cos angle)
ctx.begin-path!
2017-02-20 01:53:57 +01:00
#ctx.stroke-style = '#ffffff'
2017-01-11 21:55:38 +01:00
ctx.stroke-style = CONFIG.theme-color
ctx.line-width = 2
ctx.move-to do
(canv-w / 2) - uv.x * length / 5
(canv-h / 2) - uv.y * length / 5
ctx.line-to do
(canv-w / 2) + uv.x * length
(canv-h / 2) + uv.y * length
ctx.stroke!
2016-12-28 23:49:51 +01:00
2017-02-20 01:53:57 +01:00
// 秒
2017-01-11 21:55:38 +01:00
angle = Math.PI * s / 30
length = (Math.min canv-w, canv-h) / 2.6
uv = new vec2 (Math.sin angle), (-Math.cos angle)
ctx.begin-path!
ctx.stroke-style = 'rgba(255, 255, 255, 0.5)'
ctx.line-width = 1
ctx.move-to do
(canv-w / 2) - uv.x * length / 5
(canv-h / 2) - uv.y * length / 5
ctx.line-to do
(canv-w / 2) + uv.x * length
(canv-h / 2) + uv.y * length
ctx.stroke!
</script>
</mk-analog-clock>