AR.jsやMindARでWebARを簡単に実現できます
作成したWebAR上の任意の画像をクリックし、動画再生を行うことも可能です
動画再生は、動画そのものがクリック出来ないため、動画にサムネイル画像を重ね、サムネイル画像のクリックにより、javascriptsを起動させサムネイル画像を非表示とし、その下の動画を再生表示するという流れです
HTML
x
23
23
1
<script>
2
AFRAME.registerComponent("click1", {
3
init: function () {
4
const video = document.getElementById("video");
5
const arVideo = document.getElementById("arVideo");
6
const thumbnailImg = document.getElementById("thumbnailImg");
7
let is_playing = false;
8
this.el.addEventListener("click", () => {
9
console.log("click1");
10
if (!is_playing) {
11
thumbnailImg.setAttribute("visible", false);
12
arVideo.setAttribute("visible", true);
13
video.play();
14
is_playing = true;
15
} else {
16
video.pause();
17
is_playing = false;
18
}
19
});
20
},
21
});
22
</script>
23
サムネイルや動画を<a-assets>で準備し、配置します
HTML
xxxxxxxxxx
1
12
12
1
<a-assets>
2
<image
3
crossorigin="anonymous"
4
id="thumbnail1"
5
src="https://cdn.glitch.global/a575940f-5bb4-4d84-b1ec-39de683638f7/11omote_start.jpeg?v=1728086615601"
6
></image>
7
<video
8
crossorigin="anonymous"
9
id="video"
10
src="https://cdn.glitch.global/cbf17ad1-1348-4ae7-b9c3-7207634324f5/2024meishi.mp4?v=1703162875641"
11
></video>
12
HTML
xxxxxxxxxx
1
19
19
1
<a-entity mindar-image-target="targetIndex: 0">
2
<a-video
3
id="arVideo"
4
src="#video"
5
width="1.8"
6
height="0.9"
7
position="0 0 0"
8
rotation="0 0 0"
9
></a-video>
10
<a-image
11
click1
12
class="clickable"
13
id="thumbnailImg"
14
src="#thumbnail1"
15
width="1.8"
16
height="0.9"
17
position="0 0 0"
18
rotation="0 0 0"
19
></a-image>
なお、画像クリックには、以下の定義が不可欠です
HTML
xxxxxxxxxx
1
7
1
<a-camera
2
position="0 0 0"
3
look-controls="enabled: false"
4
cursor="fuse: false; rayOrigin: mouse;"
5
raycaster="near: 10; far: 10000; objects: .clickable"
6
></a-camera>
7
これでサムネイル画像をクリックすると動画を再生することが可能となります
加えて、MindARの利用でマルチターゲットに対応しています
なお、以下のようなjavascriptsでも動画再生は可能ですが、この場合は、画像のクリックでの動画再生でなくWindowsクリックで再生となり、オブジェクトごとでの切り分けはできません
HTML
xxxxxxxxxx
1
10
10
1
<script>
2
window.addEventListener("click", () => {
3
const video = document.getElementById("video");
4
const arVideo = document.getElementById("arVideo");
5
const thumbnailImg = document.getElementById("thumbnailImg");
6
thumbnailImg.setAttribute("visible", false);
7
arVideo.setAttribute("visible", true);
8
video.play();
9
});
10
</script>