HTML5 uses gravity induction to shake and change colors, which can be used for lucky draw and so on

Time:2022-5-7

I found a case on the Internet, which is used to shake and change the color. It’s very easy to use. However, after the test, we should pay attention to:

1. You must have IOS or Android 4.0 or above to implement functional applications.

2. If the judgment of Z coordinate is added, the screen will change color randomly. It is estimated that it is too sensitive… Or the correct usage is not found. I hope you can correct it

3. With a slight change of this function, it can become a lottery method, which is more fashionable than large turntable and scratch card. Ha ha

Copy code

The code is as follows:

<script type=”text/javascript”>
var color = new Array(‘red’, ‘blue’, ‘yellow’, ‘green’, ‘#00f’, ‘#0ff’);
if(window.DeviceMotionEvent) {
var speed = 25;
var x = y = z = lastX = lastY = lastZ = 0;
window.addEventListener(‘devicemotion’, function(){
var acceleration =event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
//z = acceleration.z;
if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed ) {
document.body.style.backgroundColor = color[Math.round(Math.random()*10)%6];
}
lastX = x;
lastY = y;
//lastZ = z;
}, false);
}
</script>

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]