2023-07-27 08:31:52 +03:00
|
|
|
<!--
|
2024-02-13 17:59:27 +02:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 08:31:52 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-02-14 16:31:24 +02:00
|
|
|
<template>
|
2023-01-14 04:46:22 +02:00
|
|
|
<svg :class="$style.root" viewBox="0 0 10 10" preserveAspectRatio="none">
|
2022-08-06 08:02:03 +03:00
|
|
|
<template v-if="props.graduations === 'dots'">
|
|
|
|
<circle
|
|
|
|
v-for="(angle, i) in graduationsMajor"
|
|
|
|
:cx="5 + (Math.sin(angle) * (5 - graduationsPadding))"
|
|
|
|
:cy="5 - (Math.cos(angle) * (5 - graduationsPadding))"
|
|
|
|
:r="0.125"
|
2022-08-06 10:39:09 +03:00
|
|
|
:fill="(props.twentyfour ? h : h % 12) === i ? nowColor : majorGraduationColor"
|
|
|
|
:opacity="!props.fadeGraduations || (props.twentyfour ? h : h % 12) === i ? 1 : Math.max(0, 1 - (angleDiff(hAngle, angle) / Math.PI) - numbersOpacityFactor)"
|
2022-08-06 08:02:03 +03:00
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
<template v-else-if="props.graduations === 'numbers'">
|
|
|
|
<text
|
|
|
|
v-for="(angle, i) in texts"
|
|
|
|
:x="5 + (Math.sin(angle) * (5 - textsPadding))"
|
|
|
|
:y="5 - (Math.cos(angle) * (5 - textsPadding))"
|
|
|
|
text-anchor="middle"
|
|
|
|
dominant-baseline="middle"
|
|
|
|
:font-size="(props.twentyfour ? h : h % 12) === i ? 1 : 0.7"
|
|
|
|
:font-weight="(props.twentyfour ? h : h % 12) === i ? 'bold' : 'normal'"
|
2022-08-06 10:39:09 +03:00
|
|
|
:fill="(props.twentyfour ? h : h % 12) === i ? nowColor : 'currentColor'"
|
|
|
|
:opacity="!props.fadeGraduations || (props.twentyfour ? h : h % 12) === i ? 1 : Math.max(0, 1 - (angleDiff(hAngle, angle) / Math.PI) - numbersOpacityFactor)"
|
2022-08-06 08:02:03 +03:00
|
|
|
>
|
|
|
|
{{ i === 0 ? (props.twentyfour ? '24' : '12') : i }}
|
|
|
|
</text>
|
|
|
|
</template>
|
2022-08-05 17:51:15 +03:00
|
|
|
|
2022-08-26 18:40:20 +03:00
|
|
|
<!--
|
2020-02-14 16:31:24 +02:00
|
|
|
<line
|
|
|
|
:x1="5 - (Math.sin(sAngle) * (sHandLengthRatio * handsTailLength))"
|
|
|
|
:y1="5 + (Math.cos(sAngle) * (sHandLengthRatio * handsTailLength))"
|
|
|
|
:x2="5 + (Math.sin(sAngle) * ((sHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:y2="5 - (Math.cos(sAngle) * ((sHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:stroke="sHandColor"
|
2021-07-19 17:31:27 +03:00
|
|
|
:stroke-width="thickness / 2"
|
2021-07-19 17:48:22 +03:00
|
|
|
stroke-linecap="round"
|
2021-07-19 17:31:27 +03:00
|
|
|
/>
|
2022-08-26 18:40:20 +03:00
|
|
|
-->
|
|
|
|
|
|
|
|
<line
|
2023-05-19 21:38:07 +03:00
|
|
|
ref="sLine"
|
2023-01-14 04:46:22 +02:00
|
|
|
:class="[$style.s, { [$style.animate]: !disableSAnimate && sAnimation !== 'none', [$style.elastic]: sAnimation === 'elastic', [$style.easeOut]: sAnimation === 'easeOut' }]"
|
2022-08-26 18:40:20 +03:00
|
|
|
:x1="5 - (0 * (sHandLengthRatio * handsTailLength))"
|
|
|
|
:y1="5 + (1 * (sHandLengthRatio * handsTailLength))"
|
|
|
|
:x2="5 + (0 * ((sHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:y2="5 - (1 * ((sHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:stroke="sHandColor"
|
|
|
|
:stroke-width="thickness / 2"
|
|
|
|
:style="`transform: rotateZ(${sAngle}rad)`"
|
|
|
|
stroke-linecap="round"
|
|
|
|
/>
|
2020-02-14 16:31:24 +02:00
|
|
|
|
|
|
|
<line
|
|
|
|
:x1="5 - (Math.sin(mAngle) * (mHandLengthRatio * handsTailLength))"
|
|
|
|
:y1="5 + (Math.cos(mAngle) * (mHandLengthRatio * handsTailLength))"
|
|
|
|
:x2="5 + (Math.sin(mAngle) * ((mHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:y2="5 - (Math.cos(mAngle) * ((mHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:stroke="mHandColor"
|
2021-07-19 17:31:27 +03:00
|
|
|
:stroke-width="thickness"
|
2021-07-19 17:48:22 +03:00
|
|
|
stroke-linecap="round"
|
2021-07-19 17:31:27 +03:00
|
|
|
/>
|
2020-02-14 16:31:24 +02:00
|
|
|
|
|
|
|
<line
|
|
|
|
:x1="5 - (Math.sin(hAngle) * (hHandLengthRatio * handsTailLength))"
|
|
|
|
:y1="5 + (Math.cos(hAngle) * (hHandLengthRatio * handsTailLength))"
|
|
|
|
:x2="5 + (Math.sin(hAngle) * ((hHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:y2="5 - (Math.cos(hAngle) * ((hHandLengthRatio * 5) - handsPadding))"
|
|
|
|
:stroke="hHandColor"
|
2021-07-19 17:31:27 +03:00
|
|
|
:stroke-width="thickness"
|
2021-07-19 17:48:22 +03:00
|
|
|
stroke-linecap="round"
|
2021-07-19 17:31:27 +03:00
|
|
|
/>
|
2020-02-14 16:31:24 +02:00
|
|
|
</svg>
|
|
|
|
</template>
|
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
<script lang="ts" setup>
|
2023-05-19 21:38:07 +03:00
|
|
|
import { computed, onMounted, onBeforeUnmount, ref } from 'vue';
|
2022-05-01 16:51:07 +03:00
|
|
|
import tinycolor from 'tinycolor2';
|
2022-08-06 08:02:03 +03:00
|
|
|
import { globalEvents } from '@/events.js';
|
2023-05-19 21:38:07 +03:00
|
|
|
import { defaultIdlingRenderScheduler } from '@/scripts/idle-render.js';
|
2020-02-14 16:31:24 +02:00
|
|
|
|
2022-08-06 10:39:09 +03:00
|
|
|
// https://stackoverflow.com/questions/1878907/how-can-i-find-the-difference-between-two-angles
|
|
|
|
const angleDiff = (a: number, b: number) => {
|
|
|
|
const x = Math.abs(a - b);
|
|
|
|
return Math.abs((x + Math.PI) % (Math.PI * 2) - Math.PI);
|
|
|
|
};
|
|
|
|
|
2022-08-05 17:51:15 +03:00
|
|
|
const graduationsPadding = 0.5;
|
2022-08-06 10:39:09 +03:00
|
|
|
const textsPadding = 0.6;
|
2022-08-05 17:51:15 +03:00
|
|
|
const handsPadding = 1;
|
|
|
|
const handsTailLength = 0.7;
|
|
|
|
const hHandLengthRatio = 0.75;
|
|
|
|
const mHandLengthRatio = 1;
|
|
|
|
const sHandLengthRatio = 1;
|
2022-08-06 10:39:09 +03:00
|
|
|
const numbersOpacityFactor = 0.35;
|
2022-08-05 17:51:15 +03:00
|
|
|
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
thickness?: number;
|
|
|
|
offset?: number;
|
|
|
|
twentyfour?: boolean;
|
2022-08-06 10:39:09 +03:00
|
|
|
graduations?: 'none' | 'dots' | 'numbers';
|
|
|
|
fadeGraduations?: boolean;
|
2022-08-30 20:19:25 +03:00
|
|
|
sAnimation?: 'none' | 'elastic' | 'easeOut';
|
2023-04-13 06:20:39 +03:00
|
|
|
now?: () => Date;
|
2022-01-10 17:05:18 +02:00
|
|
|
}>(), {
|
2022-08-05 17:51:15 +03:00
|
|
|
numbers: false,
|
2022-01-10 17:05:18 +02:00
|
|
|
thickness: 0.1,
|
2022-08-05 17:51:15 +03:00
|
|
|
offset: 0 - new Date().getTimezoneOffset(),
|
|
|
|
twentyfour: false,
|
2022-08-06 08:02:03 +03:00
|
|
|
graduations: 'dots',
|
2022-08-06 10:39:09 +03:00
|
|
|
fadeGraduations: true,
|
2022-08-30 20:19:25 +03:00
|
|
|
sAnimation: 'elastic',
|
2023-04-13 06:20:39 +03:00
|
|
|
now: () => new Date(),
|
2022-08-05 17:51:15 +03:00
|
|
|
});
|
|
|
|
|
2022-08-06 08:02:03 +03:00
|
|
|
const graduationsMajor = computed(() => {
|
|
|
|
const angles: number[] = [];
|
|
|
|
const times = props.twentyfour ? 24 : 12;
|
|
|
|
for (let i = 0; i < times; i++) {
|
|
|
|
const angle = Math.PI * i / (times / 2);
|
|
|
|
angles.push(angle);
|
|
|
|
}
|
|
|
|
return angles;
|
|
|
|
});
|
2022-08-05 17:51:15 +03:00
|
|
|
const texts = computed(() => {
|
|
|
|
const angles: number[] = [];
|
|
|
|
const times = props.twentyfour ? 24 : 12;
|
|
|
|
for (let i = 0; i < times; i++) {
|
|
|
|
const angle = Math.PI * i / (times / 2);
|
|
|
|
angles.push(angle);
|
|
|
|
}
|
|
|
|
return angles;
|
2022-01-10 17:05:18 +02:00
|
|
|
});
|
2020-02-14 16:31:24 +02:00
|
|
|
|
2022-08-06 14:04:23 +03:00
|
|
|
let enabled = true;
|
2023-12-07 07:42:09 +02:00
|
|
|
const majorGraduationColor = ref<string>();
|
2022-08-06 14:04:23 +03:00
|
|
|
//let minorGraduationColor = $ref<string>();
|
2023-12-07 07:42:09 +02:00
|
|
|
const sHandColor = ref<string>();
|
|
|
|
const mHandColor = ref<string>();
|
|
|
|
const hHandColor = ref<string>();
|
|
|
|
const nowColor = ref<string>();
|
|
|
|
const h = ref<number>(0);
|
|
|
|
const m = ref<number>(0);
|
|
|
|
const s = ref<number>(0);
|
|
|
|
const hAngle = ref<number>(0);
|
|
|
|
const mAngle = ref<number>(0);
|
|
|
|
const sAngle = ref<number>(0);
|
|
|
|
const disableSAnimate = ref(false);
|
2022-08-26 18:40:20 +03:00
|
|
|
let sOneRound = false;
|
2023-05-19 21:38:07 +03:00
|
|
|
const sLine = ref<SVGPathElement>();
|
2020-02-14 16:31:24 +02:00
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
function tick() {
|
2023-04-13 06:20:39 +03:00
|
|
|
const now = props.now();
|
|
|
|
now.setMinutes(now.getMinutes() + now.getTimezoneOffset() + props.offset);
|
2023-12-07 07:42:09 +02:00
|
|
|
const previousS = s.value;
|
|
|
|
const previousM = m.value;
|
|
|
|
const previousH = h.value;
|
|
|
|
s.value = now.getSeconds();
|
|
|
|
m.value = now.getMinutes();
|
|
|
|
h.value = now.getHours();
|
|
|
|
if (previousS === s.value && previousM === m.value && previousH === h.value) {
|
2023-04-13 06:20:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-12-07 07:42:09 +02:00
|
|
|
hAngle.value = Math.PI * (h.value % (props.twentyfour ? 24 : 12) + (m.value + s.value / 60) / 60) / (props.twentyfour ? 12 : 6);
|
|
|
|
mAngle.value = Math.PI * (m.value + s.value / 60) / 30;
|
2023-05-19 21:38:07 +03:00
|
|
|
if (sOneRound && sLine.value) { // 秒針が一周した際のアニメーションをよしなに処理する(これが無いと秒が59->0になったときに期待したアニメーションにならない)
|
2023-12-07 07:42:09 +02:00
|
|
|
sAngle.value = Math.PI * 60 / 30;
|
2023-05-19 21:38:07 +03:00
|
|
|
defaultIdlingRenderScheduler.delete(tick);
|
|
|
|
sLine.value.addEventListener('transitionend', () => {
|
2023-12-07 07:42:09 +02:00
|
|
|
disableSAnimate.value = true;
|
2023-05-19 21:38:07 +03:00
|
|
|
requestAnimationFrame(() => {
|
2023-12-07 07:42:09 +02:00
|
|
|
sAngle.value = 0;
|
2023-05-19 21:38:07 +03:00
|
|
|
requestAnimationFrame(() => {
|
2023-12-07 07:42:09 +02:00
|
|
|
disableSAnimate.value = false;
|
2023-05-19 21:38:07 +03:00
|
|
|
if (enabled) {
|
|
|
|
defaultIdlingRenderScheduler.add(tick);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, { once: true });
|
2022-08-26 18:40:20 +03:00
|
|
|
} else {
|
2023-12-07 07:42:09 +02:00
|
|
|
sAngle.value = Math.PI * s.value / 30;
|
2022-08-26 18:40:20 +03:00
|
|
|
}
|
2023-12-07 07:42:09 +02:00
|
|
|
sOneRound = s.value === 59;
|
2022-01-10 17:05:18 +02:00
|
|
|
}
|
2020-02-14 16:31:24 +02:00
|
|
|
|
2022-08-06 14:04:23 +03:00
|
|
|
tick();
|
|
|
|
|
2022-08-06 08:02:03 +03:00
|
|
|
function calcColors() {
|
|
|
|
const computedStyle = getComputedStyle(document.documentElement);
|
|
|
|
const dark = tinycolor(computedStyle.getPropertyValue('--bg')).isDark();
|
|
|
|
const accent = tinycolor(computedStyle.getPropertyValue('--accent')).toHexString();
|
2023-12-07 07:42:09 +02:00
|
|
|
majorGraduationColor.value = dark ? 'rgba(255, 255, 255, 0.3)' : 'rgba(0, 0, 0, 0.3)';
|
2022-08-06 14:04:23 +03:00
|
|
|
//minorGraduationColor = dark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(0, 0, 0, 0.2)';
|
2023-12-07 07:42:09 +02:00
|
|
|
sHandColor.value = dark ? 'rgba(255, 255, 255, 0.5)' : 'rgba(0, 0, 0, 0.3)';
|
|
|
|
mHandColor.value = tinycolor(computedStyle.getPropertyValue('--fg')).toHexString();
|
|
|
|
hHandColor.value = accent;
|
|
|
|
nowColor.value = accent;
|
2022-08-06 08:02:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
calcColors();
|
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
onMounted(() => {
|
2023-05-19 21:38:07 +03:00
|
|
|
defaultIdlingRenderScheduler.add(tick);
|
2022-08-06 08:02:03 +03:00
|
|
|
globalEvents.on('themeChanged', calcColors);
|
2022-01-10 17:05:18 +02:00
|
|
|
});
|
2020-02-14 16:31:24 +02:00
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
onBeforeUnmount(() => {
|
2022-08-06 14:04:23 +03:00
|
|
|
enabled = false;
|
2023-05-19 21:38:07 +03:00
|
|
|
defaultIdlingRenderScheduler.delete(tick);
|
2022-08-06 08:02:03 +03:00
|
|
|
globalEvents.off('themeChanged', calcColors);
|
2020-02-14 16:31:24 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-14 04:46:22 +02:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2020-02-14 16:31:24 +02:00
|
|
|
display: block;
|
2023-01-14 04:46:22 +02:00
|
|
|
}
|
2022-08-26 18:40:20 +03:00
|
|
|
|
2023-01-14 04:46:22 +02:00
|
|
|
.s {
|
|
|
|
will-change: transform;
|
|
|
|
transform-origin: 50% 50%;
|
2022-08-26 18:40:20 +03:00
|
|
|
|
2023-01-14 04:46:22 +02:00
|
|
|
&.animate.elastic {
|
|
|
|
transition: transform .2s cubic-bezier(.4,2.08,.55,.44);
|
|
|
|
}
|
2022-08-30 20:19:25 +03:00
|
|
|
|
2023-01-14 04:46:22 +02:00
|
|
|
&.animate.easeOut {
|
|
|
|
transition: transform .7s cubic-bezier(0,.7,.3,1);
|
2022-08-26 18:40:20 +03:00
|
|
|
}
|
2020-02-14 16:31:24 +02:00
|
|
|
}
|
|
|
|
</style>
|