No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Web development

Using JavaScript to vibrate the device with a web browser

npn by npn
June 23, 2019
in Web development
Reading Time: 2 mins read
0
0
SHARES
1k
VIEWS
Share on FacebookShare on Twitter
Rate this post

I was using A-Frame for Web VR production, but I wanted to vibrate the device when selecting an item on the screen.
But there seems to be no method to make A-Frame vibrate the controller.

READ ALSO

What is the location of the MySQL databases?

What is the location of the MySQL databases?

April 24, 2022
158
Top 10 Best WordPress SEO themes of 2022

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
211

As a result, I found a way to vibrate each device, but I did not have any Japanese documents, so I will post it Qiita.

It can be implemented quite simply.

var duration = 200;
navigator.vibrate(duration);

Vibration API-Developer’s Guide | According to MDN

ADVERTISEMENT

Can I use, IE and Opera are not yet compatible with Safari yet …
As browser is not supported, it seems that it is impossible at present to vibrate with Safari.
Can I use … Support tables for HTML5

originally spear wanted favorite is here.
How to open the web browser with VR and vibrate when the target targeted by the controller is selected.

If you just vibrate it, you can use this code.

var duration = 200;
var value = 0.5;
var gamepads = navigator.getGamepads && navigator.getGamepads();
for (var i = 0; gamepads.length; i++) {
  var gamepad = gamepads[i];
  gamepad.hapticActuators[0].pulse(value, duration);
}

Documentation is here
GamepadHapticActuator.pulse ()-Web API | MDN

Let’s introduce with A-Frame that can easily build WebVR.

<html>
  <head>
    <script src="https://aframe.io/releases/0.9.2/aframe.min.js"></script>
    <script>
      AFRAME.registerComponent('vibration', {
        schema: {
          duration: {type: 'int', default: 200},
          value: {type: 'number', default: 0.5}
        },
        init: function() {
          this.onClick = this.onClick.bind(this);
          this.el.addEventListener('click', this.onClick);
          this.el.setAttribute('class', 'collidable');
        },
        onClick: function(e) {
          navigator.vibrate(this.data.duration);
          var gamepads = navigator.getGamepads && navigator.getGamepads();
          for (var i = 0; gamepads.length; i++) {
            var gamepad = gamepads[i];
            gamepad.hapticActuators[0].pulse(this.data.value, this.data.duration);
          }
        }
      });
    </script>
  </head>
  <body>
    <a-scene>
      <a-sky color="black"></a-sky>
      <a-entity cursor="rayOrigin: mouse"></a-entity>
      <a-entity laser-controls raycaster="objects: .collidable; far: 5;"></a-entity>
      <a-plane vibration width="1" height="1" position="0 0.5 -2"></a-plane>
    </a-scene>
  </body>
</html>

When I saw it on Oculus Quest’s web browser, the laser from the right-hand controller comes out like this, and when I hit the button and hit the white square, it vibrates.

vr.jpg
Tags: A-FrameCSS3Gamepad APIJavaScriptVibration APIWebVR
ShareTweetShare
Previous Post

Linux performance tuning – Idea to optimize Linux system

Next Post

[JS] 2 == 1 = 0 is true

npn

npn

Related Posts

What is the location of the MySQL databases?
Linux

What is the location of the MySQL databases?

April 24, 2022
158
Top 10 Best WordPress SEO themes of 2022
Web development

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
211
Gmail – Gmail Sign Up – Gmail Login
Web development

Gmail – Gmail Sign Up – Gmail Login

August 30, 2021
6.5k
Configuring VS Code for Node/JavaScript Development
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
808
How does Nodejs solve the problem of high concurrency?
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
888
How to create a self-signed SSL certificate for Apache on Ubuntu 16.04
Linux

How to create a self-signed SSL certificate for Apache on Ubuntu 16.04

July 18, 2021
682
Next Post

[JS] 2 == 1 = 0 is true

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner