关键词搜索

源码搜索 ×
×

JavaScript之类操作:HTML5 canvas多分屏示例

发布2018-09-20浏览4994次

详情内容

HTML5中用户播放视频的方式:Canvas可以渲染图像播放,Video可以播放视频源数据。这里通过JavaScript的类来写一个多分屏的画布效果,Video标签的类似。目标要求:(1)实现canvas对象的选择边框效果(2)实现多分屏的切换,支持1、4、6、9、10、16等分屏。

实现源码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <script type="text/javascript">
  7. class UILayout
  8. {
  9. static SetScreenNumber(num) {
  10. UILayout.ScreenNumber = num;
  11. }
  12. static GetScreenNumber() {
  13. return UILayout.ScreenNumber;
  14. }
  15. static SetContainer(id) {
  16. UILayout.Container = document.getElementById(id);
  17. }
  18. static GetContainer() {
  19. return UILayout.Container;
  20. }
  21. static SetVideoWith(VideoWidth) {
  22. UILayout.VideoWidth = VideoWidth;
  23. }
  24. static GetVideoWith() {
  25. return UILayout.VideoWidth ;
  26. }
  27. static SetVideoHeight(VideoHeight) {
  28. UILayout.VideoHeight = VideoHeight;
  29. }
  30. static GetVideoHeight() {
  31. return UILayout.VideoHeight ;
  32. }
  33. static SetSelectVideoIndex(index) {
  34. UILayout.SelectVideoIndex = index;
  35. }
  36. static GetSelectVideoIndex() {
  37. return UILayout.SelectVideoIndex;
  38. }
  39. static Init(id,screenNums)
  40. {
  41. UILayout.SetContainer(id);
  42. UILayout.SetScreenNumber(screenNums);
  43. UILayout.SetVideoWith(352);
  44. UILayout.SetVideoHeight(288);
  45. UILayout.SetSelectVideoIndex(-1);
  46. UILayout.CreateCanvas();
  47. UILayout.LayoutScreens(screenNums);
  48. }
  49. static CreateCanvas() {
  50. for (var i = 1; i <= 16; i++) {
  51. //显示画布
  52. var canvas = document.createElement('canvas');
  53. canvas.width = UILayout.GetVideoWith();
  54. canvas.height = UILayout.GetVideoHeight();
  55. canvas.style.border = "1px solid black";
  56. canvas.style.cssFloat = "left";
  57. this.Container.append(canvas);
  58. var ctx = canvas.getContext('2d');
  59. ctx.fillStyle = "gray";
  60. ctx.fillRect(1, 1, canvas.width, canvas.height);
  61. }
  62. }
  63. static ContainsScreen(num) {
  64. var screens = [1, 4, 6, 9, 10, 16];
  65. for (var i = 0; i < screens.length; i++) {
  66. if (screens[i] == num) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. static LayoutScreens(num) {
  73. if (num == undefined) {
  74. console.log("LayoutScreens num is undefined");
  75. } else if (!UILayout.ContainsScreen(num)) {
  76. console.log("LayoutScreens num is not in [1, 4, 6, 9, 10, 16]");
  77. return;
  78. } else {
  79. this.ScreenNumber = num;
  80. }
  81. for (var i = 1; i <= this.Container.childElementCount; i++) {
  82. var video = this.Container.childNodes.item(i);
  83. video.index = i;
  84. video.style.margin = "1px";
  85. video.parentContainer = this.Container;
  86. video.onclick = function () {
  87. UILayout.SelectVideoIndex = this.index;
  88. alert(UILayout.SelectVideoIndex);
  89. for (var i = 1; i <= this.parentContainer.childElementCount; i++) {
  90. if (i === UILayout.SelectVideoIndex) {
  91. this.style.border = "1px solid #00FF00";
  92. } else {
  93. this.parentContainer.childNodes.item(i).style.border = "1px solid black";
  94. }
  95. }
  96. };
  97. if (this.ScreenNumber < i) {
  98. video.style.display = "none";
  99. } else {
  100. video.style.display = "block";
  101. }
  102. }
  103. var width = parseInt(this.Container.style.width.replace("px", ""));
  104. var height = parseInt(this.Container.style.height.replace("px", ""));
  105. var count = 0;
  106. for (var i = 1; i <= this.Container.childElementCount; i++) {
  107. var video = this.Container.childNodes.item(i);
  108. if (this.ScreenNumber == 1 && video.index == 1) {
  109. video.style.width = (width - 5)+"px";
  110. video.style.height = (height-5)+"px";
  111. count++;
  112. } else if (this.ScreenNumber == 4 && video.index <=4) {
  113. video.style.width = (width/2 -5) + "px";
  114. video.style.height =(height / 2-5) + "px";
  115. count++;
  116. } else if (this.ScreenNumber == 6 && video.index <= 6) {
  117. video.style.width = (width / 2-5) + "px";
  118. video.style.height = (height / 3-5) + "px";
  119. count++;
  120. } else if (this.ScreenNumber == 9 && video.index <= 9) {
  121. video.style.width = (width / 3-5) + "px";
  122. video.style.height = (height / 3-5) + "px";
  123. count++;
  124. } else if (this.ScreenNumber == 10 && video.index <= 10) {
  125. if (video.index==1) {
  126. video.style.width = (width / 5 *4- 5) + "px";
  127. video.style.height = (height /5 *4 - 10) + "px";
  128. } else {
  129. video.style.width = (width / 5 - 5) + "px";
  130. video.style.height = (height / 5 - 5) + "px";
  131. }
  132. if (video.index == 10)
  133. {
  134. video.style.cssFloat = "right";
  135. video.style.marginRight = "3px";
  136. }
  137. count++;
  138. }
  139. else if (this.ScreenNumber == 16 && video.index <= 16) {
  140. video.style.width =( width / 4-5) + "px";
  141. video.style.height = (height / 4 - 5) + "px";
  142. video.style.cssFloat = "left";
  143. video.style.margin = "1px";
  144. count++;
  145. }
  146. if (count == this.ScreenNumber )
  147. {
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. window.onload = function ()
  154. {
  155. UILayout.Init("player",4);
  156. }
  157. function Screens(num)
  158. {
  159. UILayout.LayoutScreens(num);
  160. }
  161. </script>
  162. </head>
  163. <body>
  164. <label for="cmbScreenNumbers">分屏数量:</label>
  165. <select id="cmbScreenNumbers" onchange="Screens(this.value)">
  166. <option value="1">1=1x1</option>
  167. <option value="4" selected="selected">4=2x2</option>
  168. <option value="6">6=2x3</option>
  169. <option value="9">9=3x3</option>
  170. <option value="10">10=1+9</option>
  171. <option value="16">16=4x4</option>
  172. </select>
  173. <div id="player" style="width:1000px;height:400px;border:1px solid #00ffff">
  174. </div>
  175. </body>
  176. </html>

效果展示

1分屏:

4分屏:

6分屏:

9分屏:

10分屏:

16分屏:

html5 canvas:http://www.runoob.com/html/html5-canvas.html

真实视频播放

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载