Last active
May 31, 2019 18:15
-
-
Save vbarbarosh/4797dd119e6d42c4c229052334a80539 to your computer and use it in GitHub Desktop.
math_rotate – Rotate a point around origin https://codescreens.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- Rotate a point around origin --> | |
| <!-- https://codepen.io/vbarbarosh/pen/joXEyR --> | |
| <svg> | |
| <circle id="origin" cx="200" cy="150" r="10" /> | |
| <circle id="point" cx="175" cy="125" r="5" /> | |
| </svg> | |
| <style> | |
| svg { | |
| width: 400px; | |
| height: 300px; | |
| border: 1px dashed; | |
| } | |
| </style> | |
| <script> | |
| (function () { | |
| // http://stackoverflow.com/a/13208761/1478566 | |
| function rotate(x, y, ox, oy, deg) | |
| { | |
| const rad = deg * Math.PI / 180.0; | |
| return { | |
| x: Math.cos(rad) * (x-ox) - Math.sin(rad) * (y-oy) + ox, | |
| y: Math.sin(rad) * (x-ox) + Math.cos(rad) * (y-oy) + oy | |
| }; | |
| } | |
| let deg = 0; | |
| setInterval(function () { | |
| const {x, y} = rotate(175, 125, 200, 150, deg++); | |
| point.setAttribute('cx', x); | |
| point.setAttribute('cy', y); | |
| }, 10); | |
| })(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment