-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAR-render-html.html
156 lines (139 loc) · 5.14 KB
/
AR-render-html.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>WebAR: Render HTML</title>
<script src="./js/aframe/aframe.min.js"></script>
<script src="./js/arjs/aframe-ar.js"></script>
<script src="./js/aframe/aframe-html-shader.min.js"></script>
<style type="text/css">
#target {
width: 512px;
height: 256px;
position: absolute;
background: rgba(255, 255, 0, 0.3);
}
/* Hide the HTML using z-index. */
/* Can't use display: none because that would hide the HTML in the rendered material. */
#htmlTarget.hide {
z-index: -10;
}
#target h1 {
font-family: Arial;
font-size: 110px;
margin: 0;
vertical-align: top;
color: white;
}
#target h1 span {
color: tomato;
}
#emoji {
position: absolute;
font-size: 512px;
color: mediumTurquoise;
font-style: serif;
}
#pre {
font-family: monospace;
margin: 0;
padding: 0;
height: 50px;
font-size: 50px;
background: royalblue;
color: tomato;
}
#htmlTarget {
position: absolute;
z-index: 1;
height: 100%;
width: 100%;
overflow: hidden;
position: fixed;
top: 0;
}
#debug {
position: absolute;
bottom: 0;
left: 0;
}
/* Even works with media queries. */
@media (max-width: 768px) {
#target {
width: 256px;
height: 126px;
}
#target h1 {
font-size: 55px;
}
#pre {
height: 50px;
font-size: 25px;
}
#emoji {
position: absolute;
font-size: 256px;
color: red;
}
}
</style>
<script>
// Component to dynamically update the HTML. Iterate over emojis, substring the text.
AFRAME.registerComponent('update-html', {
init: function() {
this.emojis = ['〠', '✪', '♬', '㋵', '㋺', '㋛', '㋗', '㋧'];
this.emojiEntity = document.getElementById('emojiEntity')
this.emojiIndex = 0;
this.emojiUpdateDur = 1000 / this.emojiEntity.getAttribute('material').fps;
this.emojiTime = 0;
this.emoji = document.getElementById('emoji')
this.text = 'A-frame is fun!!!';
this.lettersEntity = document.getElementById('lettersEntity')
this.lettersIndex = 0;
this.lettersUpdateDur = 1000 / this.lettersEntity.getAttribute('material').fps;
this.lettersTime = 0;
this.pre = document.getElementById('pre')
},
tick: function(t, dt) {
this.emojiTime += dt;
// Update emoji, optimizing for the shader's refresh FPS.
if (this.emojiTime >= this.emojiUpdateDur) {
this.emoji.innerHTML = this.emojis[this.emojiIndex++ % this.emojis.length]
this.emojiTime = 0;
}
let text = this.text;
this.lettersTime += dt;
// Update letters, optimizing for the shader's refresh FPS.
if (this.lettersTime >= this.lettersUpdateDur) {
let indexEnd = ++this.lettersIndex % (text.length + 1)
if (text.charAt(indexEnd) === ' ') {
indexEnd = ++this.lettersIndex % (text.length + 1)
}
this.pre.innerHTML = text.substring(0, indexEnd)
this.lettersTime = 0;
}
}
});
</script>
</head>
<body style="margin : 0px; overflow: hidden;">
<a-scene embedded arjs update-html>
<a-anchor>
<!-- Emoji. -->
<a-entity id="emojiEntity" geometry="primitive: box; width: 1; height: 1; depth: 1" position="0 0.6 0" material="shader: html; target: #emoji; transparent: true; ratio: height; fps: 0.3; side: double"></a-entity>
<!-- Letters. -->
<a-entity id="lettersEntity" geometry="primitive: plane; width: 1; height: 1" rotation="-90 0 0" position="0 0.6 1" material="shader: html; target: #target; transparent: true; ratio: width; fps: 1.5"></a-entity>
<!-- HTML to render as a material. -->
<div id="htmlTarget" class="hide">
<div id="emoji">〠</div>
<div id="target">
<h1>HELLO<span>★</span></h1>
<span id="pre">A</span>
</div>
</div>
</a-anchor>
<a-entity camera></a-entity>
</a-scene>
</body>
</html>