Pārlūkot izejas kodu

Add Pointer demo

customisations
alemart 10 mēnešus atpakaļ
vecāks
revīzija
3b5e998544

+ 1089
- 0
demos/pointer-demo/NOTICE.html
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 71
- 0
demos/pointer-demo/README.html Parādīt failu

@@ -0,0 +1,71 @@
1
+<!doctype html>
2
+<html>
3
+    <head>
4
+        <meta charset="utf-8">
5
+        <title>encantar.js pointer demo</title>
6
+        <script data-goatcounter="https://encantar-js.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
7
+        <style>
8
+        body {
9
+            font-family: sans-serif;
10
+            background-color: whitesmoke;
11
+        }
12
+
13
+        a:any-link {
14
+            color: #7e56c2;
15
+            text-decoration: none;
16
+        }
17
+
18
+        a:hover {
19
+            text-decoration: underline;
20
+        }
21
+
22
+        li + li {
23
+            margin-top: 1em;
24
+        }
25
+
26
+        ul {
27
+            list-style-type: none;
28
+            padding-left: 1em;
29
+        }
30
+
31
+        img.qr-code {
32
+            display: block;
33
+            width: 256px;
34
+            image-rendering: pixelated;
35
+            border: 1px dashed black;
36
+        }
37
+
38
+        a img {
39
+            height: 80px;
40
+            border-radius: 4px;
41
+        }
42
+        </style>
43
+    </head>
44
+    <body>
45
+        <h1>encantar.js pointer demo</h1>
46
+        <p>Scan the QR code to launch the demo.</p>
47
+
48
+        <h2>Menu</h2>
49
+        <ul>
50
+            <li><a href="index.html">Launch the pointer demo</a></li>
51
+            <li><a href="https://github.com/alemart/encantar-js/blob/master/demos/pointer-demo/demo.js" rel="external" target="_blank">Study the source code</a></li>
52
+            <li><a href="NOTICE.html">See the attribution notices</a></li>
53
+            <li><a href="../">Try other WebAR demos</a></li>
54
+        </ul>
55
+
56
+        <h2>QR code</h2>
57
+        <img src="qr-code.gif" alt="QR code" class="qr-code">
58
+
59
+        <h2>Share</h2>
60
+        <p>This experience is powered by <a href="https://alemart.github.io/encantar-js" target="_blank">encantar.js: GPU-accelerated Augmented Reality for the web</a>.</p>
61
+        <div>
62
+            <a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Falemart.github.io/encantar-js&quote=%23encantarjs" target="_blank" rel="external" class="button"><img src="../assets/facebook.png" alt="Share on Facebook"></a>
63
+            <a href="https://twitter.com/intent/tweet?url=https%3A%2F%2Falemart.github.io/encantar-js&hashtags=encantarjs" target="_blank" rel="external" class="button"><img src="../assets/twitter.png" alt="Share on X"></a>
64
+            <a href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Falemart.github.io/encantar-js" target="_blank" rel="external" class="button"><img src="../assets/linkedin.png" alt="Share on LinkedIn"></a>
65
+            <a href="https://wa.me/?text=https%3A%2F%2Falemart.github.io/encantar-js" target="_blank" rel="external" class="button"><img src="../assets/whatsapp.png" alt="Share via WhatsApp"></a>
66
+            <a href="https://t.me/share/url?url=https%3A%2F%2Falemart.github.io/encantar-js" target="_blank" rel="external" class="button"><img src="../assets/telegram.png" alt="Share via Telegram"></a>
67
+            <a href="https://github.com/sponsors/alemart" target="_blank" rel="external"><img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=%237e56c2&color=%23ffd500"></a>
68
+        </div>
69
+    </body>
70
+</html>
71
+

+ 56
- 0
demos/pointer-demo/demo.css Parādīt failu

@@ -0,0 +1,56 @@
1
+body {
2
+    background-color: #673ab6;
3
+    background-image: url("../assets/poster.svg");
4
+    background-position: center;
5
+    background-repeat: no-repeat;
6
+    height: 100vh;
7
+    margin: 0;
8
+    padding: 0;
9
+}
10
+
11
+#about {
12
+    position: absolute;
13
+    right: 0px;
14
+    top: 0px;
15
+    width: 80px;
16
+    height: 80px;
17
+
18
+    display: flex;
19
+    align-items: center;
20
+    margin: 0;
21
+    padding: 0;
22
+
23
+    color: #fff;
24
+    font-size: 56px;
25
+    font-weight: bold;
26
+    font-family: monospace;
27
+    text-decoration: none;
28
+    text-align: center;
29
+
30
+    border-width: 0 0 2px 2px;
31
+    border-radius: 0 0 0 8px;
32
+    border-style: solid;
33
+    border-color: #673ab6;
34
+    background: linear-gradient(0deg, #7e56c2 0%, #9574cd 100%);
35
+    -webkit-tap-highlight-color: transparent;
36
+}
37
+
38
+#about:hover {
39
+    border-color: #6742b8;
40
+    background: linear-gradient(0deg, #8b72cb 0%, #c894ff 100%);
41
+}
42
+
43
+#about:active {
44
+    border-color: #ddc300;
45
+    background: linear-gradient(0deg, #ffd500 0%, #ffff00 100%);
46
+}
47
+
48
+#about::before {
49
+    content: '\24D8';
50
+    width: 100%;
51
+    text-align: center;
52
+}
53
+
54
+#about:focus {
55
+    outline: none;
56
+}

+ 173
- 0
demos/pointer-demo/demo.js Parādīt failu

@@ -0,0 +1,173 @@
1
+/**
2
+ * encantar.js pointer demo
3
+ * @license LGPL-3.0-or-later
4
+ * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
5
+ */
6
+
7
+(function() {
8
+
9
+let displayMessage = true;
10
+
11
+const POINTER_RADIUS = 50;
12
+const POINTER_COLOR = 'red';
13
+const PRIMARY_POINTER_COLOR = 'lime';
14
+const BACKGROUND_COLOR = 'antiquewhite';
15
+const TEXT_COLOR = 'black';
16
+const TEXT_FONT = '36px sans-serif';
17
+const TEXT_LARGE_FONT = '96px sans-serif';
18
+const TEXT_LINE_HEIGHT = 40;
19
+const TWO_PI = 2.0 * Math.PI;
20
+
21
+/**
22
+ * Render the virtual scene
23
+ * @param {TrackablePointer[]} pointers
24
+ * @param {Viewport} viewport
25
+ * @returns {void}
26
+ */
27
+function render(pointers, viewport)
28
+{
29
+    /*
30
+
31
+    Check out the API reference to see the various interesting properties of a
32
+    TrackablePointer! They are useful for creating all sorts of interactive
33
+    experiences!
34
+
35
+    */
36
+
37
+    // get the canvas context
38
+    const canvas = viewport.canvas;
39
+    const ctx = canvas.getContext('2d');
40
+
41
+    if(!ctx)
42
+        return;
43
+
44
+    ctx.textAlign = 'center';
45
+
46
+    // draw the background
47
+    ctx.fillStyle = BACKGROUND_COLOR;
48
+    ctx.fillRect(0, 0, canvas.width, canvas.height);
49
+
50
+    // display a message
51
+    if(displayMessage) {
52
+        ctx.fillStyle = TEXT_COLOR;
53
+        ctx.font = TEXT_LARGE_FONT;
54
+        ctx.fillText('Tap to begin', canvas.width / 2, canvas.height / 2);
55
+
56
+        if(pointers.length > 0)
57
+            displayMessage = false;
58
+    }
59
+
60
+    // render the pointers
61
+    for(const pointer of pointers) {
62
+        // pointer.position is given in normalized units [-1,1]x[-1,1]
63
+        // we convert it to pixel coordinates (canvas space)
64
+        const position = viewport.convertToPixels(pointer.position);
65
+
66
+        ctx.fillStyle = pointer.isPrimary ? PRIMARY_POINTER_COLOR : POINTER_COLOR;
67
+        ctx.beginPath();
68
+        ctx.arc(position.x, position.y, POINTER_RADIUS, 0, TWO_PI);
69
+        ctx.fill();
70
+    }
71
+
72
+    // render the texts above the pointers
73
+    ctx.fillStyle = TEXT_COLOR;
74
+    ctx.font = TEXT_FONT;
75
+    for(const pointer of pointers) {
76
+        const position = viewport.convertToPixels(pointer.position);
77
+        position.y -= POINTER_RADIUS + 6 * TEXT_LINE_HEIGHT;
78
+
79
+        ctx.fillText('id: ' + pointer.id, position.x, position.y + 0 * TEXT_LINE_HEIGHT);
80
+        ctx.fillText(pointer.phase, position.x, position.y + 1 * TEXT_LINE_HEIGHT);
81
+        ctx.fillText('x: ' + pointer.position.x.toFixed(5), position.x, position.y + 2 * TEXT_LINE_HEIGHT);
82
+        ctx.fillText('y: ' + pointer.position.y.toFixed(5), position.x, position.y + 3 * TEXT_LINE_HEIGHT);
83
+        ctx.fillText('speed: ' + pointer.velocity.length().toFixed(5), position.x, position.y + 4 * TEXT_LINE_HEIGHT);
84
+        ctx.fillText('time: ' + pointer.elapsedTime.toFixed(5), position.x, position.y + 5 * TEXT_LINE_HEIGHT);
85
+    }
86
+}
87
+
88
+/**
89
+ * Start the AR session
90
+ * @returns {Promise<Session>}
91
+ */
92
+async function startARSession()
93
+{
94
+    if(!AR.isSupported()) {
95
+        throw new Error(
96
+            'This device is not compatible with this AR experience.\n\n' +
97
+            'User agent: ' + navigator.userAgent
98
+        );
99
+    }
100
+
101
+    const viewport = AR.Viewport({
102
+        resolution: '720p',
103
+        container: document.getElementById('ar-viewport'),
104
+        hudContainer: document.getElementById('ar-hud')
105
+    });
106
+
107
+    const tracker = AR.Tracker.Pointer();
108
+    const source = AR.Source.Pointer();
109
+
110
+    const session = await AR.startSession({
111
+        mode: 'immersive',
112
+        viewport: viewport,
113
+        trackers: [ tracker ],
114
+        sources: [ source ],
115
+        stats: true,
116
+    });
117
+
118
+    return session;
119
+}
120
+
121
+/**
122
+ * Animation loop
123
+ * @param {number} time
124
+ * @param {Frame} frame
125
+ * @returns {void}
126
+ */
127
+function animate(time, frame)
128
+{
129
+    const session = frame.session;
130
+    const pointers = read(frame);
131
+
132
+    render(pointers, session.viewport);
133
+
134
+    session.requestAnimationFrame(animate);
135
+}
136
+
137
+/**
138
+ * Read the results of the tracker
139
+ * @param {Frame} frame
140
+ * @returns {TrackablePointer[]}
141
+ */
142
+function read(frame)
143
+{
144
+    for(const result of frame.results) {
145
+        if(result.tracker.type == 'pointer-tracker') {
146
+            const pointers = result.trackables;
147
+            return pointers;
148
+        }
149
+    }
150
+
151
+    return [];
152
+}
153
+
154
+/**
155
+ * Start the demo
156
+ * @returns {void}
157
+ */
158
+function main()
159
+{
160
+    startARSession().then(session => {
161
+
162
+        session.requestAnimationFrame(animate); // start the animation loop
163
+
164
+    }).catch(error => {
165
+
166
+        alert(error.message);
167
+
168
+    });
169
+}
170
+
171
+document.addEventListener('DOMContentLoaded', main);
172
+
173
+})();

+ 18
- 0
demos/pointer-demo/index.html Parādīt failu

@@ -0,0 +1,18 @@
1
+<!doctype html>
2
+<html>
3
+    <head>
4
+        <meta charset="utf-8">
5
+        <meta name="viewport" content="width=device-width,initial-scale=1">
6
+        <title>encantar.js pointer demo</title>
7
+        <link href="demo.css" rel="stylesheet">
8
+        <script src="../../dist/encantar.min.js"></script>
9
+        <script src="demo.js"></script>
10
+    </head>
11
+    <body>
12
+        <div id="ar-viewport">
13
+            <div id="ar-hud" hidden>
14
+                <a id="about" href="NOTICE.html" draggable="false"></a>
15
+            </div>
16
+        </div>
17
+    </body>
18
+</html>

Binārs
demos/pointer-demo/qr-code.gif Parādīt failu


+ 8
- 0
docs/demos.md Parādīt failu

@@ -49,6 +49,14 @@ Create an augmented scene without a 3D framework.
49 49
 
50 50
 [Launch demo](/encantar-js/demos/hello-webgl/README.html){ ._blank .md-button }
51 51
 
52
+## Miscellaneous
53
+
54
+### Pointer-based input
55
+
56
+Learn how to work with pointer-based input such as touch and mouse.
57
+
58
+[Launch demo](/encantar-js/demos/pointer-demo/README.html){ ._blank .md-button }
59
+
52 60
 ## Try locally
53 61
 
54 62
 * Run on a console:

Notiek ielāde…
Atcelt
Saglabāt