Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio support and single playback in attributes. Readme update. #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,43 @@ ar-gif is an effort to bring augmented reality with web components in a easy way
This web-component is used in [Jandig](https://github.com/memeLab/Jandig), a project the aims to bring AR and Art for everyone.

### Usage
ar-gif has a simple API, we have an ar-scene and one or more ar-markers.
ar-gif has a simple API, we have an ar-scene and one or more ar-markers.
```html
<ar-scene>
<ar-marker patt="hiro.patt" content="hiro.gif"></ar-marker>
<ar-marker patt="cat.patt" content="cat.mp4"></ar-marker>
<ar-marker patt="cat.patt" content="cat.mp4"></ar-marker>
</ar-scene>
```

You can use also use some simple attributes to control the appeareance.

- scale : as a two dimensional vector
- position : as a three dimensional vector
- rotation : as a three dimensional vector
- audio : [0|1] If your mp4 supports audio. The default behavior is muted playback.
- loop : [0|1] Stop a mp4 video at the end. The default behavior is looped playback.


```html
<ar-scene>
<ar-marker patt="hiro.patt" content="hiro.gif" scale="1 1" position="0 0 0" rotation="0 0 0"></ar-marker>
<ar-marker patt="cat.patt" content="cat.mp4" audio="1" loop="0"></ar-marker>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a more semantic style, I think "0" or "1" could be changed to "true" or "false".

</ar-scene>
```
Each ar-scene is responsible to detect every marker inside it and each marker is reponsible to show his content.
The "patt" attribute indicates which pattern will be registered for that marker and the content is the gif, image or video that will be played.

For more information about how to use, check [index.html](https://github.com/rodrigocam/ar-gif/blob/master/example/index.html) int [example](https://github.com/rodrigocam/ar-gif/blob/master/example) folder.

# Build the Repo

This is only needed if you want to modify ar-gif!
Normaly you can just take the build/qr-gif.min.js and use it in your project.

For Windows and Linux:
1) clone the repo
1) install nodejs
1) change into directory
1) run $ npm install
1) run $ npx webpack
1) use build/qr-gif.min.js in your project
2 changes: 1 addition & 1 deletion build/ar-gif.min.js

Large diffs are not rendered by default.

30 changes: 24 additions & 6 deletions src/ARMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class ARMarker extends HTMLElement {
src: this.content,
scale: {x: 1, y: 1, z: 1},
position: {x: 0, y: 0, z: 0},
rotation: {x: 0, y: 0, z: 0}
rotation: {x: 0, y: 0, z: 0},
loop: 0,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change these integers (0 or 1) to really booleans (true or false).

audio: 0,
}
}else {
// case where the content was specified as ar-content
Expand All @@ -24,7 +26,9 @@ class ARMarker extends HTMLElement {
src: arContent.getAttribute('src'),
scale: this.stringToVec3(arContent.getAttribute('scale')),
position: this.stringToVec3(arContent.getAttribute('position')),
rotation: this.stringToVec3(arContent.getAttribute('rotation'))
rotation: this.stringToVec3(arContent.getAttribute('rotation')),
loop: arContent.getAttribute('loop'),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can use something inline if:

Suggested change
loop: arContent.getAttribute('loop'),
loop: arContent.getAttribute('loop'),
loop: (arContent.getAttribute('loop') == "true" ? true : false),

audio: arContent.getAttribute('audio')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
audio: arContent.getAttribute('audio')
audio: (arContent.getAttribute('audio') == "true" ? true : false)

}
}
}
Expand All @@ -50,12 +54,22 @@ class ARMarker extends HTMLElement {
}else{
this.video = document.createElement("video");
this.video.src = this.contentProps.src;

this.video.loop = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the previous changes here you can do:

Suggested change
this.video.loop = true;
this.video.loop = this.contentProps.loop;

if(this.contentProps.loop == 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the previous changes, these lines can be removed

{
this.video.loop = false;
}

this.video.muted = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.video.muted = true;
this.video.muted = !this.contentProps.audio;

if(this.contentProps.audio == 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the previous changes, these lines can be removed

{
this.video.muted = false;
}
}

const self = this

arController.addEventListener('getMarker', (ev) => {
// supress wanings
console.warn = function(){}
Expand All @@ -70,13 +84,17 @@ class ARMarker extends HTMLElement {
let ctx = self.textureCanvas.getContext('2d');
ctx.clearRect(0, 0, self.textureCanvas.width, self.textureCanvas.height);
ctx.drawImage(self.image, 0, 0, self.textureCanvas.width, self.textureCanvas.height);

} else if(ev.data.marker.id == markerId) {
if(self.video.ended != 0 && self.contentProps.loop == 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this line is when the video ended if I have to update the page to play again. When the marker goes out of the camera vision and then returns, the video doesn't play again. This is the expected behavior?

{
return;
}
self.video.play();
(function loop() {
let ctx = self.textureCanvas.getContext('2d');
if (!self.video.paused && !self.video.ended) {
ctx.clearRect(0,0, self.textureCanvas.width, self.textureCanvas.height);
ctx.clearRect(0,0, self.textureCanvas.width, self.textureCanvas.height);
ctx.drawImage(self.video, 0, 0, self.textureCanvas.width, self.textureCanvas.height);
texture.needsUpdate = true;
setTimeout(loop, 1000 / 30); // drawing at 30fps
Expand Down Expand Up @@ -116,7 +134,7 @@ class ARMarker extends HTMLElement {
// converts a string in the format "1 1 1" to an object like {x: 1, y: 1, z: 1}
stringToVec3(str) {
let split = str.split(" ")

return {
x: split[0] || 0,
y: split[1] || 0,
Expand Down