|
@@ -1,14 +1,20 @@
|
1
|
1
|
import React, { Component } from 'react';
|
2
|
2
|
import { connect } from 'dva'
|
3
|
|
-import { Row, Col } from 'antd';
|
4
|
|
-import './index.less'
|
|
3
|
+import './index.css'
|
|
4
|
+const NODE_ENV = process.env.NODE_ENV;
|
|
5
|
+const videoUrl = NODE_ENV === 'development' ? '/api/video' : 'http://hujiahua.site:8004/api/video'
|
5
|
6
|
class Home extends Component {
|
6
|
|
- state = {
|
7
|
|
- videoSrc: ''
|
8
|
|
- }
|
9
|
7
|
canvasVideoRef;
|
10
|
8
|
videoRef;
|
11
|
|
-
|
|
9
|
+ state = {
|
|
10
|
+ showLoading: true
|
|
11
|
+ }
|
|
12
|
+ componentDidMount () {
|
|
13
|
+ this.videoRef.addEventListener('canplay',() => {
|
|
14
|
+ console.log('canplay')
|
|
15
|
+ this.play()
|
|
16
|
+ })
|
|
17
|
+ }
|
12
|
18
|
changeState = (key, value, callback = () => {}) => {
|
13
|
19
|
this.setState({
|
14
|
20
|
[key]: value
|
|
@@ -29,6 +35,12 @@ class Home extends Component {
|
29
|
35
|
}
|
30
|
36
|
}
|
31
|
37
|
getVideoSizeAndDrawPosi = () => {
|
|
38
|
+ if(!this.videoRef)return {
|
|
39
|
+ videoWid: 0,
|
|
40
|
+ videoHei: 0,
|
|
41
|
+ drawY: 0,
|
|
42
|
+ drawX: 0
|
|
43
|
+ }
|
32
|
44
|
const { winHeight, winWidth } = this.getWindowSize()
|
33
|
45
|
let offsetWid = this.videoRef.offsetWidth;
|
34
|
46
|
let offsetHei = this.videoRef.offsetHeight;
|
|
@@ -45,6 +57,7 @@ class Home extends Component {
|
45
|
57
|
}
|
46
|
58
|
}
|
47
|
59
|
drawVideo = (target, targetWid = 300, targetHei = 300, drawX, drawY) => {
|
|
60
|
+ if (!targetWid || !targetHei) return
|
48
|
61
|
let cxt = this.canvasVideoRef.getContext('2d');
|
49
|
62
|
cxt.drawImage(target, 0, 0, targetWid, targetHei)
|
50
|
63
|
const { data } = cxt.getImageData(0, 0, targetWid, targetHei);
|
|
@@ -59,6 +72,7 @@ class Home extends Component {
|
59
|
72
|
const b = data[index + 2];
|
60
|
73
|
const gray = .299 * r + .587 * g + .114 * b;
|
61
|
74
|
cxt.fillText(this.img2Text(gray), _w, _h + 8);
|
|
75
|
+ cxt.fillStyle = `rgba(${r},${g},${b})`
|
62
|
76
|
}
|
63
|
77
|
}
|
64
|
78
|
}
|
|
@@ -67,31 +81,46 @@ class Home extends Component {
|
67
|
81
|
const i = g % 16 === 0 ? parseInt(g / 16) - 1 : parseInt(g / 16);
|
68
|
82
|
return ['#', '&', '@', '%', '$', 'w', '*', '+', 'o', '?', '!', ';', '^', ',', '.', ' '][i];
|
69
|
83
|
}
|
70
|
|
- onChangeInput = (e) => {
|
71
|
|
- let file = e.target.files[0];
|
72
|
|
- if (file) {
|
73
|
|
- // this.setState({ videoSrc: 'http://hujiahua.site:9999/test/testVideo.mp4' }, () => {
|
74
|
|
- this.setState({ videoSrc: URL.createObjectURL(file) }, () => {
|
75
|
|
- this.videoRef.addEventListener('play',() => {
|
76
|
|
- let { videoWid, videoHei, drawX, drawY } = this.getVideoSizeAndDrawPosi()
|
77
|
|
- let play = () => {
|
78
|
|
- this.drawVideo(this.videoRef, videoWid, videoHei, drawX, drawY)
|
79
|
|
- requestAnimationFrame(play);
|
80
|
|
- };
|
81
|
|
- play();
|
82
|
|
- }, false)
|
|
84
|
+ play = () => {
|
|
85
|
+ this.videoRef.addEventListener('play',() => {
|
|
86
|
+ this.setState({ showLoading: false }, () => {
|
|
87
|
+ let play = () => {
|
|
88
|
+ let { videoWid, videoHei, drawX, drawY } = this.getVideoSizeAndDrawPosi()
|
|
89
|
+ console.log('this.getVideoSizeAndDrawPosi()',this.getVideoSizeAndDrawPosi())
|
|
90
|
+ this.drawVideo(this.videoRef, videoWid, videoHei, drawX, drawY)
|
|
91
|
+ requestAnimationFrame(play);
|
|
92
|
+ };
|
|
93
|
+ play();
|
83
|
94
|
})
|
84
|
|
- }
|
|
95
|
+ }, false)
|
85
|
96
|
}
|
86
|
97
|
render () {
|
87
|
|
- const { videoSrc } = this.state
|
88
|
|
- const { winHeight, winWidth} = this.getWindowSize()
|
|
98
|
+ const { winHeight, winWidth} = this.getWindowSize();
|
|
99
|
+ const { showLoading } = this.state
|
|
100
|
+ let { videoWid, videoHei, drawX, drawY } = this.getVideoSizeAndDrawPosi()
|
89
|
101
|
return (
|
90
|
|
- <div className='wrap'>
|
91
|
|
- <input type="file" id="inputFile" accept=".mp4" onChange={this.onChangeInput} className='input' />
|
92
|
|
- <canvas ref={(ref) => { this.canvasVideoRef = ref }} id="canvasShow" width={winWidth} height={winHeight} className='canvas'/>
|
93
|
|
- <video id="video" ref={(ref) => { this.videoRef = ref }} autoPlay src={videoSrc} className='video' loop></video>
|
94
|
|
- </div>
|
|
102
|
+ <React.Fragment>
|
|
103
|
+ <div className='wrap'>
|
|
104
|
+ <canvas ref={(ref) => { this.canvasVideoRef = ref }} id="canvasShow" width={winWidth} height={winHeight} className='canvas'/>
|
|
105
|
+ <video
|
|
106
|
+ id="video"
|
|
107
|
+ crossOrigin="*"
|
|
108
|
+ ref={(ref) => { this.videoRef = ref }}
|
|
109
|
+ autoPlay src={videoUrl}
|
|
110
|
+ className='video'
|
|
111
|
+ width={videoWid && videoWid > winWidth ? winWidth: '100%'}
|
|
112
|
+ height={videoHei && videoHei > winHeight ? winHeight : '100%'}
|
|
113
|
+ loop/>
|
|
114
|
+ </div>
|
|
115
|
+ {
|
|
116
|
+ showLoading && (
|
|
117
|
+ <div className='loading'>
|
|
118
|
+ 加载中,请稍候...
|
|
119
|
+ </div>
|
|
120
|
+ )
|
|
121
|
+ }
|
|
122
|
+ </React.Fragment>
|
|
123
|
+
|
95
|
124
|
)
|
96
|
125
|
}
|
97
|
126
|
}
|