Zoom function for YouTube

Add zoom function to YouTube video player.

目前为 2016-04-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Zoom function for YouTube
  3. // @name:ja YouTubeで動画をズーム
  4. // @description Add zoom function to YouTube video player.
  5. // @description:ja YouTubeの動画プレイヤーにズーム機能を追加します。
  6. // @namespace https://twitter.com/sititou70
  7. // @include /https?:\/\/www\.youtube\.com\/watch\?v=.+/
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
  9. // @version 1
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. jQuery.noConflict();
  14. (function($){
  15. //function
  16. var video_scaleup = function(select_rect){
  17. var player_aspect_ratio = $("#movie_player").width() / $("#movie_player").height();
  18. var select_aspect_ratio = select_rect.width / select_rect.height;
  19. var centering_offset = {
  20. x: 0,
  21. y: 0
  22. }
  23. if(player_aspect_ratio < select_aspect_ratio){
  24. //fit width
  25. var scale_up_ratio = $("#movie_player").width() / select_rect.width;
  26. centering_offset.y = ($("#movie_player").height() - select_rect.height * scale_up_ratio) / 2;
  27. }else{
  28. //fit height
  29. var scale_up_ratio = $("#movie_player").height() / select_rect.height;
  30. centering_offset.x = ($("#movie_player").width() - select_rect.width * scale_up_ratio) / 2;
  31. }
  32. $("video").animate({
  33. top: select_rect.y * scale_up_ratio * -1 + centering_offset.y + "px",
  34. left: select_rect.x * scale_up_ratio * -1 + centering_offset.x + "px",
  35. width: $("video").width() * scale_up_ratio,
  36. height: $("video").height() * scale_up_ratio
  37. }, 500);
  38. now_scale_up = true;
  39. }
  40. var video_scaledown = function(){
  41. $("video").animate({
  42. top: save_css.top,
  43. left: save_css.left,
  44. width: save_css.width,
  45. height: save_css.height
  46. }, 500);
  47. now_scale_up = false;
  48. }
  49. //set event handler
  50. var now_scale_up = false;
  51. var mousedown_offset = null;
  52. var save_css = null;
  53. $("video").mousedown(function(e){
  54. mousedown_offset = {offsetX: e.offsetX, offsetY: e.offsetY};
  55. if(!now_scale_up){
  56. save_css = {
  57. top: $("video").css("top"),
  58. left: $("video").css("left"),
  59. width: $("video").css("width"),
  60. height: $("video").css("height")
  61. }
  62. }
  63. });
  64. $("video").mouseup(function(e){
  65. var select_rect = {
  66. width: Math.abs(e.offsetX - mousedown_offset.offsetX),
  67. height: Math.abs(e.offsetY - mousedown_offset.offsetY),
  68. x: Math.min(mousedown_offset.offsetX, e.offsetX),
  69. y: Math.min(mousedown_offset.offsetY, e.offsetY)
  70. }
  71. if(select_rect.width <= 10 || select_rect.height <= 10){
  72. if(now_scale_up){
  73. video_scaledown();
  74. }
  75. }else{
  76. if(!now_scale_up){
  77. video_scaleup(select_rect);
  78. }
  79. }
  80. });
  81. $(document).keydown(function(key){
  82. if(key.keyCode == 82){
  83. video_scaledown();
  84. }
  85. });
  86. })(jQuery);