JavaScript if 参数,忽略图像的透明度

发布于 2024-12-27 07:45:35 字数 2420 浏览 0 评论 0原文

我需要使 if 参数可选。如果这是有道理的话。

如果您发现我的两个函数 98% 相同,那么我需要将这种差异转化为参数,但它对我来说并不合适。

getElement(x, y, class)

其中新参数类更改了我在代码中标记为 //HERE 的内容,

//get the element under the mouse, ignoring all transparency.
function getElement(x, y){
  var elem = document.elementFromPoint(x, y);
  if($(elem).parents("#game").length != 1){ return -1; }
  var pixelAlpha = mousePixelOpacity(elem, x, y);
  var track = Array;
  for(var i = 0;i<50;i++){
    if($(elem).attr("id") == "game"){ return -1;}
    if(pixelAlpha == 0){ /////////////////////////////////////////HERE
      track[i] = elem;
      for(var z = 0; z<i+1; z++){ //hide elements
        $(track[z]).hide();
      }
      elem = document.elementFromPoint(x, y); //set the element right under the mouse.
      pixelAlpha = mousePixelOpacity(elem, x, y);
      for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
        $(track[z]).show();
      }
    }
    if(pixelAlpha != 0){ ///////////////////////////////////////// AND HERE
      return elem;
    }
  }
  return -1;
}

//get the tile under the mouse, even if it's behind an object
function getTile(x, y){
  var elem = document.elementFromPoint(x, y);
  if($(elem).parents("#game").length != 1 && $(elem).attr("id") != "tileHighlight"){ return -1; }
  var pixelAlpha = mousePixelOpacity(elem, x, y);
  var track = Array;
  for(var i = 0;i<50;i++){
    if($(elem).attr("id") == "game"){ return -1;}
    if(pixelAlpha == 0 || $(elem).attr('class') != "tile"){ /////////HERE
      track[i] = elem;
      for(var z = 0; z<i+1; z++){ //hide elements
        $(track[z]).hide();
      }
      elem = document.elementFromPoint(x, y); //set the element right under the mouse.
      pixelAlpha = mousePixelOpacity(elem, x, y);
      for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
        $(track[z]).show();
      }
    }
    if($(elem).attr('class') == "tile" && pixelAlpha != 0){ ///// AND HERE
      return elem;
    }
  }
  return -1;
}

我在想类似

getElement(x, y, "title") ;

//(This can be right) OR (both of these can be right.)

if( (pixelAlpha == 0) || (class="tile"  &&  onlycountclassifIsaidsovar) ){}

顺便说一下,如果你想的话,我在 http://untitled.servegame.com看看这只小狗的行动。

谢谢!

I need to make if parameters optional. If that makes sense.

If you notice both of my functions are 98% the same, I need to turn this difference into a parameter and it's just not clicking for me.

getElement(x, y, class)

where the new parameter class changes what I have marked as //HERE in my code

//get the element under the mouse, ignoring all transparency.
function getElement(x, y){
  var elem = document.elementFromPoint(x, y);
  if($(elem).parents("#game").length != 1){ return -1; }
  var pixelAlpha = mousePixelOpacity(elem, x, y);
  var track = Array;
  for(var i = 0;i<50;i++){
    if($(elem).attr("id") == "game"){ return -1;}
    if(pixelAlpha == 0){ /////////////////////////////////////////HERE
      track[i] = elem;
      for(var z = 0; z<i+1; z++){ //hide elements
        $(track[z]).hide();
      }
      elem = document.elementFromPoint(x, y); //set the element right under the mouse.
      pixelAlpha = mousePixelOpacity(elem, x, y);
      for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
        $(track[z]).show();
      }
    }
    if(pixelAlpha != 0){ ///////////////////////////////////////// AND HERE
      return elem;
    }
  }
  return -1;
}

//get the tile under the mouse, even if it's behind an object
function getTile(x, y){
  var elem = document.elementFromPoint(x, y);
  if($(elem).parents("#game").length != 1 && $(elem).attr("id") != "tileHighlight"){ return -1; }
  var pixelAlpha = mousePixelOpacity(elem, x, y);
  var track = Array;
  for(var i = 0;i<50;i++){
    if($(elem).attr("id") == "game"){ return -1;}
    if(pixelAlpha == 0 || $(elem).attr('class') != "tile"){ /////////HERE
      track[i] = elem;
      for(var z = 0; z<i+1; z++){ //hide elements
        $(track[z]).hide();
      }
      elem = document.elementFromPoint(x, y); //set the element right under the mouse.
      pixelAlpha = mousePixelOpacity(elem, x, y);
      for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
        $(track[z]).show();
      }
    }
    if($(elem).attr('class') == "tile" && pixelAlpha != 0){ ///// AND HERE
      return elem;
    }
  }
  return -1;
}

I was thinking something like

getElement(x, y, "title");

//(This can be right) OR (both of these can be right.)

if( (pixelAlpha == 0) || (class="tile"  &&  onlycountclassifIsaidsovar) ){}

By the way I'm at http://untitled.servegame.com if you want to see this puppy in action.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-01-03 07:45:35

我看到的唯一区别是 getTile() 中的三个额外测试。您可以使用布尔第三个参数(例如“tile”),如果设置该参数将应用这些测试。

The only difference I see are three extra tests in getTile(). You could use a boolean third arg (eg, 'tile') which if set would apply those tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文