javascripthtmlcsspointer-events

detecting pointermove event outside div while pointer is down


I was testing my skills the other day. The ultimate goal is to replace all html tags with div

Goal: create a color picker like the one in photoshop

Requirements: use H5, JS, and CSS only (I also use ReactJS for building the structure)

Already achieved:

HTML structure:

let padding = props.size * 0.1;
<div className="color-picker">
    <div className='color-picker-container' style={ { width: `${props.size + 2 * padding}px` } } onPointerMove={ onMove } onPointerDown={ onMove }>
        <div className="color-picker-panel" style={ { width: `${props.size}px` } }/>
        <div className="color-picker-indicator" style={ { left: pickerPos.left, top: pickerPos.top, borderColor: `rgb(${getInvertedColorChannel(ColorChannel.R, currentMaxColor)}, ${getInvertedColorChannel(ColorChannel.G, currentMaxColor)}, ${getInvertedColorChannel(ColorChannel.B, currentMaxColor)}` } }/>
    </div>
</div>

Result:

enter image description here

Explanation:

Problem:

feel free to ask for more details, thanks!


Solution

  • When you get the mouse down event in the div attach a mouseMove listener to <body> and track it from there. On mouse up, remove the listener.