center snippets

Opening a new window in the center of the screen with JavaScript

Tagged center, window, open, javascript, screen  Languages javascript

This JavaScript code will open a URL in a new window and center the window on the screen, instead of showing the window in a random place:

var url = 'http://google.com/';
var width  = 640;
var height = 480;
   
var left   = (screen.width / 2) - (width / 2);
var top    = (screen.height / 2) - (height / 2);
   
var xWindow = window.open(url, 'post', 'resizable=1,width=' + width + ',height=' + height + ', top=' + top + ', left=' + left);

xWindow.focus();

Crop + Gravity + Paperclip

Tagged weight, center, paperclip, geometry  Languages ruby

By default the cropping is weighted at the center of the Geometry. I want it to be north west, so I came up with this dirty hack for setting the gravity to north west:

module Paperclip
 
   # Defines the geometry of an image.
  class Geometry

     def cropping dst, ratio, scale
       if ratio.horizontal? || ratio.square?
         #"%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
         "%dx%d+%d+%d" % [ dst.width, dst.height, 0, 0 ]
       else
         #"%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ]
         "%dx%d+%d+%d" % [ dst.width, dst.height, 0, 0 ]
       end
     end
  end
end