I’m working on a custom control system that I’ve been using successfully in regular web applications. The mouse tracking works perfectly in Chrome and other browsers, but when I implement it in a Figma plugin interface, I run into issues.
function initializeControl(element, w, h) {
element.width = w;
element.height = h;
element.style.display = 'block';
element.changeEvent = new Event('valueChanged');
element.addEventListener('mousedown', function(event) {
const evt = window.event || event;
if (evt.button === 0) {
evt.preventDefault();
if (element.setCapture)
element.setCapture();
element.isDragging = true;
element.startX = evt.clientX;
element.initialValue = element.value;
}
});
element.addEventListener('losecapture', function() {
element.isDragging = false;
});
document.addEventListener('mouseup', function(evt) {
const e = window.event || evt;
if (e.button === 0 && element.isDragging)
element.isDragging = false;
}, true);
(element.setCapture ? element : document).addEventListener('mousemove', function(evt) {
if (element.isDragging) {
const deltaX = element.startX - evt.clientX;
const multiplier = 8 * Math.pow(Math.abs(deltaX), element.sensitivity);
element.value = Math.min(Math.max(element.minimum, element.initialValue - deltaX * element.factor * multiplier), element.maximum);
element.render();
element.dispatchEvent(element.changeEvent);
}
}, true);
element.onwheel = function(evt) {
evt.preventDefault();
const target = evt.target;
target.value = Math.min(Math.max(target.minimum, target.value + (evt.deltaY < 0 ? 1 : -1) * target.factor), target.maximum);
target.dispatchEvent(target.changeEvent);
target.render();
};
element.render = function() {
// rendering logic here
};
element.render();
}
The problem is that mouse capture gets lost immediately when the cursor moves outside the plugin window boundaries. Has anyone found a workaround for this limitation in Figma plugins?