修改时钟组件的自适应功能

This commit is contained in:
luojiahao 2022-12-15 13:42:34 +08:00
parent d4ed383e76
commit 59aa5a9c57
3 changed files with 37 additions and 54 deletions

View File

@ -1,7 +1,5 @@
import {css, CSSResult,unsafeCSS} from 'lit' import {css, CSSResult, unsafeCSS} from 'lit'
const darkHour = unsafeCSS( const darkHour = unsafeCSS(new URL('./svg/dark_hour.svg', import.meta.url).href)
new URL('./svg/dark_hour.svg', import.meta.url).href
)
const darkMinute = unsafeCSS( const darkMinute = unsafeCSS(
new URL('./svg/dark_minute.svg', import.meta.url).href new URL('./svg/dark_minute.svg', import.meta.url).href
) )
@ -83,8 +81,7 @@ export const sharedStyles: CSSResult = css`
min-width: var(--autoWidth); min-width: var(--autoWidth);
height: var(--autoWidth); height: var(--autoWidth);
min-height: var(--autoWidth); min-height: var(--autoWidth);
padding: var(--autoBorder); padding: calc(6px * var(--ratio));
transform: scale(var(--autoZoom));
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
@ -107,13 +104,13 @@ export const sharedStyles: CSSResult = css`
} }
.star-clock-shaft { .star-clock-shaft {
position: absolute; position: absolute;
width: var(--autoShapt); width: calc(18px * var(--ratio));
height: var(--autoShapt); height: calc(18px * var(--ratio));
top: 50%; top: 50%;
left: 50%; left: 50%;
border-radius: 50%; border-radius: 50%;
background: #c94d18; background: #c94d18;
border: var(--autoShaptBorder) solid #e07548; border: calc(4px * var(--ratio)) solid #e07548;
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
z-index: 1; z-index: 1;
} }
@ -126,11 +123,12 @@ export const sharedStyles: CSSResult = css`
width: 0px; width: 0px;
background-color: transparent; background-color: transparent;
transform-origin: 0 100%; transform-origin: 0 100%;
transform: rotate(calc(var(--i) * 30deg));
} }
.star-clock-point { .star-clock-point {
display: inline-block; display: inline-block;
width: var(--autoPoint); width: calc(10px * var(--ratio));
height: var(--autoPoint); height: calc(10px * var(--ratio));
position: absolute; position: absolute;
right: 50%; right: 50%;
top: 0; top: 0;
@ -140,37 +138,40 @@ export const sharedStyles: CSSResult = css`
rgba(245, 245, 245, 0.01) 100% rgba(245, 245, 245, 0.01) 100%
); );
border-radius: 50%; border-radius: 50%;
transform: translate(-50%, 0); transform: translate(-50%, 0) rotate(calc(var(--i) * -30deg));
} }
.star-clock-hour-hand { .star-clock-hour-hand {
position: absolute; position: absolute;
bottom: 50%; bottom: 50%;
left: 50%; left: 50%;
width: 12px; width: calc(12px * var(--ratio));
height: 90px; height: calc(90px * var(--ratio));
transform-origin: 0 100%; transform-origin: 0 100%;
display: inline-block; display: inline-block;
transform: rotate(var(--rotateHour)) translate(-50%, 0); transform: rotate(var(--rotateHour)) translate(-50%, 0);
background-size: cover;
} }
.star-clock-minute-hand { .star-clock-minute-hand {
position: absolute; position: absolute;
bottom: 50%; bottom: 50%;
left: 50%; left: 50%;
width: 12px; width: calc(12px * var(--ratio));
height: 132px; height: calc(132px * var(--ratio));
transform-origin: 0 100%; transform-origin: 0 100%;
display: inline-block; display: inline-block;
transform: rotate(var(--rotateMinute)) translate(-50%, 0); transform: rotate(var(--rotateMinute)) translate(-50%, 0);
background-size: cover;
} }
.star-clock-second-hand { .star-clock-second-hand {
position: absolute; position: absolute;
bottom: calc(50% - 17px); bottom: calc(50% - 17px * var(--ratio));
left: 50%; left: 50%;
display: inline-block; display: inline-block;
width: 4px; width: calc(4px * var(--ratio));
height: 160px; height: calc(160px * var(--ratio));
transform-origin: 0 calc(100% - 17px); transform-origin: 0 calc(100% - 17px * var(--ratio));
transform: rotate(var(--rotateSecond)) translate(-50%, 0); transform: rotate(var(--rotateSecond)) translate(-50%, 0);
background-size: cover;
} }
/*无表盘深色模式*/ /*无表盘深色模式*/
.star-clock-case-transparent { .star-clock-case-transparent {
@ -179,7 +180,6 @@ export const sharedStyles: CSSResult = css`
min-width: var(--autoWidth); min-width: var(--autoWidth);
height: var(--autoWidth); height: var(--autoWidth);
min-height: var(--autoWidth); min-height: var(--autoWidth);
transform: scale(var(--autoZoom));
border-radius: 50%; border-radius: 50%;
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -188,8 +188,8 @@ export const sharedStyles: CSSResult = css`
} }
.star-clock-point-transparent { .star-clock-point-transparent {
display: inline-block; display: inline-block;
width: var(--autoPointTransparent); width: calc(16px * var(--ratio));
height: var(--autoPointTransparent); height: calc(16px * var(--ratio));
position: absolute; position: absolute;
right: 50%; right: 50%;
top: 0; top: 0;

View File

@ -27,17 +27,14 @@ export class StarClock extends LitElement {
} }
@query('.star-clock') @query('.star-clock')
starClock: HTMLDivElement | undefined starClock!: HTMLDivElement
getTransparentClock() { getTransparentClock() {
return html` return html`
<div class="star-clock"> <div class="star-clock">
<div class="star-clock-case-transparent ${this.mode}"> <div class="star-clock-case-transparent ${this.mode}">
${[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map( ${[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(
(item) => html` (item) => html`
<span <span class="star-clock-rotate" style="--i: ${item}">
class="star-clock-rotate"
style="transform: rotate(${(360 / 12) * item}deg)"
>
<span class="star-clock-point-transparent"></span> <span class="star-clock-point-transparent"></span>
</span> </span>
` `
@ -57,15 +54,8 @@ export class StarClock extends LitElement {
<div class="star-clock-shaft"></div> <div class="star-clock-shaft"></div>
${[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map( ${[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(
(item) => html` (item) => html`
<span <span class="star-clock-rotate" style="--i: ${item}">
class="star-clock-rotate" <span class="star-clock-point"></span>
style="transform: rotate(${(360 / 12) * item}deg)"
>
<span
class="star-clock-point"
style="transform: translate(50%, 0) rotate(${-(360 / 12) *
item}deg)"
></span>
</span> </span>
` `
)} )}
@ -111,24 +101,16 @@ export class StarClock extends LitElement {
setInterval(() => { setInterval(() => {
this.dateUpdated(new Date()) this.dateUpdated(new Date())
}, 1000) }, 1000)
window.addEventListener('resize', () => { const resizeObserver = new ResizeObserver(this.resize)
this.requestUpdate() resizeObserver.observe(this.starClock)
})
} }
protected resize() {
let minWidth = Math.min( protected resize = () => {
this.starClock?.clientHeight || 100, let minWidth = Math.min(this.offsetHeight, this.offsetWidth)
this.starClock?.clientWidth || 100
) const ratio = minWidth / 380
let widthMy = 380 this.style.setProperty('--autoWidth', minWidth + 'px')
let bili = minWidth / widthMy this.style.setProperty('--ratio', `${ratio}`)
this.style.setProperty('--autoWidth', widthMy + 'px')
this.style.setProperty('--autoZoom', bili.toString())
this.style.setProperty('--autoBorder', '6px')
this.style.setProperty('--autoPoint', '10px')
this.style.setProperty('--autoShapt', '18px')
this.style.setProperty('--autoShaptBorder', '4px')
this.style.setProperty('--autoPointTransparent', '16px')
} }
} }
declare global { declare global {

View File

@ -93,6 +93,7 @@ class ClockWidget extends GaiaWidget {
:host { :host {
height: 100%; height: 100%;
width: 100%; width: 100%;
padding: 0.5%;
} }
</style> </style>
<star-clock type="${this._type}" mode="${this.mode}"></star-clock> <star-clock type="${this._type}" mode="${this.mode}"></star-clock>