Skip to content
On this page

getImageColor

获取图片中任意坐标的像素

Demo

原始图片: 鼠标移动获取到的颜色块:
固定右上角获取到的颜色块:

Usage

js
import { getImageColor } from '@morehook/utils'

const key = getRandomStr()
const { offsetX, offsetY } = useMousePosition({ type: 'move', target })

Type Declarations

typescript
/**
 * x: x轴坐标
 * y: y轴坐标
 * direction: 快捷定义坐标(左上,右上,中间,左下,右下),优先级大于x,y
 * target: 图片在页面的DOM,以防图片在外层被压缩宽高导致传入的x,y与实际不一
 */
interface Actions {
  x?: number
  y?: number
  direction?:
    | "left-top"
    | "right-top"
    | "center"
    | "left-bottom"
    | "right-bottom"
  target?: HTMLElement
}
/**
 * 获取图片中任意坐标的像素
 * @param url 图片路径
 * @param actions Actions
 */
export declare function getImageColor(
  url: string,
  actions: Actions
): Promise<number[]>

Source

SourceDemoDocs