User Tools

Site Tools


dynamic_zooming

Dynamic Zooming

Zoom the website based on uzbl's window size.

Javascript

Demo / screenshot

using xmonad spiral layout:

screencast with dwm:

http://www.youtube.com/watch?v=uWc-m6S2ImM

Usage

Save the script below to a file called dynzoom.js.

Add this to uzbl's config:

@on_event GEOMETRY_CHANGED script /path/to/dynzoom.js

Script

/*                                                                                                                    
 * Dynamically zoom the content 
 * based on uzbl's window size
 *
 * (c) 2009 by Robert Manea
 *
*/
 
(function(Uzbl) {
 
function set_zoom(level) {
    var zl = parseFloat(Uzbl.run("print \@zoom_level"));
 
    if(zl != level)
        Uzbl.run("set zoom_level = " + level); 
}
 
/* Get geometry information */
var r = /^([0-9]+)x([0-9]+)[+-].*$/;
var g = Uzbl.run("print \@geometry");
r.exec(g);
 
var w = parseInt(RegExp.$1);
var h = parseInt(RegExp.$2);
 
/* Logic */  
if(w*h > 300*400) { 
    set_zoom(1.0);
}
else {
    set_zoom(0.5);
}
 
})(Uzbl);

Zooming in an out often changes the scroll position, so I added a couple of lines to handle that within the set_zoom function:

function set_zoom(level) {
    var zl = parseFloat(Uzbl.run("print \@zoom_level"));
 
    if(zl != level)
        if(level < zl) Uzbl.run("set savedscroll = " + window.pageYOffset);
        Uzbl.run("set zoom_level = " + level); 
        if(level > zl) window.scroll(0, Uzbl.run("print @savedscroll"));
}

I should probably unite the two levels of if, but for now this works great

Python

Usage

Save the script below to a file called dynzoom.py.

Add this to uzbl's config. Assuming a minimal width of 1024 and height of 768.

@on_event GEOMETRY_CHANGED spawn /path/to/dynzoom.py \@geometry 1024 768

Script

See http://gist.github.com/319063

#!/usr/bin/env python
 
# dynzoom.py - dynamic zooming for uzbl, based on dynzoom.js
# Usage:
# @on_event GEOMETRY_CHANGED spawn /path/to/dynzoom.py \@geometry 1024 768
# Where 1024x768 is the resolution where we start to zoom out
 
import sys, re
 
" Parses WxH+X+Y into a 4-tuple of ints "
def get_geometry(geo):
    return map(int, re.match(r'(\d+)x(\d+)[\+-](\d+)[\+-](\d+)', geo).groups())
 
" Calculate the zoom level "
def calc_zoom(geo, min_width, min_height):
    width, height, x, y = get_geometry(geo)
    w = min(1, width/float(min_width))
    h = min(1, height/float(min_height))
    return (w + h)/2
 
" Set the zoom level "
def set_zoom(fifo, level):
    with open(fifo, 'w') as f:
        f.write('set zoom_level = %f\n' % level)
 
if __name__ == '__main__':
    set_zoom(sys.argv[4], calc_zoom(*sys.argv[8:11]))

Or, for use with the experimental branch:

#!/usr/bin/env python
 
# dynzoom.py - dynamic zooming for uzbl, based on dynzoom.js
# Usage:
# @on_event GEOMETRY_CHANGED spawn /path/to/dynzoom.py \@geometry 1024 768
# Where 1024x768 is the resolution where we start to zoom out
 
import sys, re, os
 
" Parses WxH+X+Y into a 4-tuple of ints "
def get_geometry(geo):
    return map(int, re.match(r'(\d+)x(\d+)[\+-](\d+)[\+-](\d+)', geo).groups())
 
" Calculate the zoom level "
def calc_zoom(geo, min_width, min_height):
    width, height, x, y = get_geometry(geo)
    w = min(1, width/float(min_width))
    h = min(1, height/float(min_height))
    return (w + h)/2
 
" Set the zoom level "
def set_zoom(fifo, level):
    with open(fifo, 'w') as f:
        f.write('set zoom_level = %f\n' % level)
 
if __name__ == '__main__':
    set_zoom(os.environ['UZBL_FIFO'], calc_zoom(*sys.argv[1:4]))
dynamic_zooming.txt · Last modified: 2012/07/23 23:57 (external edit)