I'm working on a tool which will pick colors from an image, and I'm trying to use draggable divs to highlight the color in an image to add to the palette. The code I have works in the preview function of espresso, but once it's deployed to the website, the background color does not update real-time. Instead it will update when I scroll the window down or sometimes after a delay. Here is the relevant code:
for (i=0; i<maxPaletteSize; i++)
{
circleDiv[i] = document.getElementById('circle' + i);
$("#circle" + i).draggable({
drag: function(event, ui) {
// find current x,y of div
var circleX = ui.position.left;
var circleY = ui.position.top;
// figure out what color pixel the circle is over
var pixelNum = Math.round(circleY * img.width + circleX) * 4;
var color = "rgba(" + imgData.data[pixelNum] + "," + imgData.data[pixelNum+1] + "," + imgData.data[pixelNum+2] + ")";
// change div background to the appropriate color
$(ui.helper).css({backgroundColor: color});
}
});
}
When I step through the code, the color is set correctly, it's just the backgroundColor does not update real time (although it's more responsive when I step through the code). I've tried using animate as well as changing the display to none and then inline (suggestions I've seen for other issues posted here), but nothing makes it respond real time.
Any help would be appreciated.
Editing to add the code where the divs are initially set up. I store all the divs in an array for easier access in other parts of the code.
// draw the circles
for (i=0; i<paletteSize; i++)
{
// get the color from the palette
var paletteColorIdx = paletteCircles[i].palette;
// show the div
circleDiv[i].style.display = 'inline';
// calculate the absolute x,y coords
circleDiv[i].style.left = (uiFrame.offsetLeft + paletteCircles[i].x - circleDiv[i].offsetWidth/2) + "px";
circleDiv[i].style.top = (uiFrame.offsetTop + paletteCircles[i].y - circleDiv[i].offsetHeight/2) + "px";
// set the color
circleDiv[i].style.backgroundColor = "rgb(" + palette[paletteColorIdx][0] + "," + palette[paletteColorIdx][1] + "," + palette[paletteColorIdx][2] + ")";
}
jsfiddle link: http://jsfiddle.net/FzxNk/6/
To see behavior:
Load an image.
Move one of the circles onto an area that is a different color (it should be updating the colors as you move, but it doesn't.)
Move another circle off the image and then back on the image.
When you move the circle onto the image, the previous circle will "pop" to the correct color, and the one you just moved will be black.