JavaScript | javascript 로 css rotate 의 현재 각도를 가져오는 함수
페이지 정보
- 작성자:
- 핵탐
- 작성일
- 10.03 17:45
- 조회
- 3,645
- 댓글
- 0
본문
[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]
댓글 0개
등록된 댓글이 없습니다.