Home > Blockchain >  Is transform: scale(x) a costly operation?
Is transform: scale(x) a costly operation?

Time:10-28

I have a UI element that is designed to work best at exactly '200px' width. But if the size of the UI needs to change based on window / media size, it would be a pain to have to adjust this Component bit by bit and change the width away from 200px.

Is changing the size of a component by using transform: scale(x) to adjust to window / media size an acceptable practice? Is it costly in performance? In practical reality, window / media size will be set from the beginning, so the scale function would only have to be run once anways.

if (screen width < 600px) {
  element.style.transform = "scale(.7)"
} else {
  element.style.transform = "scale(1)"
}

CodePudding user response:

  1. Is transform: scale(x) a costly operation?

No, the transform operation doesn't require a DOM update. A browser makes a rendering update. Its pros are its performance costs are low, it works fast. Its cons are scaling could greatly downgrade its visual quality (for example, blur images), the downscaled component with its content could be hard to use or read.

  1. A JS usage to set CSS is definitely a costly way to make changes. The most efficient way is to use CSS only (media query).

  2. It looks weird to 'scale' a component for a small screen. A common way to solve it now is to make a responsive layout. But think, you don't need to let its content be dynamic inside. Such a task could be solved with CSS. Just make another step forward. Tie the inner content with outer sizes at CSS - use em/rem units instead of px for all its measures. And the media query will change its base font-size only. Different size, no potential scaling visualization issues, same content position. :)

  3. The CSS could be like this below. Sorry, a minimal working HTML, CSS, JS to demonstrate the concept. To check JS in different screen sizes in this snippet - change your screen size, refresh the snippet. The CSS option works without a refresh.

function setSizeJs() {
  const element = document.getElementById('target-element-px');
  element.style.transform = window.innerWidth < 600 ? 'scale(.7)' : 'scale(1)';
}
.parent {
  font-size: 14px;
}

#target-element-px {
  /* original width */
  width: 200px;
  /* colorize element to show its size */
  background-color: green;
}

#target-element-em {
  /* main style, will be applied always */
  /* an equivalent of 'width 200px' in EMs, where EM is taken as '14px' from the 'parent' class */
  width: 14.28571428571429em;
  background-color: grey;
}

@media screen and (max-width: 600px) {
  /* additional style, will be applied only when the screen size will be less than 600px */
  /* it will overwrite the main style */
  #target-element-em {
    font-size: 0.7em;
  }
}
<div class="parent">
  <div id="target-element-px" class="target-element" onload="setSizeJs">
  Some content for original 200px width
  </div>

  <div id="target-element-em" class="target-element">
  Some content for original 200px width
  </div>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related