zent
Version:
一套前端设计语言和基于React的实现
31 lines (27 loc) • 918 B
text/typescript
export function calculateChange(e, skip, props, container) {
!skip && e.preventDefault();
const containerWidth = container.clientWidth;
const containerHeight = container.clientHeight;
const x = typeof e.pageX === 'number' ? e.pageX : e.touches[0].pageX;
const y = typeof e.pageY === 'number' ? e.pageY : e.touches[0].pageY;
let left = x - (container.getBoundingClientRect().left + window.pageXOffset);
let top = y - (container.getBoundingClientRect().top + window.pageYOffset);
if (left < 0) {
left = 0;
} else if (left > containerWidth) {
left = containerWidth;
} else if (top < 0) {
top = 0;
} else if (top > containerHeight) {
top = containerHeight;
}
const saturation = (left * 100) / containerWidth;
const bright = -((top * 100) / containerHeight) + 100;
return {
h: props.hsl.h,
s: saturation,
v: bright,
a: props.hsl.a,
source: 'rgb',
};
}