MZ [Solved] Check if xy of picture is transparent (via JS)?

Hello,

is it possible to check if the pixel XY of a picture is transparent via java-script? I found some java-scripts online and something else (bitmap https://developer.rpgmakerweb.com/rpg-maker-mz/Bitmap.html). I don't know how to use it right, if I'm going in the right direction and if I just miss something or if it's just not possible.

Would be great if someone could help me.

What do I want to do? Showing a picture, get mouse x & y position and check if it's colliding with the picture.
You can use Bitmap#getAlphaPixel (rmmz_core.js, line 1534):
JavaScript:
/**
 * Returns alpha pixel value at the specified point.
 *
 * @param {number} x - The x coordinate of the pixel in the bitmap.
 * @param {number} y - The y coordinate of the pixel in the bitmap.
 * @returns {string} The alpha value.
 */
Bitmap.prototype.getAlphaPixel = function(x, y) {
Alpha is an integer from 0 (transparent) to 255 (opaque), cf ImageData.

Also, the Sprite wrapper includes an opacity value, also in the range 0~255. You can get the overall alpha by multiplying by opacity / 255. 👋

As an example, here's a function I wrote for one of my MZ plugins for testing alpha against a threshold:
Spoiler: sprite alpha test example
JavaScript:
/**
 * Tests the alpha of a given pixel in a given sprite against a threshold value.
 * @param {Sprite} p - The sprite to test
 * @param {Number} x - X coordinate of test pixel
 * @param {Number} y - Y coordinate of test pixel
 * @param {Number} t - Alpha threshold value
 * @returns {Boolean} True iff alpha(x, y) >= threshold.
 */
$.alphaTest = function(p, x, y, t) {
  return p?.bitmap?.getAlphaPixel(x, y) * p.opacity / 255 >= t;
};
In case you're interested, the full plugin (Cae_PictureTouch) can be found over here:
Thanks for the quick reply, unfortunately I don't quite understand where and how I can/must insert the "image name".

I played around a bit, unfortunately nothing worked or maybe I do something else wrong. Here is an example of how I tried it:

JavaScript:
AlphaTest = function(p, x, y, t) {
  return p?.bitmap?.getAlphaPixel(x, y) * p.opacity / 255 >= t;
};

var Test1 = AlphaTest($gameScreen.picture(1), $gameVariables.value(1), $gameVariables.value(2), 1);
console.log(Test1);

JavaScript:
AlphaTest = function(p, x, y, t) {
  return p?.bitmap?.getAlphaPixel(x, y) * p.opacity / 255 >= t;
};

var Test1 = AlphaTest("UFO_EnemyBoss", $gameVariables.value(1), $gameVariables.value(2), 1);
console.log(Test1);

I test it with a random picture, at the moment it's "UFO_EnemyBoss" that's located under "\img\pictures".


Event-Page 1: So first I do "show picture" (Name: UFO_EnemyBoss, ID: 1, X = 0, Y = 0, ...),
then activate self-switch A,

Event-Page 2 (if self-switch A is activated - parallel process):
I set variable 1 = TouchInput.x | Variable 2 = TouchInput.y,
1 frame wait,
script-call

(german)
1657561737645.png
Ah, OK!
  1. You need a sprite reference, i.e. Sprite_Picture, not an image filename.

  2. Also, you need sprite coordinates. TouchInput coordinates are screen-based, so you'll need to transform them to the sprite's coordinate system. Luckily, PIXI makes this easy!
Maybe try something like this (untested):
JavaScript:
const pictureId = 1;
const sprite = SceneManager._scene._spriteset._pictureContainer.children[pictureId - 1];
if (sprite) {
  const pt = sprite.worldTransform.applyInverse(new Point(TouchInput.x, TouchInput.y));
  const ax = sprite.anchor.x * sprite.width;
  const ay = sprite.anchor.y * sprite.height;
  const alpha = sprite.bitmap.getAlphaPixel(pt.x + ax, pt.y + ay) * sprite.opacity / 255;
  console.log('Alpha of picture', pictureId, 'at touch coordinates is:', alpha);
} else
  console.log('No sprite for picture ID', pictureId);
[Edit: added anchors.]
Ah, OK!
  1. You need a sprite reference, i.e. Sprite_Picture, not an image filename.

  2. Also, you need sprite coordinates. TouchInput coordinates are screen-based, so you'll need to transform them to the sprite's coordinate system. Luckily, PIXI makes this easy!
Maybe try something like this (untested):
JavaScript:
const pictureId = 1;
const sprite = SceneManager._scene._spriteset._pictureContainer.children[pictureId - 1];
if (sprite) {
  const pt = sprite.worldTransform.applyInverse(new Point(TouchInput.x, TouchInput.y));
  const ax = sprite.anchor.x * sprite.width;
  const ay = sprite.anchor.y * sprite.height;
  const alpha = sprite.bitmap.getAlphaPixel(pt.x + ax, pt.y + ay) * sprite.opacity / 255;
  console.log('Alpha of picture', pictureId, 'at touch coordinates is:', alpha);
} else
  console.log('No sprite for picture ID', pictureId);
[Edit: added anchors.]
Worked perfectly! Thank you so much. 😀 Is it okay if I put you into the credits (under the category "Thanks to...")? If I should use a different name just let me know.

PS: I make a little game with many "arcade games", as a little showcase.
Great, you're welcome! 😂

And yes, you can credit me as caethyril or Caethyril (big or small C), I don't mind which. 👋

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-check-if-xy-of-picture-is-transparent-via-js.149346/