-
Notifications
You must be signed in to change notification settings - Fork 0
/
tv.html
211 lines (187 loc) · 6.64 KB
/
tv.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html >
<!--
By Dave Lawrence on 30 Nov 2009
Animation code - demo with CanvasPixelArray, masking and animation
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
html,body { width:100%; height:100%; marginRatio:0; padding:0; overflow:hidden; }
</style>
<title>TV demo</title>
<script type="text/javascript">
var width = window.innerWidth;
var height = window.innerHeight * .8;
var fps = 6;
var tv;
var context;
var picture;
// write out canvas as we have to put width/height inline in the HTML, CSS will just scale pixels
document.write('<canvas id="canvas" width=' + width + ' height=' + height + '></canvas>');
function setup() {
context = document.getElementById("canvas").getContext("2d");
context.fillStyle = "#FFFFFF";
context.strokeStyle = "#000000";
context.fillRect(0,0, width, height);
tv = new TV(context, width/5);
tv.x = width/2;
tv.y = height/2;
setInterval(draw, 1000 / fps);
picture = document.getElementById("hidden-image");
}
function updateImage() {
var imgAddress = document.getElementById("image-address").value;
picture.src = imgAddress;
}
function draw() {
tv.draw(context);
}
function TV(ctx, w) {
this.x = 0;
this.y = 0;
this.width = w;
this.marginRatio = 0.05;
this.panelRatio = 0.15;
this.aspectRatio = 8/5;
this.screenWidth = w * (1 - this.marginRatio * 3 - this.panelRatio);
this.screenHeight = this.screenWidth / this.aspectRatio;
this.screenXOffset = -w / 2 + this.marginRatio * w + this.screenWidth / 2;
this.height = (w * this.marginRatio * 2 + this.screenWidth) / this.aspectRatio;
this.pixels = new Pixels(ctx, this.screenWidth, this.screenHeight);
this.draw = function(context) {
context.save();
context.translate(this.x, this.y);
this.drawFrame(context);
context.translate(this.screenXOffset, 0);
this.drawScreenBg(context);
this.drawClip(context);
if (Math.random() < .3 && picture.width > 0) {
this.drawPicture(context);
} else {
this.drawStatic(context);
}
context.restore();
}
this.drawFrame = function(context) {
context.save();
var hw = this.width/2;
var hh = this.height/2
// draw outside of TV
context.fillStyle = "#760c0c";
context.beginPath();
context.rect(-hw, -hh, this.width, this.height);
context.fill();
context.stroke();
var panelMidpoint = this.width/2 - this.width * (this.marginRatio + this.panelRatio/2);
var panelWidth = this.panelRatio * this.width;
// antenna
var antennaX = panelMidpoint;
var antennaY = this.height * .8;
context.beginPath();
context.moveTo(antennaX, -hh);
context.lineTo(antennaX - this.width / 20, -hh - antennaY);
context.moveTo(antennaX, -hh);
context.lineTo(antennaX + this.width / 20, -hh - antennaY);
context.stroke();
var hsw = this.screenWidth/2;
var hsh = this.screenHeight/2
// panel
context.fillStyle = "#a98666";
context.beginPath();
context.rect(panelMidpoint - panelWidth/2, -hsh, panelWidth, this.screenHeight);
context.fill();
context.stroke();
context.fillStyle = "#000000";
context.beginPath();
context.arc(panelMidpoint, -hsh * .6, panelWidth * .3, 0, 2 * Math.PI, true);
context.arc(panelMidpoint, -hsh * .1, panelWidth * .3, 0, 2 * Math.PI, true);
context.fill();
context.restore();
}
this.drawScreenBg = function(context) {
var hsw = this.screenWidth/2;
var hsh = this.screenHeight/2
context.save();
// draw grey screen bit
context.fillStyle = "#e1e1e1";
context.beginPath();
context.rect(-hsw, -hsh, this.screenWidth, this.screenHeight);
context.fill();
context.stroke();
context.strokeStyle = "#aaaaaa";
context.beginPath();
context.moveTo(-hsw, -hsh);
context.lineTo(hsw, hsh);
context.stroke();
context.moveTo(-hsw, hsh);
context.lineTo(hsw, -hsh);
context.stroke();
context.restore();
}
this.drawClip = function(context) {
var marginRatio = 2;
var curve = 15;
var hw = this.screenWidth/2 - curve/2 - marginRatio;
var hh = this.screenHeight/2 - curve/2 - marginRatio;
context.beginPath();
context.moveTo(-hw, -hh);
context.bezierCurveTo(-hw, -hh, 0, -hh - curve, hw, -hh);
context.bezierCurveTo(hw, -hh, hw + curve * 1/this.aspectRatio, 0, hw, hh);
context.bezierCurveTo(hw, hh, 0, hh + curve, -hw, hh);
context.bezierCurveTo(-hw, hh, -hw - curve * 1/this.aspectRatio, 0, -hw, -hh);
//context.stroke();
context.clip();
}
this.drawPicture = function(context) {
var hsw = this.screenWidth/2;
var hsh = this.screenHeight/2
context.save();
context.drawImage(picture, -hsw, -hsh, this.screenWidth, this.screenHeight);
context.restore();
}
this.drawStatic = function(context) {
for (var y=0 ; y<this.pixels.height ; ++y) {
for (var x=0 ; x<this.pixels.width ; ++x) {
var grey = Math.random() * 255;
this.pixels.color.r = grey;
this.pixels.color.g = grey;
this.pixels.color.b = grey;
this.pixels.putPixel(x, y);
}
}
this.pixels.draw(context, this.x + this.screenXOffset, this.y);
}
}
function Pixels(context, w,h) {
this.image = context.getImageData(0, 0, w, h);
this.width = w;
this.height = h;
this.color = new Object();
this.color.r = 255;
this.color.g = 255;
this.color.b = 255;
this.color.a = 255;
this.putPixel = function(x,y) {
var p = (y * this.image.width + x) * 4;
this.image.data[p] = this.color.r;
this.image.data[p+1] = this.color.g;
this.image.data[p+2] = this.color.b;
this.image.data[p+3] = this.color.a;
}
this.draw = function(context, x, y) {
context.putImageData(this.image, x - this.width/2, y - this.height/2);
}
}
function rand(x) {
return Math.random() * x;
}
function vary(x, variance) {
return x + variance - 2 * rand(variance);
}
</script>
</head>
<body onload="setup()">
Image address: <input id="image-address" /> <input type="button" value="change" onclick="javascript:updateImage()" />
<img id="hidden-image" style="display:none" src="pig.jpg">
</body>
</html>