<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>David Stenbeck</title>
    <description>Collected ramblings and experiments.
</description>
    <link>http://davst.github.io/</link>
    <atom:link href="http://davst.github.io/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Wed, 18 Mar 2015 14:00:27 +0000</pubDate>
    <lastBuildDate>Wed, 18 Mar 2015 14:00:27 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Smart cropping using css</title>
        <description>&lt;p&gt;&lt;em&gt;Photo credit: Philipp Rümmele&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;At lunch today there was a discussion on using responsive images in web projects and some challenges of wanting to display in a nice edge-to-edge manner which leaves the aspect ratio of the image area outside our control.&lt;/p&gt;

&lt;p&gt;The question I got that inspired this post was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Is there any good library or such that&amp;#39;ll let me decide the weighting of the featured photo so it&amp;#39;ll scale towards the important part?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I actually didn&amp;#39;t have a response to this at once, but I was determined to find it since the issue intrigued me.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s use this photo of a stunning view with a our beloved Eddie (this dude definitely needed a name) looking out over the mountains as an example of a image that&amp;#39;d we&amp;#39;d like to feature.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/article_images/2014-10-27-smart-cropping-without-js/samplePhoto.jpg&quot; alt=&quot;Say we have the following image, with a man looking out over a landscape.&quot;&gt;&lt;/p&gt;

&lt;h3&gt;Basics&lt;/h3&gt;

&lt;p&gt;We can achieve a responsive, awesomely moody full screen background using the following code:&lt;/p&gt;

&lt;h3&gt;HTML&lt;/h3&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset=&amp;quot;UTF-8&amp;quot;&amp;gt;
  &amp;lt;title&amp;gt;Smart cropping&amp;lt;/title&amp;gt;
  &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;style.css&amp;quot;&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div class=&amp;quot;coverImage&amp;quot;&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;CSS&lt;/h3&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;body {
  margin: 0px; /* Remove the extra margins */
}

.coverImage {
  background: url(coverPhoto.jpg); /* Set our background photo */
  min-width: 100vw; /* 100% of viewport width */
  min-height: 100vh; /* 100% of viewport height */
  background-size: cover; /* cover sizing is what helps us adapt the photo to the div size */
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See demo &lt;a href=&quot;http://jsfiddle.net/davidstenbeck/oqLuek11/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, if you play around with the fiddle above, you&amp;#39;ll discover that if the area available to display the image strays from the original aspect ratio we are likely to run into two problems. Either the man looking out over the view won&amp;#39;t be visible and we&amp;#39;ll lose alot of the point of the photo, or even worse, the man &lt;em&gt;and&lt;/em&gt; the closest mountaintop both will be outside the field of view and we&amp;#39;ll also lose depth in the photo as illustrated below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/article_images/2014-10-27-smart-cropping-without-js/samplePhoto-ratio.jpg&quot; alt=&quot;Either type of ratio change will introduce problems.&quot;&gt;&lt;/p&gt;

&lt;h2&gt;In comes Smart cropping&lt;/h2&gt;

&lt;p&gt;The idea of smart cropping in responsive design is the ability to have a image adapt and follow a main focal point. this is a concern in responsive design where the area we aim to cover with an image can change in both size and aspect ratio.&lt;/p&gt;

&lt;h3&gt;Awesome, how do we save Eddie?&lt;/h3&gt;

&lt;p&gt;I had a look around on the Internet for some kind of project that was looking to accomplish this and found that a project named &lt;a href=&quot;https://github.com/adamdbradley/focal-point&quot;&gt;Focal Point&lt;/a&gt; was getting alot of attention. Focal Point is a CSS library that provides smart cropping, it is however focused on providing this for legacy browsers and thus it does have alot of bloat if you aren&amp;#39;t in need of an option that will work prior to &lt;em&gt;Internet Explorer 9&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another interesting project is &lt;a href=&quot;https://github.com/jonom/jquery-focuspoint&quot;&gt;Focus Point&lt;/a&gt; (No chance for mixing these two up eh?). focus Point solves smart cropping with a js library requiring jQuery, so again there is the issue of some bloat, but if you want some nice fancy transitions and stuff this might interest you. (To his credit he does mention the method I&amp;#39;m suggesting as well and has done some analysis on how his methods weighting differs.)&lt;/p&gt;

&lt;h3&gt;A clean and minimalist solution.&lt;/h3&gt;

&lt;p&gt;Being a fan of the simple, I do feel that people tend to over-complicate this issue that can in fact be solved with one line of CSS.&lt;/p&gt;

&lt;p&gt;Returning to the sample image, if we look at where we&amp;#39;d like to weight the focus when scaling this image it&amp;#39;d be on Eddie himself. I&amp;#39;d say we&amp;#39;d like to focus in the chest to waist area to make sure we&amp;#39;d get some cliff in the shot as well.&lt;/p&gt;

&lt;p&gt;So we get the coordinates from this point and use them to calculate the percentage from the left and top edge.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/article_images/2014-10-27-smart-cropping-without-js/samplePhoto-crosshair.jpg&quot; alt=&quot;The crosshair (our weighting point) intersects at about x:437 y:289.&quot;&gt;&lt;/p&gt;

&lt;p&gt;For width: 437 / 640 = 0.68 which gives us a weighting point 68% from the left edge of the photo.&lt;/p&gt;

&lt;p&gt;For height: 289 / 427 = 0.68 again, meaning our weighting point is 68% from the top of the photo.&lt;/p&gt;

&lt;p&gt;So, building on our above example, if we add the following line of CSS the image will automatically weight itself towards our target, without adding any bloat to our site.&lt;/p&gt;

&lt;h3&gt;CSS&lt;/h3&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;background-position: 68% 68%;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See demo (with some tweaking of the weighting) &lt;a href=&quot;http://jsfiddle.net/davidstenbeck/u06gy3kp/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;So this turned out to be a long post about a simple solution. The point here though, is that it was a valid question and one that illustrates the power of CSS3 and how things that we think may be very complex to achieve. I also am a big fan of the simple solutions when you find them and think they are worth the same attention that a complex one would have gotten. The third takeaway would be to feature the hard work of the two mentioned GitHub projects that have clearly gone way further in depth with this than I have and seem to have solved this decently for legacy software which to me is impressive. Right?&lt;/p&gt;
</description>
        <pubDate>Mon, 27 Oct 2014 00:00:00 +0000</pubDate>
        <link>http://davst.github.io/css/responsive/images/2014/10/27/smart-cropping-without-js.html</link>
        <guid isPermaLink="true">http://davst.github.io/css/responsive/images/2014/10/27/smart-cropping-without-js.html</guid>
        
        <category>featured</category>
        
        
        <category>css</category>
        
        <category>responsive</category>
        
        <category>images</category>
        
      </item>
    
      <item>
        <title>Building something new</title>
        <description>&lt;h2&gt;Gathering dust&lt;/h2&gt;

&lt;p&gt;For some time I&amp;#39;ve had the feeling that time had outgrown the hosting model I was using for my personal website. Today this notion was brought to the light again when I received an email from my hosting provider with the annual bill for the coming 12 months of service.&lt;/p&gt;

&lt;p&gt;I remember having the same thoughts a year ago, but the combination of me wanting to put focus on other projects and that I received a very good deal for the hosting plan I was on made me decide to put off the upgrade and stay with my provider for another year. During this time it became very apparent that for me, the whole web hosting thing felt like a thing of the past and I wasn&amp;#39;t at all motivated to spend any considerable amounts of time on something that felt obsolete to being with.&lt;/p&gt;

&lt;p&gt;Something obviously had to change. I gave some consideration to where I wanted to go with this and what I actually wanted to get out of having some kind of public on-line presence which resulted in several insights. The main one being that I needed something that would let me hit the ground running, to be able to start building and adding content along the process of evolving the site itself.&lt;/p&gt;

&lt;h2&gt;Simplifying things&lt;/h2&gt;

&lt;p&gt;The first step of achieving this would be to have something up and running in no-time. This would mean moving into a much simpler setup, which was something I liked, having started out in the era of simple text editors creating websites. I looked around on the net and found &lt;a href=&quot;http://jekyllrb.com/&quot;&gt;Jekyll&lt;/a&gt; (which I realize that I likely found so fast since my brother had spoken to me about it a few months earlier) a static site generator that works well with &lt;a href=&quot;http://pages.github.com&quot;&gt;GitHub Pages&lt;/a&gt;. After reading about Jekyll&amp;#39;s pro&amp;#39;s and con&amp;#39;s I decided that this was a good place to start. Resetting and going back to a more simplistic solution would refocus on content and getting things done.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s to the start of a new adventure on a clean slate where only the first block has been placed.&lt;/p&gt;
</description>
        <pubDate>Sun, 26 Oct 2014 00:00:00 +0000</pubDate>
        <link>http://davst.github.io/jekyll/update/hosting/2014/10/26/building-something-new.html</link>
        <guid isPermaLink="true">http://davst.github.io/jekyll/update/hosting/2014/10/26/building-something-new.html</guid>
        
        
        <category>jekyll</category>
        
        <category>update</category>
        
        <category>hosting</category>
        
      </item>
    
  </channel>
</rss>
