Sunday, August 21, 2016

Balanced Responsive Grid

I needed to create a responsive grid of logos and this is what I have done.

Step 1

I started creating a simple html with the img nested in couple of divs. Then I used the old fashioned float left. Is possible to use flex boxes for a better result. The first result was horrible due to the fact that images have all sort of pixel size and ratio.


Step 2

To adjust the things I decided to go with the padding-bottom-trick. Having padding-bottom equal 100% insure that the hight of the div is always square because it relate to the width of the parent div.

The just making the image position absolute and adding some style fix the rest. I used max-width and max height = 75% because I wanted to leave some margin around the logo.


The page is much better but I want to limit the max-size of the logo to a certain value, let's say 200px.

Step 3

So I added some math to the Sass that would generated media queries. I started with logos occupying 100% of the screen, so as soon the screen reach 200px I needed to add a media query that would the logos going in two columns.

The media query is
@media (min-width: 200px) {
  .logo {
    width: 50%;
  }
}
When those two logos reach again the maximum width, the screen will be at 200px * 2 = 400px. Time to add a new media query:
@media (min-width: 400px) {
  .logo {
    width: 33.33333%;
  }
}
And so on. To simplify the list of media queries I first created a mixin like
@mixin mediaQuery($lev)
 @media (min-width: $max-element-size * $lev)
  width: percentage( 1 / ( $lev + 1 ) )
Then I created a loop that generate the media query with the mixin
@while $level < $max-levels
 @include mediaQuery($level)
 $level: $level + 1
Now the page adjust itself beautifully and logos width is never wider than 200 pixels.

This would be the conclusion of the exercise until I noted that some font look larger than other. It is just a feeling because the logo share all the same container. Well, probably not just a feeling but it depends on the shape of the logo and eventually on the area of the logo.


Step 4

So I was thinking to calculate the area of the logo to compensate the size but calculating the are may be not trivial as it depend on the shape of the logo and not just the size of the image. Round logos have less area compare to the squared one.

So I decided to use the aspect ratio to influence the size of the logo. I eventually came out with this formula:
Size of the image (percentage) = minPerc + 
    ((( maxPerc - minPerc ) / ( maxRatio - 1 )) * ( r - 1 ) );
Dove r is the ratio of the image that can go from 1 for squared logos to a very large number for rectangular logos. A logos of size 100px horizontal and 50px vertical would have a ratio of 100/50 = 2. The ratio in this case ignore if the logo is bigger horizontally or vertically. So, to calculate the ration I used this formula:
r = Math.min( Math.max(x,y) / Math.min(x,y), maxRatio );
Where maxRatio is the maximum ratio that I will take in consideration. In my example I use 4 so that all logos that have a side longer more than 4 times the other side will all be treated in similar way.

The calculation of a new percentage of the image require width and height of the logo. These values are available once the image is loaded so it would be reasonable to apply the new percentage on the onload event for each image.

Unfortunately the onload event for image has several issues so I decided to go for a much more inelegant way of just waiting, using timers, for when the sizes are ready. There are probably better solutions than this.

While waiting for the new percentage to kick in, I let the css style to resize logos to an average size. So, in my example I will change the size of images from 90% to 50%, depending on the ratio.

Logos contained in a squared shape with a ratio of 1 will have a percentage of 50%; rectangular logos that have one side 4 times or more longer than the other will have a percentage of 90%. Everything else goes in between these two limit cases.

This is the final result (note how the rectangular logos shrink and the square logo enlarge):



Saturday, July 23, 2016

Small Node/Javascript code to upload a file using sftp

This script upload a file using sftp. Before running it be sure to
  • Install NodeJS
  • Install ssh2 (npm install ssh2)
  • Fill the parameters at the top of the file
The script also list all the file in the folder before and after the uploading

Tuesday, May 3, 2016

A way to embed html in javasript

If you are not working with React and JSX, this could be a quick way to embed html inside Javascript...

Sunday, February 7, 2016

Intentional or not?

Happened to me several time that I buy food in a box with a nice picture on it and the content is very different from the picture.

Today I found a product with an actual window in the box where I could see the real product. So, no surprises!



But... it seems that the only two nuts inside were just in the position of the window.

Wednesday, May 13, 2015

Shortcut

I took this video along the path from the train station to my office. This path require walking through a Konbini (Convenience Store).

I found it very hilarious and I could not help laughing the first time I was walking it. I think it was a good business for the shop. They need to clean the floor more often but they would get many extra customers every day.

Sunday, November 9, 2014

SL只見線紅葉号 - Steam Locomotive in Japan

"Densha Otaku" (train nerd) while hunting a Steam Locomotive in Aizu. There are 36 type of densha otaku.

Friday, June 15, 2007

Yamanote Minuto - 山手線

The Yamanote Line is one of the busiest train lines in the world. Running in a circle around the heart of Tokyo, it carries 3.5 million passengers a day.




Tuesday, April 5, 2005

Mapping Topic Maps on Relational Databases

Abstract

Topic maps are a new ISO standard for describing knowledge structures and associating them with information resources, a solution for organizing and accessing large and continuously growing information pools (1). They are dubbed as the 'GPS of the information universe'. One possibility to permanently store Topic Maps is using Relational Databases. In this document I present a possible database schema that allows to store Topic Maps.
There are general rules to map DTDs to databases (2) but here I tried to build something ad hoc for Topic Maps. The result is a bit naive and cluttered (Figure 1). I was hoping I could find out a more elegant solution. If you have any suggestion or you know better database model please let me know.

Thursday, November 30, 2000

I treni (The Trains)

https://sites.google.com/site/nihonnoluca/home/i-treni

30 Novembre 2000

Il sistema tranviario qui a Tokyo e` impressionante. Tantissime le stazione e tantissimi i treni. C`e` anche una metropolitana grandissima nel centro di Tokyo e una piu` piccola a Yokohama.


Read more >>

Friday, September 18, 1998

Byte Generation

Byte Generation (1998) di Boris Alfani, Sergio Foti e Luca Mugnaini

Un cattolico alle prese col vizio del fumo, il rito della confessione e Internet.

http://www2.kwcinema.kataweb.it/videominuto/bestoff.html