Skip to content

Commit 6ed50ba

Browse files
Add getPixelDataFromMedia function for image color detection
1 parent e6129d5 commit 6ed50ba

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

js/blocks/SensorsBlocks.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,37 @@ function setupSensorsBlocks(activity) {
744744
}
745745
return ctx.getImageData(Math.floor(x), Math.floor(y), 1, 1).data;
746746
}
747+
getPixelDataFromMedia(x, y, mediaBlock) {
748+
try {
749+
// Find the media bitmap in the block
750+
const mediaBitmap = mediaBlock.container.getChildByName("media");
751+
if (!mediaBitmap) {
752+
throw new Error("No media found in this block");
753+
}
754+
755+
// Create a temporary canvas
756+
const tempCanvas = document.createElement('canvas');
757+
const tempCtx = tempCanvas.getContext('2d');
758+
759+
// Set canvas size to match the image
760+
const image = mediaBitmap.image;
761+
tempCanvas.width = image.width;
762+
tempCanvas.height = image.height;
763+
764+
// Draw the media image to the temporary canvas
765+
tempCtx.drawImage(image, 0, 0);
766+
767+
// Get pixel data at the specified coordinates
768+
const pixelData = tempCtx.getImageData(Math.floor(x), Math.floor(y), 1, 1).data;
769+
770+
return pixelData;
771+
772+
} catch (error) {
773+
console.error("Error getting pixel data from media:", error);
774+
throw new Error("Cannot get pixel data from media block");
775+
}
776+
}
777+
747778

748779
/**
749780
* Determines the color based on pixel data.

0 commit comments

Comments
 (0)