Zoom function for YouTube

Add zoom function to YouTube video player.

当前为 2016-09-23 提交的版本,查看 最新版本

  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.4
  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. var scale_up_ratio = null;
  24. if(player_aspect_ratio < select_aspect_ratio){
  25. //fit width
  26. scale_up_ratio = $("#movie_player").width() / select_rect.width;
  27. centering_offset.y = ($("#movie_player").height() - select_rect.height * scale_up_ratio) / 2;
  28. }else{
  29. //fit height
  30. scale_up_ratio = $("#movie_player").height() / select_rect.height;
  31. centering_offset.x = ($("#movie_player").width() - select_rect.width * scale_up_ratio) / 2;
  32. }
  33. $("video").stop().animate({
  34. top: select_rect.y * scale_up_ratio * -1 + centering_offset.y + "px",
  35. left: select_rect.x * scale_up_ratio * -1 + centering_offset.x + "px",
  36. width: $("video").width() * scale_up_ratio,
  37. height: $("video").height() * scale_up_ratio
  38. }, 500);
  39. now_scale_up = true;
  40. };
  41. var video_scaledown = function(){
  42. $("video").stop().animate({
  43. top: save_css.top,
  44. left: save_css.left,
  45. width: save_css.width,
  46. height: save_css.height
  47. }, 500);
  48. now_scale_up = false;
  49. };
  50. //set event handler
  51. var now_scale_up = false;
  52. var mousedown_offset = null;
  53. var save_css = null;
  54. $("video").mousedown(function(e){
  55. mousedown_offset = {offsetX: e.offsetX, offsetY: e.offsetY};
  56. if(!now_scale_up){
  57. save_css = {
  58. top: $("video").css("top"),
  59. left: $("video").css("left"),
  60. width: $("video").css("width"),
  61. height: $("video").css("height")
  62. };
  63. }
  64. });
  65. $("video").mouseup(function(e){
  66. var select_rect = {
  67. width: Math.abs(e.offsetX - mousedown_offset.offsetX),
  68. height: Math.abs(e.offsetY - mousedown_offset.offsetY),
  69. x: Math.min(mousedown_offset.offsetX, e.offsetX),
  70. y: Math.min(mousedown_offset.offsetY, e.offsetY)
  71. };
  72. if(!(select_rect.width <= 10 || select_rect.height <= 10)){
  73. video_scaleup(select_rect);
  74. return false;
  75. }
  76. return true;
  77. });
  78. var timer_id = 0;
  79. $("#player-api").mousemove(function(){
  80. $(".ytp-gradient-top").css("opacity", "1");
  81. $(".ytp-chrome-top").css("opacity", "1");
  82. $(".ytp-gradient-bottom").css("opacity", "1");
  83. $(".ytp-chrome-bottom").css("opacity", "1");
  84. clearTimeout(timer_id);
  85. timer_id = setTimeout(function(){
  86. $(".ytp-gradient-top").css("opacity", "0");
  87. $(".ytp-chrome-top").css("opacity", "0");
  88. $(".ytp-gradient-bottom").css("opacity", "0");
  89. $(".ytp-chrome-bottom").css("opacity", "0");
  90. }, 2000);
  91. }).mouseleave(function(){
  92. $(".ytp-gradient-top").css("opacity", "0");
  93. $(".ytp-chrome-top").css("opacity", "0");
  94. $(".ytp-gradient-bottom").css("opacity", "0");
  95. $(".ytp-chrome-bottom").css("opacity", "0");
  96. });
  97. $(document).keydown(function(key){
  98. switch(key.keyCode){
  99. case 82:
  100. video_scaledown();
  101. break;
  102. }
  103. });
  104. })(jQuery);