javascript 로 css rotate 의 현재 각도를 가져오는 함수 > IT 기술백서

IT 기술백서

직접 알아내거나 검색하기 귀찮아서 모아 둔 것

JavaScript | javascript 로 css rotate 의 현재 각도를 가져오는 함수

본문

[code]

getRotationDegrees() {

  // get the computed style object for the element

  const style = window.getComputedStyle(this.$refs.needle)

  // this string will be in the form 'matrix(a, b, c, d, tx, ty)'

  const transformString =

style['-webkit-transform'] || style['-moz-transform'] || style.transform

  if (!transformString || transformString === 'none') return 0

  const splits = transformString.split(',')

  // parse the string to get a and b

  const parenLoc = splits[0].indexOf('(')

  const a = parseFloat(splits[0].substr(parenLoc + 1))

  const b = parseFloat(splits[1])

  // doing atan2 on b, a will give you the angle in radians

  const rad = Math.atan2(b, a)

  let deg = (180 * rad) / Math.PI

  // instead of having values from -180 to 180, get 0 to 360

  if (deg < 0) deg += 360

  return deg

}

[/code]

 

출처: https://stackoverflow.com/questions/3336101/css3-animation-on-transform-rotate-way-to-fetch-current-deg-of-the-rotating-el

댓글 0개

등록된 댓글이 없습니다.

Menu