te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - get div corners pixel positions after rotation - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get div corners pixel positions after rotation - Stack Overflow

programmeradmin4浏览0评论

how can i calculate top-left, bottom-left, top-right, bottom-right pixel positions of a div after you have set a rotation radian/degree on it?

An example would be helpful.

how can i calculate top-left, bottom-left, top-right, bottom-right pixel positions of a div after you have set a rotation radian/degree on it?

An example would be helpful.

Share Improve this question edited Apr 13, 2015 at 12:18 GibboK 73.9k147 gold badges451 silver badges672 bronze badges asked Jul 18, 2014 at 10:47 RaskolnikoovRaskolnikoov 55711 silver badges27 bronze badges 5
  • you can't calculate the corners unless you know what the center of rotation was. – Alnitak Commented Jul 18, 2014 at 10:50
  • Could you build a use case (in jsfiddle or other) so we can identify your exact aim – web-tiki Commented Jul 18, 2014 at 11:05
  • 4 The answer you have accepted is pletely incorrect. – Alnitak Commented Jul 18, 2014 at 14:14
  • Are you still sure the answer you have accepted is correct? Did it work for you? – John Dvorak Commented Jul 22, 2014 at 10:24
  • I've updated my accepted answer to my own solution I got worked out. This works for me and return correct results. – Raskolnikoov Commented Jul 23, 2014 at 22:51
Add a ment  | 

3 Answers 3

Reset to default 12

Assuming rotation relative to the center and coordinates of the four corners also relative to that same origin, each point (±a, ±b) where a and b are the half-width and half-height of the div needs to be multiplied by the transformation matrix:

|  cos(theta)   -sin(theta) |
|  sin(theta)    cos(theta) |

e.g.:

x' = a * cos(theta) - b * sin(theta)
y' = a * sin(theta) + b * cos(theta)

NB: the above is for cartesian coordinates - invert the theta terms as necessary for DOM coordinates where the y axis runs downwards.

I was confused about the answers here. The down-voted answer is definitely wrong but I also struggled with the other answer because it's a very good but very pure hint. The example there is still very theoretical for a usual web developer. So I bined both answers and made a working version of the down-voted example. So for those who also search for a working javascript solution. Here it is:

function getPixelsByAngle(x, y, width, height, angle) {
   var radians = angle * Math.PI / 180;
   return [
      //upper left
      [x + width/2 + width/-2 * Math.cos(radians) - height/-2 * Math.sin(radians), y + height/2 + width/-2 * Math.sin(radians) + height/-2 * Math.cos(radians)],
      //upper right
      [x + width/2 + width/2 * Math.cos(radians) - height/-2 * Math.sin(radians), y + height/2 + width/2 * Math.sin(radians) + height/-2 * Math.cos(radians)],
      //bottom right
      [x + width/2 + width/2 * Math.cos(radians) - height/2 * Math.sin(radians), y + height/2 + width/2 * Math.sin(radians) + height/2 * Math.cos(radians)],
      //bottom left
      [x + width/2 + width/-2 * Math.cos(radians) - height/2 * Math.sin(radians), y + height/2 + width/-2 * Math.sin(radians) + height/2 * Math.cos(radians)],
   ];
}

This solved my issue with calculating new pixel positions after rotating. I created a function to be used, passing in the x/y position, half width/height of the div and the new rotation angle.

function getPixelsByAngle(x,y,halfWidth,halfHeight,angle){
   var bounds = [
      //upper left
      [x + (halfWidth) * Math.cos(angle) - (halfHeight) * Math.sin(angle) + halfWidth, y + (halfHeight) * Math.cos(angle) + (halfWidth) * Math.sin(angle) + halfHeight],
      //upper right
      [x - (halfWidth) * Math.cos(angle) - (halfHeight) * Math.sin(angle) + halfWidth, y + (halfHeight) * Math.cos(angle) - (halfWidth) * Math.sin(angle) + halfHeight],
      //bottom right
      [x - (halfWidth) * Math.cos(angle) + (halfHeight) * Math.sin(angle) + halfWidth, y - (halfHeight) * Math.cos(angle) - (halfWidth) * Math.sin(angle) + halfHeight],
      //bottom left
      [x + (halfWidth) * Math.cos(angle) + (halfHeight) * Math.sin(angle) + halfWidth, y - (halfHeight) * Math.cos(angle) + (halfWidth) * Math.sin(angle) + halfHeight]
   ];
   return bounds;
}
发布评论

评论列表(0)

  1. 暂无评论