components update

This commit is contained in:
Spedon
2023-03-20 21:58:34 +08:00
parent 843e9bb24e
commit 023aa36c3f
4 changed files with 133 additions and 64 deletions

View File

@@ -1,31 +1,32 @@
import { overlayEnable } from './overlay' import { overlayEnable } from './overlay'
import { import {
styleCache,
FIFO,
layersStyleSet,
center,
type position, type position,
createImgElement, createImgElement,
calcImageIndex, calcImageIndex,
delay, delay
mouseToTransform
} from './utils' } from './utils'
import { thresholdSensitivityArray, thresholdIndex } from './thresholdCtl' import { thresholdSensitivityArray, thresholdIndex } from './thresholdCtl'
import { imgIndexSpanUpdate } from './indexDisp' import { imgIndexSpanUpdate } from './indexDisp'
import { imagesArrayLen, imagesArray } from './dataFetch' import { imagesArrayLen, imagesArray } from './dataFetch'
import { preloader } from './imageCache'
const imagesDiv = document.getElementsByClassName('images').item(0) as HTMLDivElement // get layer divs
export const layers: HTMLDivElement[] = [
let imagesDivNodes: NodeListOf<HTMLImageElement> document.getElementById('layer1') as HTMLDivElement,
document.getElementById('layer2') as HTMLDivElement,
export const desktopImagesInit = (): void => { document.getElementById('layer3') as HTMLDivElement,
for (let i = 0; i < imagesArrayLen; i++) { document.getElementById('layer4') as HTMLDivElement,
imagesDiv.appendChild(createImgElement(imagesArray[i])) document.getElementById('layer5') as HTMLDivElement
} ]
imagesDivNodes = imagesDiv.childNodes as NodeListOf<HTMLImageElement>
}
export const trailingImageIndexes: number[] = []
// layers position caching // layers position caching
export const layersStyleArray: number[][] = [[], []] export const layersStyleArray: string[][] = [
['0px', '0px', '0px', '0px', '0px'],
['0px', '0px', '0px', '0px', '0px']
]
// global index for "activated" // global index for "activated"
export let globalIndex: number = 0 export let globalIndex: number = 0
@@ -33,37 +34,13 @@ export let globalIndex: number = 0
// last position set as "activated" // last position set as "activated"
let last: position = { x: 0, y: 0 } let last: position = { x: 0, y: 0 }
let EnterOverlayController = new AbortController()
// activate top image // activate top image
const activate = (index: number, mouseX: number, mouseY: number): void => { const activate = (index: number, x: number, y: number): void => {
EnterOverlayController.abort() const imgElem: HTMLImageElement = createImgElement(imagesArray[index])
EnterOverlayController = new AbortController() styleCache(x, y, layersStyleArray)
trailingImageIndexes.push(index) layersStyleSet(layersStyleArray, layers)
let indexesNum: number = trailingImageIndexes.length FIFO(imgElem, layers)
// push the tail index out and hide the image last = { x, y }
if (indexesNum > 10) {
imagesDivNodes[trailingImageIndexes.shift() as number].style.display = 'none'
indexesNum = 10
}
// set img position
imagesDivNodes[index].style.transform = mouseToTransform(mouseX, mouseY, true, true)
// reset z index
for (let i = 0; i < indexesNum; i++) {
imagesDivNodes[trailingImageIndexes[i]].style.zIndex = `${i}`
}
imagesDivNodes[index].style.display = 'block'
imagesDivNodes[index].addEventListener(
'click',
() => {
void enterOverlay()
},
{
passive: true,
signal: EnterOverlayController.signal
}
)
last = { x: mouseX, y: mouseY }
} }
// Compare the current mouse position with the last activated position // Compare the current mouse position with the last activated position
@@ -89,11 +66,17 @@ export const handleOnMove = (e: MouseEvent): void => {
} }
async function enterOverlay(): Promise<void> { async function enterOverlay(): Promise<void> {
layers[4].style.backgroundImage = ''
const topLayerImage = layers[4].lastElementChild as HTMLImageElement
topLayerImage.style.transition = ''
// stop images animation // stop images animation
window.removeEventListener('mousemove', handleOnMove) window.removeEventListener('mousemove', handleOnMove)
// set top image // set top image
alert('center') center(layers[4])
await delay(2500) for (let i = 4; i >= 0; i--) {
layers[i].dataset.status = `t${4 - i}`
}
await delay(1600)
// Offset previous self increment of global index (by handleOnMove) // Offset previous self increment of global index (by handleOnMove)
globalIndexDec() globalIndexDec()
// overlay init // overlay init
@@ -102,16 +85,22 @@ async function enterOverlay(): Promise<void> {
// initialization // initialization
export const trackMouseInit = (): void => { export const trackMouseInit = (): void => {
desktopImagesInit() window.addEventListener('mousemove', handleOnMove)
window.addEventListener('mousemove', handleOnMove, { passive: true }) layers[4].addEventListener(
'click',
() => {
void enterOverlay()
},
{
passive: true
}
)
} }
export const globalIndexDec = (): void => { export const globalIndexDec = (): void => {
globalIndex-- globalIndex--
preloader(globalIndex)
} }
export const globalIndexInc = (): void => { export const globalIndexInc = (): void => {
globalIndex++ globalIndex++
preloader(globalIndex)
} }

View File

@@ -1,5 +1,20 @@
import { delay, calcImageIndex, mouseToTransform } from './utils' import {
import { handleOnMove, globalIndex, globalIndexDec, globalIndexInc } from './desktop' delay,
layersStyleSet,
center,
createImgElement,
calcImageIndex,
FIFO,
mouseToTransform
} from './utils'
import {
layersStyleArray,
layers,
handleOnMove,
globalIndex,
globalIndexDec,
globalIndexInc
} from './desktop'
import { imagesArray, imagesArrayLen } from './dataFetch' import { imagesArray, imagesArrayLen } from './dataFetch'
import { imgIndexSpanUpdate } from './indexDisp' import { imgIndexSpanUpdate } from './indexDisp'
@@ -20,7 +35,12 @@ const setCursorText = (text: string): void => {
const setTextPos = (e: MouseEvent): void => { const setTextPos = (e: MouseEvent): void => {
// overlayCursor.style.left = `${e.clientX}px` // overlayCursor.style.left = `${e.clientX}px`
// overlayCursor.style.top = `${e.clientY}px` // overlayCursor.style.top = `${e.clientY}px`
overlayCursor.style.transform = mouseToTransform(e.clientX, e.clientY, false, true) overlayCursor.style.transform = mouseToTransform(
`${e.clientX}px`,
`${e.clientY}px`,
false,
true
)
} }
// disable listeners // disable listeners
@@ -47,19 +67,28 @@ export const overlayDisable = (): void => {
// handle close click // handle close click
async function handleCloseClick(): Promise<void> { async function handleCloseClick(): Promise<void> {
overlayDisable() overlayDisable()
await delay(2500) layersStyleSet(layersStyleArray, layers)
window.addEventListener('mousemove', handleOnMove) for (let i: number = 4; i >= 0; i--) {
layers[i].dataset.status = `r${4 - i}`
}
await delay(1700)
for (let i: number = 4; i >= 0; i--) {
layers[i].dataset.status = 'null'
}
window.addEventListener('mousemove', handleOnMove, { passive: true })
} }
const handlePrevClick = (): void => { const handlePrevClick = (): void => {
globalIndexDec() globalIndexDec()
const imgIndex = calcImageIndex(globalIndex, imagesArrayLen) const imgIndex = calcImageIndex(globalIndex, imagesArrayLen)
FIFO(createImgElement(imagesArray[imgIndex]), layers)
imgIndexSpanUpdate(imgIndex + 1, imagesArrayLen) imgIndexSpanUpdate(imgIndex + 1, imagesArrayLen)
} }
const handleNextClick = (): void => { const handleNextClick = (): void => {
globalIndexInc() globalIndexInc()
const imgIndex = calcImageIndex(globalIndex, imagesArrayLen) const imgIndex = calcImageIndex(globalIndex, imagesArrayLen)
FIFO(createImgElement(imagesArray[imgIndex]), layers)
imgIndexSpanUpdate(imgIndex + 1, imagesArrayLen) imgIndexSpanUpdate(imgIndex + 1, imagesArrayLen)
} }
@@ -109,6 +138,7 @@ export const vwRefreshInit = (): void => {
r.style.setProperty('--footer-height', '31px') r.style.setProperty('--footer-height', '31px')
} }
// recenter image (only in overlay) // recenter image (only in overlay)
if (layers[4].dataset.status === 't0') center(layers[4])
}, },
{ passive: true } { passive: true }
) )

View File

@@ -5,7 +5,7 @@ const thresholdDisp = document.getElementsByClassName('thid').item(0) as HTMLSpa
// threshold data // threshold data
const threshold: number[] = [0, 40, 80, 120, 160, 200] const threshold: number[] = [0, 40, 80, 120, 160, 200]
export const thresholdSensitivityArray: number[] = [200, 40, 18, 14, 9, 5] export const thresholdSensitivityArray: number[] = [100, 40, 18, 14, 9, 5]
export let thresholdIndex: number = 2 export let thresholdIndex: number = 2
// update inner text of threshold display element // update inner text of threshold display element

View File

@@ -18,20 +18,66 @@ export interface deviceType {
desktop: boolean desktop: boolean
} }
// cache a xy position to array
export const styleCache = (x: number, y: number, styleArray: string[][]): void => {
// pop element if length surpass limitation
styleArray[0].shift()
styleArray[1].shift()
// push new element
styleArray[0].push(`${x}px`)
styleArray[1].push(`${y}px`)
}
// 0 to 0001, 25 to 0025 // 0 to 0001, 25 to 0025
export const duper = (num: number): string => { export const duper = (num: number): string => {
return ('0000' + num.toString()).slice(-4) return ('0000' + num.toString()).slice(-4)
} }
// FIFO data array for image display
export const FIFO = (
element: HTMLImageElement,
layersArray: HTMLDivElement[],
fifoPosition: boolean = true
): void => {
function layerProcess(layerL: HTMLDivElement, layerH: HTMLDivElement): void {
if (layerL.childElementCount !== 0)
layerL.removeChild(layerL.lastElementChild as HTMLImageElement)
if (layerH.childElementCount !== 0) {
const layerHNode = layerH.lastElementChild as HTMLImageElement
layerL.appendChild(layerHNode.cloneNode(true))
if (fifoPosition) layerL.style.transform = layerH.style.transform
}
}
for (let i: number = 0; i <= 3; i++) {
layerProcess(layersArray[i], layersArray[i + 1])
}
if (layersArray[4].childElementCount !== 0)
layersArray[4].removeChild(layersArray[4].lastElementChild as HTMLImageElement)
layersArray[4].appendChild(element)
}
export const mouseToTransform = ( export const mouseToTransform = (
x: number, x: string,
y: number, y: string,
centerCorrection: boolean = true, centerCorrection: boolean = true,
accelerate: boolean = false accelerate: boolean = false
): string => { ): string => {
return `translate${accelerate ? '3d' : ''}(${ return `translate${accelerate ? '3d' : ''}(${
centerCorrection ? `calc(${x}px - 50%)` : `${x}px` centerCorrection ? `calc(${x} - 50%)` : `${x}`
}, ${centerCorrection ? `calc(${y}px - 50%)` : `${y}px`}${accelerate ? ', 0' : ''})` }, ${centerCorrection ? `calc(${y} - 50%)` : `${y}`}${accelerate ? ', 0' : ''})`
}
// set position for 5 image display layers
export const layersStyleSet = (
styleArray: string[][],
layersArray: HTMLDivElement[]
): void => {
function posSet(layer: HTMLDivElement, index: number): void {
layer.style.transform = mouseToTransform(styleArray[0][index], styleArray[1][index])
}
for (let i = 4; i >= 0; i--) {
posSet(layersArray[i], i)
}
} }
// eslint-disable-next-line @typescript-eslint/promise-function-async // eslint-disable-next-line @typescript-eslint/promise-function-async
@@ -53,7 +99,7 @@ export const center = (e: HTMLDivElement): void => {
} else { } else {
y = (window.innerHeight - 31) / 2 + 31 y = (window.innerHeight - 31) / 2 + 31
} }
e.style.transform = mouseToTransform(x, y) e.style.transform = mouseToTransform(`${x}px`, `${y}px`)
} }
export const createImgElement = (input: ImageData): HTMLImageElement => { export const createImgElement = (input: ImageData): HTMLImageElement => {
@@ -62,7 +108,11 @@ export const createImgElement = (input: ImageData): HTMLImageElement => {
img.setAttribute('alt', '') img.setAttribute('alt', '')
img.setAttribute('height', input.imgH) img.setAttribute('height', input.imgH)
img.setAttribute('width', input.imgW) img.setAttribute('width', input.imgW)
img.style.display = 'none' img.style.zIndex = '0'
img.style.opacity = '1'
img.style.backgroundImage = `linear-gradient(15deg, ${input.pColor}, ${input.sColor})`
img.style.backgroundSize = `${input.imgW}px ${input.imgH}px`
img.dataset.status = 'null'
return img return img
} }