src.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import url from '$utils/tool/url';
  2. export default <LibMixins>{
  3. data(){
  4. return {
  5. videoSrc:'',
  6. imageSrc:'',
  7. useVideo: false
  8. }
  9. },
  10. computed:{
  11. usePoster(){
  12. if (this.baseURL) {
  13. return url.addBaseURL(this.poster,this.baseURL);
  14. } else {
  15. return this.poster;
  16. }
  17. }
  18. },
  19. videoSuffix:{
  20. 'mp4':1,
  21. 'MP4':1,
  22. 'ogg':1,
  23. 'OGG':1,
  24. 'webm':1,
  25. 'webM':1
  26. },
  27. methods:{
  28. setImageSrc(src:string,async:boolean=true){
  29. if (!src) return;
  30. if (src.indexOf('data:') === 0) {
  31. if (src.indexOf('data:video') === 0) {
  32. return this.setVideoSrc(src);
  33. }
  34. } else {
  35. let suffix:string = src.substr(src.lastIndexOf('.')+1);
  36. // 执行添加 url
  37. if (this.baseURL) {
  38. src = url.addBaseURL(src,this.baseURL);
  39. }
  40. // if (this.baseURL && src.indexOf('http') !== 0) {
  41. // src= this.baseURL + src;
  42. // }
  43. if (this.$options.videoSuffix[suffix]) {
  44. return this.setVideoSrc(src,async);
  45. }
  46. }
  47. // 如果不为视频
  48. if (this.useVideo) this.useVideo = false;
  49. if (async){
  50. return this.$nextTick(()=> this.imageSrc = src);
  51. } else {
  52. this.imageSrc = src
  53. }
  54. },
  55. setVideoSrc(src:string,async:boolean=true){
  56. if (!this.useVideo) this.useVideo = true;
  57. if (this.video || this.poster) {
  58. if (async) {
  59. return this.$nextTick(()=> this.imageSrc = src);
  60. } else {
  61. this.imageSrc = src;
  62. }
  63. } else {
  64. if (async) {
  65. return this.$nextTick(()=> this.videoSrc = src);
  66. } else {
  67. this.videoSrc = src;
  68. }
  69. }
  70. },
  71. videoLoad(){
  72. let canvas:HTMLCanvasElement = document.createElement('canvas');
  73. canvas.width = this.videoEl.videoWidth;
  74. canvas.height = this.videoEl.videoHeight;
  75. // @ts-ignore
  76. canvas.getContext('2d').drawImage(this.videoEl,0,0,canvas.width,canvas.height);
  77. try {
  78. let src:string = canvas.toDataURL('image/png');
  79. this.setImageSrc(src);
  80. } catch (e) {
  81. if (this.videoMode) {
  82. let src = this.videoSrc;
  83. this.$nextTick(()=> this.imageSrc = src)
  84. }
  85. }
  86. // @ts-ignore
  87. canvas = null;
  88. this.videoSrc = '';
  89. }
  90. }
  91. }