Tcl Source Code

Check-in [29517d12f7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a local copy of reference JS demo at same fps. Add an interactive tclsh page for easy timings, including toy number manipulation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | ferrieux-nacl
Files: files | file ages | folders
SHA1: 29517d12f7fe3d62b9737caac3dd17c0830e31bd
User & Date: ferrieux 2011-10-25 04:21:18
Context
2011-10-25
04:21
Add a local copy of reference JS demo at same fps. Add an interactive tclsh page for easy timings, i... Leaf check-in: 29517d12f7 user: ferrieux tags: ferrieux-nacl
2011-10-18
22:03
Streamline demo setup; update README to reflect API changes. check-in: 91aea72dbd user: ferrieux tags: ferrieux-nacl
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added nacl/demo/ballsjs.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE HTML>
<html>
    <head>
        <style>
            body {
                margin: 0px;
                padding: 0px;
            }
            
            #myCanvas {
                border: 1px solid #9C9898;
            }
        </style>
        <script src="kinetic2d-v1.0.3.js">
        </script>
        <script>
            function initBalls(){
                balls = [];
                
                var blue = "#3A5BCD";
                var red = "#EF2B36";
                var yellow = "#FFC636";
                var green = "#02A817";
                
                // G
                balls.push(new Ball(173, 63, 0, 0, blue));
                balls.push(new Ball(158, 53, 0, 0, blue));
                balls.push(new Ball(143, 52, 0, 0, blue));
                balls.push(new Ball(130, 53, 0, 0, blue));
                balls.push(new Ball(117, 58, 0, 0, blue));
                balls.push(new Ball(110, 70, 0, 0, blue));
                balls.push(new Ball(102, 82, 0, 0, blue));
                balls.push(new Ball(104, 96, 0, 0, blue));
                balls.push(new Ball(105, 107, 0, 0, blue));
                balls.push(new Ball(110, 120, 0, 0, blue));
                balls.push(new Ball(124, 130, 0, 0, blue));
                balls.push(new Ball(139, 136, 0, 0, blue));
                balls.push(new Ball(152, 136, 0, 0, blue));
                balls.push(new Ball(166, 136, 0, 0, blue));
                balls.push(new Ball(174, 127, 0, 0, blue));
                balls.push(new Ball(179, 110, 0, 0, blue));
                balls.push(new Ball(166, 109, 0, 0, blue));
                balls.push(new Ball(156, 110, 0, 0, blue));
                
                // O
                balls.push(new Ball(210, 81, 0, 0, red));
                balls.push(new Ball(197, 91, 0, 0, red));
                balls.push(new Ball(196, 103, 0, 0, red));
                balls.push(new Ball(200, 116, 0, 0, red));
                balls.push(new Ball(209, 127, 0, 0, red));
                balls.push(new Ball(223, 130, 0, 0, red));
                balls.push(new Ball(237, 127, 0, 0, red));
                balls.push(new Ball(244, 114, 0, 0, red));
                balls.push(new Ball(242, 98, 0, 0, red));
                balls.push(new Ball(237, 86, 0, 0, red));
                balls.push(new Ball(225, 81, 0, 0, red));
                
                // O
                var oOffset = 67;
                balls.push(new Ball(oOffset + 210, 81, 0, 0, yellow));
                balls.push(new Ball(oOffset + 197, 91, 0, 0, yellow));
                balls.push(new Ball(oOffset + 196, 103, 0, 0, yellow));
                balls.push(new Ball(oOffset + 200, 116, 0, 0, yellow));
                balls.push(new Ball(oOffset + 209, 127, 0, 0, yellow));
                balls.push(new Ball(oOffset + 223, 130, 0, 0, yellow));
                balls.push(new Ball(oOffset + 237, 127, 0, 0, yellow));
                balls.push(new Ball(oOffset + 244, 114, 0, 0, yellow));
                balls.push(new Ball(oOffset + 242, 98, 0, 0, yellow));
                balls.push(new Ball(oOffset + 237, 86, 0, 0, yellow));
                balls.push(new Ball(oOffset + 225, 81, 0, 0, yellow));
                
                // G
                balls.push(new Ball(370, 80, 0, 0, blue));
                balls.push(new Ball(358, 79, 0, 0, blue));
                balls.push(new Ball(346, 79, 0, 0, blue));
                balls.push(new Ball(335, 84, 0, 0, blue));
                balls.push(new Ball(330, 98, 0, 0, blue));
                balls.push(new Ball(334, 111, 0, 0, blue));
                balls.push(new Ball(348, 116, 0, 0, blue));
                balls.push(new Ball(362, 109, 0, 0, blue));
                balls.push(new Ball(362, 94, 0, 0, blue));
                balls.push(new Ball(355, 128, 0, 0, blue));
                balls.push(new Ball(340, 135, 0, 0, blue));
                balls.push(new Ball(327, 142, 0, 0, blue));
                balls.push(new Ball(325, 155, 0, 0, blue));
                balls.push(new Ball(339, 165, 0, 0, blue));
                balls.push(new Ball(352, 166, 0, 0, blue));
                balls.push(new Ball(367, 161, 0, 0, blue));
                balls.push(new Ball(371, 149, 0, 0, blue));
                balls.push(new Ball(366, 137, 0, 0, blue));
                
                // L
                balls.push(new Ball(394, 49, 0, 0, green));
                balls.push(new Ball(381, 50, 0, 0, green));
                balls.push(new Ball(391, 61, 0, 0, green));
                balls.push(new Ball(390, 73, 0, 0, green));
                balls.push(new Ball(392, 89, 0, 0, green));
                balls.push(new Ball(390, 105, 0, 0, green));
                balls.push(new Ball(390, 118, 0, 0, green));
                balls.push(new Ball(388, 128, 0, 0, green));
                balls.push(new Ball(400, 128, 0, 0, green));
                
                // E
                balls.push(new Ball(426, 101, 0, 0, red));
                balls.push(new Ball(436, 98, 0, 0, red));
                balls.push(new Ball(451, 95, 0, 0, red));
                balls.push(new Ball(449, 83, 0, 0, red));
                balls.push(new Ball(443, 78, 0, 0, red));
                balls.push(new Ball(430, 77, 0, 0, red));
                balls.push(new Ball(418, 82, 0, 0, red));
                balls.push(new Ball(414, 93, 0, 0, red));
                balls.push(new Ball(412, 108, 0, 0, red));
                balls.push(new Ball(420, 120, 0, 0, red));
                balls.push(new Ball(430, 127, 0, 0, red));
                balls.push(new Ball(442, 130, 0, 0, red));
                balls.push(new Ball(450, 125, 0, 0, red));
                
                return balls;
            }
            
            function updateBalls(kin, balls){
                var canvas = kin.getCanvas();
                var context = kin.getContext();
                var collisionDamper = 0.3;
                var floorFriction = 0.0005 * kin.getTimeInterval();
                var mouseForceMultiplier = 1 * kin.getTimeInterval();
                var restoreForce = 0.002 * kin.getTimeInterval();
                
                for (var n = 0; n < balls.length; n++) {
                    var ball = balls[n];
                    // set ball position based on velocity
                    ball.y += ball.vy;
                    ball.x += ball.vx;
                    
                    // restore forces
                    if (ball.x > ball.origX) {
                        ball.vx -= restoreForce;
                    }
                    else {
                        ball.vx += restoreForce;
                    }
                    if (ball.y > ball.origY) {
                        ball.vy -= restoreForce;
                    }
                    else {
                        ball.vy += restoreForce;
                    }
                    
                    // mouse forces
                    var mousePos = kin.getMousePos();
                    mouseX = mousePos === null ? 99999 : mousePos.x;
                    mouseY = mousePos === null ? 99999 : mousePos.y;
                    var distX = ball.x - mouseX;
                    var distY = ball.y - mouseY;
                    
                    var radius = Math.sqrt(Math.pow(distX, 2) +
                    Math.pow(distY, 2));
                    
                    var totalDist = Math.abs(distX) + Math.abs(distY);
                    
                    var forceX = (Math.abs(distX) / totalDist) *
                    (1 / radius) *
                    mouseForceMultiplier;
                    var forceY = (Math.abs(distY) / totalDist) *
                    (1 / radius) *
                    mouseForceMultiplier;
                    
                    if (distX > 0) { // mouse is left of ball
                        ball.vx += forceX;
                    }
                    else {
                        ball.vx -= forceX;
                    }
                    if (distY > 0) { // mouse is on top of ball
                        ball.vy += forceY;
                    }
                    else {
                        ball.vy -= forceY;
                    }
                    
                    // floor friction
                    if (ball.vx > 0) {
                        ball.vx -= floorFriction;
                    }
                    else if (ball.vx < 0) {
                        ball.vx += floorFriction;
                    }
                    if (ball.vy > 0) {
                        ball.vy -= floorFriction;
                    }
                    else if (ball.vy < 0) {
                        ball.vy += floorFriction;
                    }
                    
                    // floor condition
                    if (ball.y > (canvas.height - ball.radius)) {
                        ball.y = canvas.height - ball.radius - 2;
                        ball.vy *= -1;
                        ball.vy *= (1 - collisionDamper);
                    }
                    
                    // ceiling condition
                    if (ball.y < (ball.radius)) {
                        ball.y = ball.radius + 2;
                        ball.vy *= -1;
                        ball.vy *= (1 - collisionDamper);
                    }
                    
                    // right wall condition
                    if (ball.x > (canvas.width - ball.radius)) {
                        ball.x = canvas.width - ball.radius - 2;
                        ball.vx *= -1;
                        ball.vx *= (1 - collisionDamper);
                    }
                    
                    // left wall condition
                    if (ball.x < (ball.radius)) {
                        ball.x = ball.radius + 2;
                        ball.vx *= -1;
                        ball.vx *= (1 - collisionDamper);
                    }
                }
            }
            
            function Ball(x, y, vx, vy, color){
                this.x = x;
                this.y = y;
                this.vx = vx;
                this.vy = vy;
                this.color = color;
                this.origX = x;
                this.origY = y;
                this.radius = 10;
            }
            
            window.onload = function(){
                kin = new Kinetic_2d("myCanvas");
                var canvas = kin.getCanvas();
                var context = kin.getContext();
                var mouseX = 99999;
                var mouseY = 99999;
                
                canvas.onmouseout = function(){
                    mouseX = 99999;
                    mouseY = 99999;
                };
                
                var balls = initBalls();
                
                kin.setDrawStage(function(){
                    updateBalls(this, balls);
                    this.clear();
                    var mousePos = kin.getMousePos();
                    mouseX = mousePos === null ? 99999 : mousePos.x;
                    mouseY = mousePos === null ? 99999 : mousePos.y;
                    
                    for (var n = 0; n < balls.length; n++) {
                        var ball = balls[n];
                        context.beginPath();
                        context.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI, false);
                        context.fillStyle = ball.color;
                        context.fill();
                    }
                });
                
                kin.startAnimation();
            };
        </script>
    </head>
    <body onmousedown="return false;">
        <canvas id="myCanvas" width="578" height="200">
        </canvas>
    </body>
</html>

Added nacl/demo/inter.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
<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="loader.js"></script>
  </head>
  <body>
    <div id="listener">
      <script type="text/javascript">
      	document.getElementById('listener')
        .addEventListener('load', moduleDidLoad, true);
      </script>

      <textarea id="txt" cols="160" rows="40"></textarea>
      
      <embed
	 name='nacl_module'
	 id='tcl'
	 width=0 height=0
	 src='../tcl.nmf'
	 type='application/x-nacl'
	 verbose=0
	 source='inter.natcl'>

        <h2>Status</h2>
        <div id="modstatus">NO-STATUS</div>
      </embed>
    </div>

    <p style="font-size : smaller;">
      To run this demo, you need
      <ul style="font-size : smaller;">
        <li> An x86-family processor</li>
        <li> Chrome 15 or higher</li>
        <li> Enabled "Native Client" item in about:flags</li>
	<li> if everything else fails, try the Nacl demos at <a href="http://code.google.com/chrome/nativeclient/docs/examples.html">http://code.google.com/chrome/nativeclient/docs/examples.html</a></li>
      </ul>
    </p>
  </body>
</html>

Added nacl/demo/inter.natcl.









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# sourced by inter.html demo

::nacl::js {
    t0=document.getElementById('txt');
    function bot(w) {
        len=w.value.length+1;
	w.setSelectionRange(len,len);
   	w.focus();     
    }
    t0.value = "# Type Tcl code in there\n";
    t0.onkeydown=function(event) {
	if (event.which==13) {
	    bot(t0);
	    tclDoCoro("swallow \""+tclEsc(t0.value.replace(/.*\n/g,''))+"\"");
	    bot(t0);
	}
	return true;
    }
    bot(t0);
}

proc swallow s {
    append ::accu $s \n
    if {[info complete $::accu]} {
	set x $::accu
	unset ::accu
	set res [uplevel #0 $x]
	::nacl::js "t0.value+=[::nacl::jsquote $res\n];bot(t0);"
    }
}

proc ::nacl::bgerror s {
    ::nacl::js "t0.value+=[::nacl::jsquote ###$s\n];bot(t0);"
}

source rowland.natcl

Added nacl/demo/kinetic2d-v1.0.3.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
 * KineticJS 2d JavaScript Library v1.0.3
 * http://www.kineticjs.com/
 * Copyright 2011, Eric Rowell
 * Licensed under the MIT or GPL Version 2 licenses.
 * Date: September 2 2011
 *
 * Copyright (C) 2011 by Eric Rowell
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var Kinetic_2d = function(canvasId) {
    this.canvas = document.getElementById(canvasId);
    this.context = this.canvas.getContext("2d");
    this.drawStage = undefined;
    this.listening = false;

    // desktop flags
    this.mousePos = null;
    this._mouseDown = false;
    this._mouseUp = false;
    this._mouseOver = false;
    this._mouseMove = false;

    // mobile flags
    this.touchPos = null;
    this._touchStart = false;
    this._touchMove = false;
    this._touchEnd = false;

    // Region Events
    this._currentRegion = null;
    this._regionIndex = 0;
    this._lastRegionIndex = -1;
    this._mouseOverRegionIndex = -1;

    // Animation
    this.t = 0;
    this.timeInterval = 0;
    this.startTime = 0;
    this.lastTime = 0;
    this.frame = 0;
    this.animating = false;
    
    this.mouseDown = false;

    // provided by Paul Irish
    window.requestAnimFrame = (function(callback) {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
            window.setTimeout(callback, 1000 / 40);
        };

    })();
};

// ======================================= GENERAL
// =======================================

Kinetic_2d.prototype.getContext = function() {
    return this.context;
};

Kinetic_2d.prototype.getCanvas = function() {
    return this.canvas;
};

Kinetic_2d.prototype.clear = function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
};

Kinetic_2d.prototype.getCanvasPos = function() {
    var obj = this.getCanvas();
    var top = 0;
    var left = 0;
    while(obj.tagName != "BODY") {
        top += obj.offsetTop;
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return {
        top : top,
        left : left
    };
};

Kinetic_2d.prototype._drawStage = function() {
    if(this.drawStage !== undefined) {
        this.drawStage();

        // desktop flags
        this._mouseOver = false;
        this._mouseMove = false;
        this._mouseDown = false;
        this._mouseUp = false;

        // mobile touch flags
        this._touchStart = false;
        this._touchMove = false;
        this._touchEnd = false;
    }
};

Kinetic_2d.prototype.setDrawStage = function(func) {
    this.drawStage = func;
    this.listen();
};

// ======================================= CANVAS EVENTS

Kinetic_2d.prototype.handleEvent = function(evt) {
    if(!evt) {
        evt = window.event;
    }

    this.setMousePosition(evt);
    this.setTouchPosition(evt);
    this._regionIndex = 0;

    if(!this.animating) {
        this._drawStage();
    }
};

Kinetic_2d.prototype.listen = function() {
    var that = this;
    this._drawStage();

    // desktop events
    this.canvas.addEventListener("mousedown", function(evt) {
        that._mouseDown = true;
        that.mouseDown = true;
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("mousemove", function(evt) {
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("mouseup", function(evt) {
        that._mouseUp = true;
        that.mouseDown = false;
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("mouseover", function(evt) {
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("mouseout", function(evt) {
        that.mousePos = null;
    }, false);

    // mobile events
    this.canvas.addEventListener("touchstart", function(evt) {
        evt.preventDefault();
        that._touchStart = true;
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("touchmove", function(evt) {
        evt.preventDefault();
        that.handleEvent(evt);
    }, false);

    this.canvas.addEventListener("touchend", function(evt) {
        evt.preventDefault();
        that._touchEnd = true;
        that.handleEvent(evt);
    }, false);

};

Kinetic_2d.prototype.getMousePos = function(evt) {
    return this.mousePos;
};

Kinetic_2d.prototype.getTouchPos = function(evt) {
    return this.touchPos;
};

Kinetic_2d.prototype.setMousePosition = function(evt) {
    var mouseX = evt.clientX - this.getCanvasPos().left + window.pageXOffset;
    var mouseY = evt.clientY - this.getCanvasPos().top + window.pageYOffset;
    this.mousePos = {
        x : mouseX,
        y : mouseY
    };
};

Kinetic_2d.prototype.setTouchPosition = function(evt) {
    if(evt.touches !== undefined && evt.touches.length == 1) { // Only deal with
        // one finger
        var touch = evt.touches[0];
        // Get the information for finger #1
        var touchX = touch.pageX - this.getCanvasPos().left + window.pageXOffset;
        var touchY = touch.pageY - this.getCanvasPos().top + window.pageYOffset;

        this.touchPos = {
            x : touchX,
            y : touchY
        };
    }
};

Kinetic_2d.prototype.isMouseDown = function() {
    return this.mouseDown;  
};

// ======================================= REGION EVENTS

Kinetic_2d.prototype.beginRegion = function() {
    this._currentRegion = {};
    this._regionIndex++;
};

Kinetic_2d.prototype.addRegionEventListener = function(type, func) {
    var event = (type.indexOf('touch') == -1) ? 'on' + type : type;
    this._currentRegion[event] = func;
};

Kinetic_2d.prototype.closeRegion = function() {
    var pos = this.touchPos || this.mousePos;

    if(pos !== null && this.context.isPointInPath(pos.x, pos.y)) {
        if(this._lastRegionIndex != this._regionIndex) {
            this._lastRegionIndex = this._regionIndex;
        }

        // handle onmousedown
        if(this._mouseDown && this._currentRegion.onmousedown !== undefined) {
            this._currentRegion.onmousedown();
            this._mouseDown = false;
        }

        // handle onmouseup
        else if(this._mouseUp && this._currentRegion.onmouseup !== undefined) {
            this._currentRegion.onmouseup();
            this._mouseUp = false;
        }

        // handle onmouseover
        else if(!this._mouseOver && this._regionIndex != this._mouseOverRegionIndex && this._currentRegion.onmouseover !== undefined) {
            this._currentRegion.onmouseover();
            this._mouseOver = true;
            this._mouseOverRegionIndex = this._regionIndex;
        }

        // handle onmousemove
        else if(!this._mouseMove && this._currentRegion.onmousemove !== undefined) {
            this._currentRegion.onmousemove();
            this._mouseMove = true;
        }

        // handle touchstart
        if(this._touchStart && this._currentRegion._touchStart !== undefined) {
            this._currentRegion._touchStart();
            this._touchStart = false;
        }

        // handle touchend
        if(this._touchEnd && this._currentRegion._touchEnd !== undefined) {
            this._currentRegion._touchEnd();
            this._touchEnd = false;
        }

        // handle touchmove
        if(!this._touchMove && this._currentRegion._touchMove !== undefined) {
            this._currentRegion._touchMove();
            this._touchMove = true;
        }

    }
    else if(this._regionIndex == this._lastRegionIndex) {
        this._lastRegionIndex = -1;
        this._mouseOverRegionIndex = -1;

        // handle mouseout condition
        if(this._currentRegion.onmouseout !== undefined) {
            this._currentRegion.onmouseout();
        }
    }
};

// ======================================= ANIMATION
// =======================================

Kinetic_2d.prototype.isAnimating = function() {
    return this.animating;
};

Kinetic_2d.prototype.getFrame = function() {
    return this.frame;
};

Kinetic_2d.prototype.startAnimation = function() {
    this.animating = true;
    var date = new Date();
    this.startTime = date.getTime();
    this.lastTime = this.startTime;

    this._drawStage();

    this.animationLoop();
};

Kinetic_2d.prototype.stopAnimation = function() {
    this.animating = false;
};

Kinetic_2d.prototype.getTimeInterval = function() {
    return this.timeInterval;
};

Kinetic_2d.prototype.getTime = function() {
    return this.t;
};

Kinetic_2d.prototype.getFps = function() {
    return this.timeInterval > 0 ? 1000 / this.timeInterval : 0;
};

Kinetic_2d.prototype.animationLoop = function() {
    var that = this;

    this.frame++;
    var date = new Date();
    var thisTime = date.getTime();
    this.timeInterval = thisTime - this.lastTime;
    this.t += this.timeInterval;
    this.lastTime = thisTime;

    this._drawStage();

    if(this.animating) {
        requestAnimFrame(function() {
            that.animationLoop();
        });

    }
};

Changes to nacl/demo/loader.js.

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
//
//  Its job is to pass a Tcl string to [eval] (through naclwrap, see
//  init.nacl), and then take back the result as JS and eval() it.
//  It also detects errors in the latter eval() and pipes them back
//  to [bgerror].

function tclEsc(text) {
    return text.replace(/[][\\$""]/g,'\\$0');
}

function tclDo(s) {

	tclModule.postMessage("eval:::nacl::wrap {" + s + "}");



}
function tcl()
{
	tclDo(Array.prototype.slice.call(arguments).join(" "));
}

// Handle a message coming from the NaCl module.
function handleMessage(message_event) {
    try {
		
		t = message_event.data;
		//printf("ret:"+t);
		eval(t);
    } catch(err) {
		//printf("JS-err:"+err);
		alert("ERROR:"+err);
    }
}

function serialT (thing) {







|



>
|
>
>
>











|
|







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
//
//  Its job is to pass a Tcl string to [eval] (through naclwrap, see
//  init.nacl), and then take back the result as JS and eval() it.
//  It also detects errors in the latter eval() and pipes them back
//  to [bgerror].

function tclEsc(text) {
    return text.replace(/([\[\]\\$""{}])/g,'\\$1');
}

function tclDo(s) {
    //console.log("TclDo: "+s);
    tclModule.postMessage("eval:::nacl::wrap {" + s + "}");
}
function tclDoCoro(s) {
    tclModule.postMessage("eval:coroutine ::main_coro ::nacl::wrap \"" + tclEsc(s) + "\"");
}
function tcl()
{
	tclDo(Array.prototype.slice.call(arguments).join(" "));
}

// Handle a message coming from the NaCl module.
function handleMessage(message_event) {
    try {
		
		t = message_event.data;
		//console.log("JSdo:"+t);
		window.eval(t);
    } catch(err) {
		//printf("JS-err:"+err);
		alert("ERROR:"+err);
    }
}

function serialT (thing) {

Added nacl/demo/rowland.natcl.









































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
proc tcl::mathfunc::gcd {a b} { 
     while {$b} {set b [expr {$a % [set a $b]}]} 
     return $a 
}
# Rowland's: compute a(n)-a(n-1) where a(1)=7 and 
#   a(n) = a(n – 1) + gcd(n, a(n – 1)) 

proc rowland m {
    set a 7   
    set n 1 
    set out {}
    while {$n<=$m} { 
	incr n 
	set b $a 
	set a [expr {$b+gcd($n,$b)}] 
	set p [expr {$a-$b}] 
	if {$p>1} {lappend out $p} 
    }
    return $out
}

Changes to nacl/init.natcl.

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	    return ""
	} else {
	    return "\{[join $opts ,]\}"
	}
    }

    proc alert {args} {
        js "alert([jsquote [join $args]])"
    }

    proc bgerror {args} {
        printf "### BGERROR: [join $args]\n# [info errorstack]"
        js "alert([jsquote [join $args]])"
    }

    proc wrap {s} {
        variable hooks
        if {[catch {
            uplevel #0 $s
            foreach x $hooks {uplevel #0 $x}







|




|







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	    return ""
	} else {
	    return "\{[join $opts ,]\}"
	}
    }

    proc alert {args} {
        js "alert([::nacl::jsquote [join $args]])"
    }

    proc bgerror {args} {
        printf "### BGERROR: [join $args]\n# [info errorstack]"
        js "alert([::nacl::jsquote [join $args]])"
    }

    proc wrap {s} {
        variable hooks
        if {[catch {
            uplevel #0 $s
            foreach x $hooks {uplevel #0 $x}