Skip to content

Commit 4e0d1f7

Browse files
committed
Merge branch 'joelrbrandt-new-web-worker-demo'
2 parents 61fe0b9 + f07a64c commit 4e0d1f7

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

demos/worker.html

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
<html>
2-
<head>
3-
<title>Worker</title>
4-
<style>
5-
body {
6-
font-family: sans-serif;
7-
}
8-
9-
#status {
10-
height: 200px;
11-
max-height: 200px;
12-
border: thin solid black;
13-
overflow-y: scroll;
14-
}
15-
16-
#square {
17-
position: absolute;
18-
left: 0px;
19-
top: 0px;
20-
width: 75px;
21-
height: 75px;
22-
background-color: rgba(0, 0, 220, 0.3);
23-
z-index: -1;
24-
}
25-
</style>
26-
</head>
27-
<body>
28-
<h1>Web Worker Demo</h1>
29-
<p>Works in Chrome, Safari, and Firefox. Web worker portion works in Opera.</p>
30-
<p>Use arrow keys to change the direction of the animated square. The square is animated with <em>requestAnimationFrame</em>.</p>
31-
<p>Click the button below to start or stop the worker.</p>
32-
<div><input type="button" value="start worker" id="toggleWorker" /></div>
33-
<h2>Messages from Worker:</h2>
34-
<div id="status"></div>
35-
<div id="square"></div>
36-
<script src="../js/worker-main.js"></script>
37-
</body>
38-
</html>
1+
<title>Web Worker</title>
2+
<style>
3+
body {
4+
font-family: sans-serif;
5+
}
6+
7+
#status {
8+
height: 200px;
9+
max-height: 200px;
10+
border: thin solid #aaa;
11+
overflow-y: scroll;
12+
}
13+
14+
#animationWrapper {
15+
position: relative;
16+
height: 50px;
17+
}
18+
19+
#square {
20+
position: absolute;
21+
left: 0px;
22+
top: 0px;
23+
width: 50px;
24+
height: 50px;
25+
background-color: rgba(0, 0, 220, 0.3);
26+
}
27+
</style>
28+
<article>
29+
<p>This demo shows how main window animation isn't interrupted by Web Workers. <small>Note that the animation does not work in Opera (due to lack of requestAnimationFrame support).</small></p>
30+
<p>Use arrow keys to change the direction of the animated square. The square is animated with <em>requestAnimationFrame</em>.</p>
31+
<div id="animationWrapper">
32+
<div id="square"></div>
33+
</div>
34+
<p>Click the button below to start or stop the worker.</p>
35+
<div><input type="button" value="start worker" id="toggleWorker" /></div>
36+
<h2>Messages from Worker:</h2>
37+
<div id="status"></div>
38+
<script src="/js/worker-main.js"></script>
39+
</article>

js/worker-main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(function () {
55
"use strict";
66

7-
var SQUARE_SIZE = 75;
7+
var SQUARE_SIZE = 50;
88
var MOVEMENT_STEP = 3;
99

1010
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
@@ -52,26 +52,26 @@
5252
var top = parseInt(square.style.top, 10);
5353
var right = left + SQUARE_SIZE;
5454
var bottom = top + SQUARE_SIZE;
55-
55+
5656
switch (direction) {
5757
case 37: // left
5858
if (left > 0) {
59-
square.style.left = left - MOVEMENT_STEP;
59+
square.style.left = left - MOVEMENT_STEP + 'px';
6060
}
6161
break;
6262
case 38: // up
6363
if (top > 0) {
64-
square.style.top = top - MOVEMENT_STEP;
64+
square.style.top = top - MOVEMENT_STEP + 'px';
6565
}
6666
break;
6767
case 39: //right
6868
if (right < document.documentElement.clientWidth) {
69-
square.style.left = left + MOVEMENT_STEP;
69+
square.style.left = left + MOVEMENT_STEP + 'px';
7070
}
7171
break;
7272
case 40: // down
7373
if (bottom < document.documentElement.clientHeight) {
74-
square.style.top = top + MOVEMENT_STEP;
74+
square.style.top = top + MOVEMENT_STEP + 'px';
7575
}
7676
break;
7777
default:

0 commit comments

Comments
 (0)