Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating a ratio difference?
    primarykey
    data
    text
    <p>On our site we have video thumbnails, clicking the thumbnail opens up the video in a modal dialog.</p> <p>The video itself is 16:9 but the player has extra details at the bottom so isn't.</p> <p>In the dialog we have two buttons, grow, and shrink.</p> <p>The buttons grow/shrink the player in steps of (originalsize (*|/) growBy). So, for steps of 50% of the original, growBy would be 1.5.</p> <p>Anyway, this is all working great and is shown in code block #1,</p> <p>The issue I am running into is that I need to put a limitation in so that, if the dialog is asked to grow beyond the width or height of the window it will simply grow to the (in ratio) maximum size it can.</p> <p>E.g. if we are asked to grow beyond the maximum width of the window, the dialogs width (including padding) will be equal to the window width, but the height will not be (unless of course the dialog ratio happens to exactly match the window, but that's unlikely!)</p> <p>Conversely, if we are asked to grow beyond the maximum height of the window, the dialogs height (including padding) will be equal to the window height, but the height will not be (again unless the dialog ratio happens to exactly match the window). </p> <p>This is where I am getting stuck and my attempt is in code block #2, Right now I am just doing the check for too big height, but I have that working fine to do the height, the issue is that, I don't know how to work out what the width should be when the height step is adjusted. The interesting code is in Code Block pre#2.</p> <p>If my thinking is right, I need to work out, what percentage of <code>videoStep.height</code> <code>tempStep.height</code> is and then set <code>tempStep.width</code> to <code>videoStep.width+ (videoStep.width*percentage)</code>?</p> <p>But I tried doing:</p> <pre><code>var percentage = tempStep.height/videoStep.height; tempStep.width = videoStep.width+(videoStep.width*percentage); </code></pre> <p>in place of</p> <pre><code>tempStep.width = (newDialogSize.width-videoDialogSize.padding.width) - windowSize.width; </code></pre> <p>But the width seems to be enlarging too much as the videos ratio is lost (player ratio becomes 586:281)</p> <h3>Code Block pre#2</h3> <pre><code>var windowSize = { height: $(window).height(), width: $(window).width() }; useTempStep = false; var fullDialogSize = { height: newDialogSize.height+videoDialogSize.padding.height, width: newDialogSize.width+videoDialogSize.padding.width }; if(fullDialogSize.height &gt; windowSize.height){ $growButton.addClass('disabled'); tempStep.height = (newDialogSize.height-videoDialogSize.padding.height) - windowSize.height; tempStep.width = (newDialogSize.width-videoDialogSize.padding.width) - windowSize.width; useTempStep = true; newPlayerSize = sizeCalc(currentPlayerSize, tempStep, action); newDialogSize = { height: newPlayerSize.height + dialogDifference.height, width: newPlayerSize.width + dialogDifference.width //This line is not being calculated right! }; } </code></pre> <h3>Code Block #1</h3> <pre><code>function sizeCalc(currentPlayerSize, step, action){ var newPlayerSize; if (action == 'grow') { newPlayerSize = { height: currentPlayerSize.height + step.height, width: currentPlayerSize.width + step.width }; } else { newPlayerSize = { height: currentPlayerSize.height - step.height, width: currentPlayerSize.width - step.width }; } return newPlayerSize; } var $videoDialog = $('#video'); var $videoPlayer = $('#video-player_wrapper'); var $growButton = $('.resizeVideo[data-action="grow"]', $videoDialog); var $shrinkButton = $('.resizeVideo[data-action="shrink"]', $videoDialog); var growBy = 1.5; var videoPlayerSize = { height: $videoPlayer.height() }; var videoDialogSize = { height: $videoDialog.height(), width: $videoDialog.width() }; var dialogDifference = { height: videoDialogSize.height - videoPlayerSize.height, width: videoDialogSize.width - videoPlayerSize.width }; var videoStep = { height: (videoPlayerSize.height*growBy)-videoPlayerSize.height, width: (videoPlayerSize.width*growBy)-videoPlayerSize.width }; var clickHandler = function () { var button = $(this); var action = button.data('action'); var currentPlayerSize = { height: $videoPlayer.height(), width: $videoPlayer.width() }; var newPlayerSize = sizeCalc(currentPlayerSize, videoStep, action); var newDialogSize = { height: newPlayerSize.height + dialogDifference.height, width: newPlayerSize.width + dialogDifference.width }; var windowSize = { height: $(window).height(), width: $(window).width() }; var newMargin = -(newDialogSize.width / 2); $videoDialog.animate({ height: newDialogSize.height, width: newDialogSize.width, marginLeft: newMargin }); $videoPlayer.animate({ height: newPlayerSize.height, width: newPlayerSize.width }); }; $growButton.on('click', clickHandler); $shrinkButton.on('click', clickHandler); </code></pre> <h3>Code Block #2</h3> <pre><code>function sizeCalc(currentPlayerSize, step, action){ var newPlayerSize; if (action == 'grow') { newPlayerSize = { height: currentPlayerSize.height + step.height, width: currentPlayerSize.width + step.width }; } else { newPlayerSize = { height: currentPlayerSize.height - step.height, width: currentPlayerSize.width - step.width }; } return newPlayerSize; } var $videoDialog = $('#video'); var $videoPlayer = $('#video-player_wrapper'); var $growButton = $('.resizeVideo[data-action="grow"]', $videoDialog); var $shrinkButton = $('.resizeVideo[data-action="shrink"]', $videoDialog); var growBy = 1.5; var videoPlayerSize = { height: $videoPlayer.height(), width: $videoPlayer.width() }; var videoDialogSize = { height: $videoDialog.height(), width: $videoDialog.width(), padding: { height: $videoDialog.outerHeight() - $videoDialog.height(), width: $videoDialog.outerWidth() - $videoDialog.width() } }; var dialogDifference = { height: videoDialogSize.height - videoPlayerSize.height, width: videoDialogSize.width - videoPlayerSize.width }; var videoStep = { height: (videoPlayerSize.height*growBy)-videoPlayerSize.height, width: (videoPlayerSize.width*growBy)-videoPlayerSize.width }; var tempStep = { height: 0, width: 0 }; var useTempStep = false; var clickHandler = function () { var button = $(this); if(button.hasClass('disabled')){ return; } $growButton.removeClass('disabled'); var action = button.data('action'); var currentPlayerSize = { height: $videoPlayer.height(), width: $videoPlayer.width() }; var newPlayerSize = sizeCalc(currentPlayerSize, (useTempStep ? tempStep : videoStep), action); var newDialogSize = { height: newPlayerSize.height + dialogDifference.height, width: newPlayerSize.width + dialogDifference.width }; var windowSize = { height: $(window).height(), width: $(window).width() }; useTempStep = false; var fullDialogSize = { height: newDialogSize.height+videoDialogSize.padding.height, width: newDialogSize.width+videoDialogSize.padding.width }; if(fullDialogSize.height &gt; windowSize.height){ $growButton.addClass('disabled'); tempStep.height = (newDialogSize.height-videoDialogSize.padding.height) - windowSize.height; tempStep.width = (newDialogSize.width-videoDialogSize.padding.width) - windowSize.width; useTempStep = true; newPlayerSize = sizeCalc(currentPlayerSize, tempStep, action); newDialogSize = { height: newPlayerSize.height + dialogDifference.height, width: newPlayerSize.width + dialogDifference.width }; } var newMargin = -(newDialogSize.width / 2); $videoDialog.animate({ height: newDialogSize.height, width: newDialogSize.width, marginLeft: newMargin }); $videoPlayer.animate({ height: newPlayerSize.height, width: newPlayerSize.width }); }; $growButton.on('click', clickHandler); $shrinkButton.on('click', clickHandler); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload