====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
script_parallax_wallpaper [2014/10/12 11:38] trianguloy created |
script_parallax_wallpaper [2018/08/14 16:39] (current) f43nd1r fix link |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== About the script ====== | ====== About the script ====== | ||
* Purpose : Background wallpaper that moves with accelerometer (parallax effect) The script takes the accelerometer values and place the item accordingly. It is size-dependent so the bigger the item is the more it will move. (Items smaller that the screen size will move a bit odd) | * Purpose : Background wallpaper that moves with accelerometer (parallax effect) The script takes the accelerometer values and place the item accordingly. It is size-dependent so the bigger the item is the more it will move. (Items smaller that the screen size will move a bit odd) | ||
- | * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] with the help of [[https://plus.google.com/+LukasMorawietz|LM13]]. All the new bind class, and accelerometer events code was made by Lukas (also some suggestions), the position computation, tags and relative is mine. | + | * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] with the help of F43nd1r. All the new bind class, and accelerometer events code was made by Lukas (also some suggestions), the position computation, tags and relative is mine. |
- | * Link: https://plus.google.com/u/1/105066926163073195690/posts/XqiqsFmcnDS | + | * Link: https://plus.google.com/+TrianguloY/posts/XqiqsFmcnDS |
====== How to use the script ====== | ====== How to use the script ====== | ||
Line 22: | Line 22: | ||
Note: I needed to add a small code to avoid the '0-size' bug. If you get an error when the container is loaded just turn off/on the screen. Also I couldn't test it myself long enough. If you get an error or it don't work as expected write a comment. | Note: I needed to add a small code to avoid the '0-size' bug. If you get an error when the container is loaded just turn off/on the screen. Also I couldn't test it myself long enough. If you get an error or it don't work as expected write a comment. | ||
+ | |||
+ | Version 2: now it works on portrait and landscape | ||
====== Script code ====== | ====== Script code ====== | ||
+ | **New version** | ||
+ | <sxh javascript> | ||
+ | //Version 2 | ||
+ | |||
+ | var delta=0.04;//between (0,1], to avoid shaking due to accelerometer fast changes ( try low values like 0.1) | ||
+ | |||
+ | var threshold=0;//minimum pixels required to move it, if it shakes when the device rest (try 1,2...) | ||
+ | |||
+ | var accuracy=1; //accuracy of the Sensor. 0 is the fastest but can consume a bit more battery. 3 is battery-friendly, but can be slower. 1 and 2 are a medium value. Default 1 | ||
+ | |||
+ | var tablet = false; //set this to true if your device is a tablet or if the item is moving vertical instead of horizontal and the opposite | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | var data=LL.getEvent().getData(); | ||
+ | if(data=="")data=null; | ||
+ | |||
+ | var gyro=LL.getEvent().getItem()||LL.getItemById(data||-1); | ||
+ | if(gyro==null){ | ||
+ | alert("run this script from an item"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | var source=LL.getEvent().getSource(); | ||
+ | |||
+ | //disable when desired | ||
+ | if(source!="I_RESUMED" && data==null) | ||
+ | { | ||
+ | gyro.setTag("parallax",null); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | var cont=gyro.getParent(); | ||
+ | |||
+ | |||
+ | //small tweak to avoid strange bugs that happen when the script is run directly | ||
+ | //this only runs itself | ||
+ | if(gyro.getTag("parallax")!="repeat"&&cont.getHeight()==0){ | ||
+ | gyro.setTag("parallax","repeat"); | ||
+ | var name=LL.getCurrentScript().getName(); | ||
+ | setTimeout(function(){LL.runScript(name,gyro.getId())},0); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | var token=Math.random(); | ||
+ | while(token==gyro.getTag("parallax"))token=Math.random(); | ||
+ | |||
+ | gyro.setTag("parallax",token); | ||
+ | |||
+ | |||
+ | var g=[gyro.getWidth()*gyro.getScaleX()-cont.getWidth(),gyro.getHeight()*gyro.getScaleY()-cont.getHeight()]; | ||
+ | var portrait=cont.getWidth()<cont.getHeight(); | ||
+ | var xy=[,]; | ||
+ | |||
+ | LL.bindClass("android.hardware.Sensor"); | ||
+ | LL.bindClass("android.hardware.SensorEventListener"); | ||
+ | LL.bindClass("android.hardware.SensorManager"); | ||
+ | |||
+ | var sm = LL.getContext().getSystemService(Context.SENSOR_SERVICE); | ||
+ | var orientation = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | ||
+ | var i = { | ||
+ | onAccuracyChanged: function(sensor,accuracy){}, | ||
+ | onSensorChanged: function(event) { | ||
+ | |||
+ | if(LL.isPaused()){ | ||
+ | gyro.setTag("parallax",null); | ||
+ | sm.unregisterListener(l); | ||
+ | return; | ||
+ | } | ||
+ | if(gyro.getTag("parallax")!=token){ | ||
+ | sm.unregisterListener(l); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | if(portrait!=(cont.getWidth()<cont.getHeight())){ | ||
+ | |||
+ | sm.unregisterListener(l); | ||
+ | cont.setTag("parallax",null); | ||
+ | |||
+ | var name=LL.getCurrentScript().getName(); | ||
+ | setTimeout(function(){LL.runScript(name,gyro.getId())},0); | ||
+ | |||
+ | return; | ||
+ | } | ||
+ | |||
+ | if(portrait!=tablet){ | ||
+ | var v=[-event.values[0],event.values[1]]; | ||
+ | }else{ | ||
+ | var v=[event.values[1],event.values[0]]; | ||
+ | } | ||
+ | |||
+ | var x=(v[0]+10)/20; | ||
+ | var y=(v[1]+10)/20; | ||
+ | |||
+ | if(xy[0]==null)xy=[x,y]; | ||
+ | |||
+ | x=(x>1)?1:(x<0)?0:x; | ||
+ | y=(y>1)?1:(y<0)?0:y | ||
+ | //var flags=[true,true]; | ||
+ | if(Math.abs(x-xy[0])*delta*g[0]<threshold)x=xy[0]; | ||
+ | if(Math.abs(y-xy[1])*delta*g[1]<threshold)y=xy[1]; | ||
+ | |||
+ | xy=[xy[0]+(x-xy[0])*delta,xy[1]+(y-xy[1])*delta]; | ||
+ | |||
+ | |||
+ | gyro.setPosition(-g[0]*xy[0],-g[1]*xy[1]); | ||
+ | |||
+ | } | ||
+ | }; | ||
+ | |||
+ | var l = new SensorEventListener(i); | ||
+ | |||
+ | sm.registerListener(l, orientation,accuracy); | ||
+ | </sxh> | ||
+ | |||
+ | **Old version** | ||
<sxh javascript> | <sxh javascript> | ||