This commit is contained in:
こぴなたみぽ 2018-02-12 22:07:28 +09:00
parent dd724b609f
commit d3a0546390
1 changed files with 43 additions and 30 deletions

View File

@ -1,36 +1,41 @@
<mk-analog-clock> <template>
<canvas ref="canvas" width="256" height="256"></canvas> <canvas class="mk-analog-clock" ref="canvas" width="256" height="256"></canvas>
<style lang="stylus" scoped> </template>
:scope
> canvas <script lang="ts">
display block import Vue from 'vue';
width 256px import { themeColor } from '../../../config';
height 256px
</style> const Vec2 = function(x, y) {
<script lang="typescript">
const Vec2 = function(x, y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
};
export default Vue.extend({
data() {
return {
clock: null
}; };
},
this.on('mount', () => { mounted() {
this.draw() this.tick();
this.clock = setInterval(this.draw, 1000); this.clock = setInterval(this.tick, 1000);
}); },
beforeDestroy() {
this.on('unmount', () => {
clearInterval(this.clock); clearInterval(this.clock);
}); },
methods: {
tick() {
const canv = this.$refs.canvas as any;
this.draw = () => {
const now = new Date(); const now = new Date();
const s = now.getSeconds(); const s = now.getSeconds();
const m = now.getMinutes(); const m = now.getMinutes();
const h = now.getHours(); const h = now.getHours();
const ctx = this.$refs.canvas.getContext('2d'); const ctx = canv.getContext('2d');
const canvW = this.$refs.canvas.width; const canvW = canv.width;
const canvH = this.$refs.canvas.height; const canvH = canv.height;
ctx.clearRect(0, 0, canvW, canvH); ctx.clearRect(0, 0, canvW, canvH);
{ // { //
@ -72,7 +77,7 @@
const length = Math.min(canvW, canvH) / 4; const length = Math.min(canvW, canvH) / 4;
const uv = new Vec2(Math.sin(angle), -Math.cos(angle)); const uv = new Vec2(Math.sin(angle), -Math.cos(angle));
ctx.beginPath(); ctx.beginPath();
ctx.strokeStyle = _THEME_COLOR_; ctx.strokeStyle = themeColor;
ctx.lineWidth = 2; ctx.lineWidth = 2;
ctx.moveTo(canvW / 2 - uv.x * length / 5, canvH / 2 - uv.y * length / 5); ctx.moveTo(canvW / 2 - uv.x * length / 5, canvH / 2 - uv.y * length / 5);
ctx.lineTo(canvW / 2 + uv.x * length, canvH / 2 + uv.y * length); ctx.lineTo(canvW / 2 + uv.x * length, canvH / 2 + uv.y * length);
@ -90,6 +95,14 @@
ctx.lineTo(canvW / 2 + uv.x * length, canvH / 2 + uv.y * length); ctx.lineTo(canvW / 2 + uv.x * length, canvH / 2 + uv.y * length);
ctx.stroke(); ctx.stroke();
} }
}; }
</script> }
</mk-analog-clock> });
</script>
<style lang="stylus" scoped>
.mk-analog-clock
display block
width 256px
height 256px
</style>