Javascript 在保持正方形大小的情况下增加大小

Javascript 在保持正方形大小的情况下增加大小,javascript,math,Javascript,Math,我需要在Javascript中以固定增量增加一个框的大小,但也要保持正方形的大小。我认为应该有一个“简单”的数学函数,但我想不出来。我用pow()和sqrt()以及长if语句做了一些尝试,但似乎没有任何效果 示例: 假设我有一个(WxH)54x45的盒子,我的增量是10,我想增加高度(用一个按钮)。正常增量为10时,高度将变为55,但我希望它首先返回值54,因此长方体变为正方形。当它达到54,我再次增加高度,它应该继续保持在55 所以我的盒子是(WxH)54x45 用户单击按钮“+”以增加高度

我需要在Javascript中以固定增量增加一个框的大小,但也要保持正方形的大小。我认为应该有一个“简单”的数学函数,但我想不出来。我用
pow()
sqrt()
以及长if语句做了一些尝试,但似乎没有任何效果

示例: 假设我有一个(WxH)54x45的盒子,我的增量是10,我想增加高度(用一个按钮)。正常增量为10时,高度将变为55,但我希望它首先返回值54,因此长方体变为正方形。当它达到54,我再次增加高度,它应该继续保持在55

  • 所以我的盒子是(WxH)54x45
  • 用户单击按钮“+”以增加高度
  • 盒子应为(宽x高)54 x 54
  • 用户再次单击按钮“+”以增加高度
  • 盒子应为(宽x高)54 x 55
  • 用户再次单击按钮“+”以增加高度
  • 盒子应为(宽x高)54 x 65
  • 等等
  • 纵横比并不重要


    我希望有人能帮我,或者至少给我指出正确的方向。谢谢

    这不是你要问的问题,但应该是这样的:

    var boxGetter = function(w, h){
        // anonymouse function setting the original width and height
        return {
            // get the new height 
            // pass in the new width
            height: function(nw){
                return (w / h) * nw;
            },
            width: function(nh){
                return (h / w) * nh;
            }
        }
    }
    
    var getter = boxGetter(54, 45);
    
    console.log(getter.height(54+10)); // 66
    console.log(getter.width(45+10)); // 45.83
    
    var step = 10, squareCase = null;
    
    function increaseWidth() {
      if (squareCase !== null) {   // we are currently at the square case
        width = squareCase + step; // resume from remembered
        squareCase = null;         // and leave the special case
      }
      else if (width < height && width + step > height) {
        squareCase = width;       // remember value so we can resume sequence
        width = height;           // do the square now
      }
      else {
        width = width + step;     // default case
      }
    }
    

    pow
    sqrt
    诸如此类的东西都帮不了你,你只需要做一个特殊的例子。大概是这样的:

    var boxGetter = function(w, h){
        // anonymouse function setting the original width and height
        return {
            // get the new height 
            // pass in the new width
            height: function(nw){
                return (w / h) * nw;
            },
            width: function(nh){
                return (h / w) * nh;
            }
        }
    }
    
    var getter = boxGetter(54, 45);
    
    console.log(getter.height(54+10)); // 66
    console.log(getter.width(45+10)); // 45.83
    
    var step = 10, squareCase = null;
    
    function increaseWidth() {
      if (squareCase !== null) {   // we are currently at the square case
        width = squareCase + step; // resume from remembered
        squareCase = null;         // and leave the special case
      }
      else if (width < height && width + step > height) {
        squareCase = width;       // remember value so we can resume sequence
        width = height;           // do the square now
      }
      else {
        width = width + step;     // default case
      }
    }
    
    var阶跃=10,squareCase=null;
    函数increaseWidth(){
    如果(squareCase!==null){//我们当前处于方形情况
    宽度=squareCase+step;//从记忆中恢复
    squareCase=null;//并保留特殊情况
    }
    否则如果(宽度<高度和宽度+台阶>高度){
    squareCase=width;//记住该值,以便恢复序列
    宽度=高度;//现在做正方形
    }
    否则{
    宽度=宽度+步长;//默认情况
    }
    }
    

    在编写相应的递减时,请记住,
    squareCase
    变量包含递增之前的值,因此它应该包含递减情况下已经递减的值。

    不清楚您要的是什么。一张照片会有帮助。您想保持一致的纵横比吗?或者,您希望放大后的物体变成正方形吗?例如,如果您将高度增加10,并且希望长方体具有相同的形状,则宽度应增加
    10*(54/45)
    即12,这意味着您将拥有一个66 x 55的长方体。然而,这并不完全正确。@Pointy我用一些额外的信息更新了我的问题,我希望现在问题清楚了。谢谢谢谢MvG,你的代码为我指明了正确的方向。我已经用你的方法实现了。谢谢大家和我一起思考!