合并代码
This commit is contained in:
13
src/utils/getNavbarHeight.ts
Normal file
13
src/utils/getNavbarHeight.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import Taro from "@tarojs/taro";
|
||||
|
||||
export const getNavbarHeight = (): { statusBarHeight: number; navbarHeight: number; totalHeight: number; } => {
|
||||
const systemInfo = Taro.getSystemInfoSync();
|
||||
const statusBarHeight = systemInfo?.statusBarHeight || 0;
|
||||
const isIOS = systemInfo.platform === "ios";
|
||||
const navbarHeight = isIOS ? 44 : 48;
|
||||
return {
|
||||
statusBarHeight,
|
||||
navbarHeight,
|
||||
totalHeight: statusBarHeight + navbarHeight,
|
||||
};
|
||||
};
|
||||
9
src/utils/index.ts
Normal file
9
src/utils/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export * from './getNavbarHeight'
|
||||
export * from './locationUtils'
|
||||
export * from './processImage'
|
||||
export * from './timeUtils'
|
||||
export * from './tokenManager'
|
||||
|
||||
export function delay(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
33
src/utils/processImage.ts
Normal file
33
src/utils/processImage.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export async function getTextColorOnImage(url) {
|
||||
let canvas
|
||||
const width = 100
|
||||
const height = 50
|
||||
try {
|
||||
// 1. 创建离屏Canvas
|
||||
canvas = wx.createOffscreenCanvas({ type: '2d', width, height })
|
||||
const ctx = canvas.getContext('2d')
|
||||
|
||||
// 2. 加载图片
|
||||
const img = canvas.createImage()
|
||||
await new Promise((resolve) => {
|
||||
img.onload = resolve
|
||||
img.src = url
|
||||
})
|
||||
|
||||
// 3. 绘制并分析图像
|
||||
ctx.drawImage(img, 0, 0, width, height)
|
||||
|
||||
// TODO: 增加取样,提高精确性
|
||||
const pixelData = ctx.getImageData(10, 10, 1, 1).data
|
||||
|
||||
// 4. 计算文字颜色
|
||||
const brightness = (pixelData[0] * 2126 + pixelData[1] * 7152 + pixelData[2] * 722) / 10000
|
||||
return { textColor: brightness > 128 ? 'black' : 'white' }
|
||||
} finally {
|
||||
// 释放资源
|
||||
if (canvas) {
|
||||
canvas.width = 0;
|
||||
canvas.height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user