summaryrefslogtreecommitdiff
path: root/snap.html
blob: 65b21f78e5bae73d90e8588b78a59ef354da1cf8 (plain)
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
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Snap! Build Your Own Blocks. Beta</title>
        <link rel="shortcut icon" href="favicon.ico">
        <link rel="stylesheet" href="leaflet.css" />
        <link rel="stylesheet" href="Leaflet.Coordinates-0.1.4.css" />
        <script type="text/javascript" src="leaflet.js"></script>
        <script type="text/javascript" src="OSMBuildings-Leaflet.js"></script>
        <script type="text/javascript" src="Leaflet.Coordinates-0.1.4.min.js"></script>
        <script type="text/javascript" src="leaflet-geodesy.js"></script>
        <script type="text/javascript" src="leaflet-pip.min.js"></script>
        <script type="text/javascript" src="pouchdb-3.3.1.min.js"></script>
        <script type="text/javascript" src="L.TileLayer.PouchDBCached.js"></script>
        <script type="text/javascript" src="peer.js"></script>
        <script type="text/javascript" src="morphic.js"></script>
        <script type="text/javascript" src="widgets.js"></script>
        <script type="text/javascript" src="blocks.js"></script>
        <script type="text/javascript" src="threads.js"></script>
        <script type="text/javascript" src="objects.js"></script>
        <script type="text/javascript" src="gui.js"></script>
        <script type="text/javascript" src="paint.js"></script>
        <script type="text/javascript" src="lists.js"></script>
        <script type="text/javascript" src="byob.js"></script>
        <script type="text/javascript" src="xml.js"></script>
        <script type="text/javascript" src="store.js"></script>
        <script type="text/javascript" src="locale.js"></script>
        <script type="text/javascript" src="cloud.js"></script>
        <script type="text/javascript" src="sha512.js"></script>
    </head>
    <body style="margin: 0;">
        <canvas id="world" tabindex="1" style="position: absolute;"></canvas>
        <div id="map" style="position: absolute; width: 480px; height: 360px;" />
        <!-- set initial size to 480x360 or the map will not load properly -->
        <script type="text/javascript">
                // custom leaflet definitions
                L.SpriteIcon = L.Icon.extend({
                    options: {
                        iconSize: [24, 24],
                        iconAnchor: [12, 12],
                        canvas: null,
                        className: 'leaflet-sprite-icon'
                    },
                    createIcon: function () {
                        this.options.iconSize =
                            [this.options.canvas.width, this.options.canvas.height];
                        var copy = document.createElement('canvas');
                        copy.width = this.options.iconSize[0];
                        copy.height = this.options.iconSize[1];
                        var context = copy.getContext('2d');
                        context.drawImage(this.options.canvas, 0, 0);
                        this._setIconStyles(copy, 'icon');
                        return copy;
                    },
                    createShadow: function (oldIcon) {
                        return null;
                    }
                });
                L.spriteIcon = function (options) {
                    return new L.SpriteIcon(options);
                };

                // Snap! YOW starts here
                var world, map, layer, spriteGroup;

                function loop() {
                    world.doOneCycle();
                }

                // Grabbing values via global variables from deep inside Snap! is easier
                // Possible drawback: Multiple worlds could conflict
                function locationfound(e) {
                    if (!window.geoposition) {
                        // Sets the view on the first locate
                        map.setView(e.latlng, 18);
                    }
                    window.geoposition = e.latlng;
                }

                window.map = map;
                window.layer = layer;

                // initialize leaflet
                map = L.map('map', {
                    keyboard: false,
                    closePopupOnClick: false
                });
                // The map steals the key events, so the 'world' canvas has to be focused
                map.on('mouseover', function() {document.getElementById('world').focus();}); 
                // Resizing hides the markers
                map.on('resize', function() {
                    ide.sprites.asArray().forEach(function (sprite) {
                        sprite.updateMarker();
                    });
                });

                var state = 'desktop'; // TODO: check other states than listed below
                // TODO: too much repeated code, shorten the url part
                if ('connection' in navigator) {
                    // mobile cordova app
                    state = navigator.connection.type;
                }
                console.log('Using tile layer appropriate for ' + state);

                // mobile resolution is JPG 70% (smallest)
                var url = 'http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA';
                var attribution = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';

                if (state == 'desktop') {
                    // download everything and in high resolution
                    layer = L.tileLayer('http://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}@2x.jpg?access_token=pk.eyJ1IjoiY29kZS13dnMiLCJhIjoielU1N2dQayJ9.miUjtJaW7Y_Xx46dC6m0LA', {
                        useCache: true,
                        cacheMaxAge: 604800000, // one week
                        maxZoom: 21,
                        maxNativeZoom: 19,
                        attribution: attribution
                    });
                    new OSMBuildings(map).load();
                } else if (state == 'wifi') {
                    // download low-res and cache as much as possible
                    // with wifi, fancy buildings are ok
                    layer = L.tileLayer(url, {
                        useCache: true,
                        cacheMaxAge: 604800000, // one week
                        maxZoom: 21,
                        maxNativeZoom: 19,
                        attribution: attribution
                    });
                    new OSMBuildings(map).load();
                } else if (state == 'cellular') {
                    // we do not need OSMBuildings here
                    layer = L.tileLayer(url, {
                        useCache: true,
                        cacheMaxAge: 604800000, // one week
                        maxZoom: 21,
                        maxNativeZoom: 19,
                        attribution: attribution
                    });
                } else {
                    // shit, we're offline - let's hope there is something cached
                    // OSMBuildings won't work, anyway
                    layer = L.tileLayer(url, {
                        useCache: true,
                        useOnlyCache: true,
                        cacheMaxAge: 604800000, // one week
                        maxZoom: 21,
                        maxNativeZoom: 19,
                        attribution: attribution
                    });
                }
                layer.addTo(map);
                L.control.coordinates({
                    decimals: 7,
                    enableUserInput: false,
                    labelTemplateLng: "Lon: {x}",
                    position: "topright"
                }).addTo(map);

                // continuously update the local location variable
                map.on('locationfound', locationfound);
                map.locate({watch: true, enableHighAccuracy: true});

                // OSMBuildings provides fancy 3D buildings

                // A Sprite's X position is mapped to longitude
                // A Sprite's Y position is mapped to latitude
                // The stage has -x left and +x right,
                // -y top and +y bottom but we want
                // +y top and -y bottom.
                window.spriteGroup = spriteGroup;
                spriteGroup = L.layerGroup().addTo(map);

                // every sprite has its own polylines/polygons that are added to penTrails
                window.penTrails = L.layerGroup().addTo(map);

                // Load the IDE
                world = new WorldMorph(document.getElementById('world'));
                world.worldCanvas.focus();
                ide = new IDE_Morph();
                ide.openIn(world);

                setInterval(loop, 10);
        </script>
    </body>
</html>