To efficiently handle mouse pointer events in your component, you can use the lifecycle methods onPointerDown
, onPointerMove
, and onPointerUp
. These methods, as described in the bobril/index.ts, make implementing this functionality easier with just a little extra code.
Incorporate bobril's b.registerMouseOwner
with your context within the onPointerDown
method to set yourself as the mouse owner.
onPointerDown(ctx: ICtx, event: b.IBobrilPointerEvent) {
b.registerMouseOwner(ctx);
// Store the initial coordinates
ctx.lastX = event.x;
ctx.lastY = event.y;
return true;
},
You can then handle mouse movement in the onPointerMove
method even when the pointer moves outside the element. Ensure that you are still the current owner before executing any logic within this method:
onPointerMove(ctx: ICtx, event: b.IBobrilPointerEvent) {
if (!b.isMouseOwner(ctx))
return false;
if (ctx.lastX === event.x && ctx.lastY === event.y)
return false;
// Execute registered handler function
if (ctx.data.onMove) {
ctx.data.onMove(event.x - ctx.lastX, event.y - ctx.lastY);
}
// Update coordinates
ctx.lastX = event.x;
ctx.lastY = event.y;
return true;
},
Don't forget to release your mouse ownership using the onPointerUp
method:
onPointerUp(ctx: ICtx, event: b.IBobrilPointerEvent) {
b.releaseMouseOwner();
return true;
}
The example above demonstrates how to store and utilize the last pointer coordinates in the ICtx
component context. This allows for calculating deltaX
and deltaY
to be used by the onMove
handler. Remember to register this handler through input data when creating the component node.