-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoToString.js
93 lines (80 loc) · 2.92 KB
/
videoToString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(function(context){
function VideoToString(vedioId){
if(!vedioId){return alert('无法找到播放的视频!')}
this.vedio=document.getElementById(vedioId);
this.canvas = document.createElement("canvas");
this.showDiv = document.createElement("div");
this.showDiv.setAttribute('id',"showDiv");
this.canvas.style.display="none";
this.canvasContext = this.canvas.getContext('2d');
this.tiemr=null;
this.vedio.addEventListener('play',this.play.bind(this), false);
this.vedio.addEventListener('pause',this.pause.bind(this), false);
document.body.appendChild(this.canvas);
document.body.appendChild(this.showDiv);
}
VideoToString.prototype.play=function(){
var width = parseFloat(getComputedStyle(this.vedio).width),
height = parseFloat(getComputedStyle(this.vedio).height);
this.canvas.width = width;
this.canvas.height = height;
this.showDiv.style.width = width + "px";
this.showDiv.style.height = height + "px";
this.tiemr = setInterval(()=>{
this.canvasContext.drawImage(this.vedio, 0, 0, width, height);
draw(this.canvasContext,this.showDiv,width, height);
if (this.vedio.ended) {
clearInterval(this.tiemr)
}
}, 20);
}
VideoToString.prototype.pause=function(){
clearInterval(this.tiemr)
}
function draw(ctx,showDiv,width,height) {
const imgdata = ctx.getImageData(0, 0, width, height);
var imgDataArr = imgdata.data;
var imgDataWidth = imgdata.width;
var imgDataHeight = imgdata.height;
var html = '';
for (h = 0; h < imgDataHeight; h += 12) {
var p = '<p>';
for (w = 0; w < imgDataWidth; w += 6) {
var index = (w + imgDataWidth * h) * 4;
var r = imgDataArr[index + 0];
var g = imgDataArr[index + 1];
var b = imgDataArr[index + 2];
var gray = getGray(r, g, b);
p += toString(gray);
}
p += '</p>';
html += p;
}
showDiv.innerHTML = html;
}
// 根据灰度生成相应字符
function toString(g) {
if (g <= 30) {
return '8';
} else if (g > 30 && g <= 60) {
return '&';
} else if (g > 60 && g <= 120) {
return '$';
} else if (g > 120 && g <= 150) {
return '*';
} else if (g > 150 && g <= 180) {
return 'o';
} else if (g > 180 && g <= 210) {
return '!';
} else if (g > 210 && g <= 240) {
return ';';
} else {
return '.';
}
}
// 根据rgb值计算灰度
function getGray(r, g, b) {
return 0.299 * r + 0.578 * g + 0.114 * b;
}
context.VideoToString= VideoToString;
})(window);