The definitive article
If you want to understand Viewport, read this excellent article A tale of two
viewports. The two viewports are layout viewport and visual viewport. The article has detailed explanation and diagram illustration, is well organized, informative, easy to understand. I took some notes here as my reading notes.
Two viewports
Viewport is exactly equal to the browser window. The viewport is not an HTML construct, so you cannot influence it by CSS. It just has the width and height of the browser window on desktop. On mobile, there are the visual viewport and the layout viewport. The visual viewport is the part of the page that’s currently shown on-screen. The layout viewport is considerably wider than the visual viewport. The CSS layout are calculated relative to the layout viewport.
CSS pixel and device pixel
CSS pixel indictates how your style sheet is rendered, and device pixel is used to measure user's screen which never changes. At zoom level 100% one CSS pixel is exactly equal to one device pixel. Zooming to 200% makes one CSS pixel grow to four times the size of one device pixels. If the user zooms in you get less available space in the window (so window inner width/height is decreasing).
screen.width and screen.height are measured in device pixel. They contain the total width and height of the user’s screen (never change).
window.innerWidth and window.innerHeight are measured in CSS pixel. They are the inner dimensions of the browser window, tell exactly how much space the user currently has available for the CSS layout. They include scroll bars.
window.pageXOffset and window.pageYOffset are measured in CSS pixel. They contain the horizontal and vertical scrolling offsets of the document.
document.documentElement.clientWidth and -Height always gives the viewport dimensions, regardless of the dimensions of the <html> element. They don't include scroll bars.
document.documentElement.offsetWidth and -Height gives the dimensions of <html> element.
So basically we have 5 different views, namely screen > window > viewport -> scrolling -> html
Viewport width on mobile
For layout viewport width, different browsers have different value. Safari iPhone uses 980px, Opera 850px, Android WebKit 800px, and IE 974px. The <html> element takes the width of the layout viewport initially, and CSS is interpreted as if the screen were significantly wider than the phone screen. This makes sure that your site’s layout behaves as it does on a desktop browser.
The layout viewport width is always the same. If you rotate your phone, the visual viewport changes, but the browser adapts to this new orientation by zooming in slightly so that the layout viewport is again as wide as the visual viewport. (?? don't fully understand this because layout viewport width is expected to be wider than visual viewport) Web developers don’t care about the height, only about the width.
document.documentElement.clientWidth and -Height contain the layout viewport’s dimensions.
window.innerWidth and window.innerHeight contain the visual viewport's dimensions. Obviously the measurements change when the user zooms out or in, since more or fewer CSS pixels fit into the screen.
Event coordinates
pageX/Y is still relative to the page in CSS pixels
clientX/Y is relative to the visual viewport in CSS pixels (?)
screenX/Y is relative to the screen in device pixels
Media Query
There are two relevant media queries: width/height and device-width/device-height.
Use width and forget device-width — on desktop
Meta viewport
<meta name="viewport" content="width=320">
Wednesday, May 23, 2012
Tuesday, May 22, 2012
WebSocket
One major part of HTML5 is connectivity, and WebSocket is the one will change web in terms of communication protocol. Wikipedia defines WebSocket as below:
WebSocket is a web technology providing for bi-directional, full-duplex communications channels over a single TCP connection. The WebSocket API is being standardized by the W3C, and the WebSocket protocol has been standardized by the IETF as RFC 6455.
We are looking for different ways to do server push instead of client polling. This near real time server push mechanism provides better user experience on the web. Comet (long polling) defines push technologies applied to Ajax (XHR) web applications, XMLSocket flash also provides the ability to create flash socket, BOSH/XMPP is often used in web chat application. Then comes to HTML5 which has Server sent event (one way), and WebSocket (two ways) for client/server bi-directional communication. The old frequent polling mechanism was out-dated which was a short-term solution before we have these new technologies.
WebSocket is becoming popular and broadly adopted on both server side and client side. Java, .NET, PHP, Python and server side Javascript etc all have WebSocket support. Recently I looked at node.js and tried out socket.io, which is pretty awesome to provide a WebSocket server. There are many discussion about Nginx reverse proxy support for WebSocket, so that deployment diagram can have Nginx (serving static resources) in front of Socket.IO (WebSocket). This comparison table has part of the list http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations. Btw, Kaazing WebSocket Gateway is very famous due to HTML5 evangelist/trainer Peter Lubber from Kaazing. Another one is Union Platform which supports WebSocket too.
On the client side, there are many WebSocket libraries to support WebSocket programming. Using the native WebSocket API is hard (for now) due to browser support compatibility and the spec is not finalized. Old browsers don't have WebSocket support (fallback to Flash or Long Polling), newer browsers have unsecure WebSocket implementation, latest modern browsers start to implement WebSocket specification, and mobile browsers have different behaviors too. For browser WebSocket support compatibility, check out http://caniuse.com/#search=websocket With this kind of complexity or mess, we see more client libraries built for intermittent solutions. Usually the library has feature support detection, elegant fallback (polyfill), and native WebSocket API usage. Recently I looked at some Javascript libraries (Socket.IO client, SockJS and web-socket-js), and did read web-socket-js code which is use WebSocket API and fallback to Flash (WebSocketMain.swf). If you looked at socket.io, it has more protocol detection and fallback to cover more scenarios.
During the test using web-socket-js, I learnt Flash socket policy file configuration. Starting from Flash Player 9, there is a new strict access policy for Flash applications that make Socket or XMLSocket connections to a remote host. It now requires the presence of a socket policy file on the server. It means you have control to the server which your flash application connects to. There are two ways to deploy socket policy file: one is port 843 (not practical for production and firewall), the other is destination port (usually 80 or 443). In Nginx, we can set up the port 843 or use error page for 400 error as following
server {
listen 834;
server_name localhost;
location / {
rewrite ^(.*)$ /crossdomain.xml;
}
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 400 /crossdomain.xml;
}
The crossdomain.xml looks like:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false" to-ports="*"/>
<site-control permitted-cross-domain-policies="master-only" />
</cross-domain-policy>
Finally, don't forget to check out WebRTC from google, it enables real-time communication on browser based on HTML5 and Javascript. Google open source it to try to disrupt Microsoft skype and Apple Facetime.
WebSocket is a web technology providing for bi-directional, full-duplex communications channels over a single TCP connection. The WebSocket API is being standardized by the W3C, and the WebSocket protocol has been standardized by the IETF as RFC 6455.
We are looking for different ways to do server push instead of client polling. This near real time server push mechanism provides better user experience on the web. Comet (long polling) defines push technologies applied to Ajax (XHR) web applications, XMLSocket flash also provides the ability to create flash socket, BOSH/XMPP is often used in web chat application. Then comes to HTML5 which has Server sent event (one way), and WebSocket (two ways) for client/server bi-directional communication. The old frequent polling mechanism was out-dated which was a short-term solution before we have these new technologies.
WebSocket is becoming popular and broadly adopted on both server side and client side. Java, .NET, PHP, Python and server side Javascript etc all have WebSocket support. Recently I looked at node.js and tried out socket.io, which is pretty awesome to provide a WebSocket server. There are many discussion about Nginx reverse proxy support for WebSocket, so that deployment diagram can have Nginx (serving static resources) in front of Socket.IO (WebSocket). This comparison table has part of the list http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations. Btw, Kaazing WebSocket Gateway is very famous due to HTML5 evangelist/trainer Peter Lubber from Kaazing. Another one is Union Platform which supports WebSocket too.
On the client side, there are many WebSocket libraries to support WebSocket programming. Using the native WebSocket API is hard (for now) due to browser support compatibility and the spec is not finalized. Old browsers don't have WebSocket support (fallback to Flash or Long Polling), newer browsers have unsecure WebSocket implementation, latest modern browsers start to implement WebSocket specification, and mobile browsers have different behaviors too. For browser WebSocket support compatibility, check out http://caniuse.com/#search=websocket With this kind of complexity or mess, we see more client libraries built for intermittent solutions. Usually the library has feature support detection, elegant fallback (polyfill), and native WebSocket API usage. Recently I looked at some Javascript libraries (Socket.IO client, SockJS and web-socket-js), and did read web-socket-js code which is use WebSocket API and fallback to Flash (WebSocketMain.swf). If you looked at socket.io, it has more protocol detection and fallback to cover more scenarios.
During the test using web-socket-js, I learnt Flash socket policy file configuration. Starting from Flash Player 9, there is a new strict access policy for Flash applications that make Socket or XMLSocket connections to a remote host. It now requires the presence of a socket policy file on the server. It means you have control to the server which your flash application connects to. There are two ways to deploy socket policy file: one is port 843 (not practical for production and firewall), the other is destination port (usually 80 or 443). In Nginx, we can set up the port 843 or use error page for 400 error as following
server {
listen 834;
server_name localhost;
location / {
rewrite ^(.*)$ /crossdomain.xml;
}
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 400 /crossdomain.xml;
}
The crossdomain.xml looks like:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false" to-ports="*"/>
<site-control permitted-cross-domain-policies="master-only" />
</cross-domain-policy>
Finally, don't forget to check out WebRTC from google, it enables real-time communication on browser based on HTML5 and Javascript. Google open source it to try to disrupt Microsoft skype and Apple Facetime.
Thursday, May 17, 2012
Image Optimization
Recently we did pngout for more than 2000 files, but few of them could not be optimized due to bit depth is 64 or null. However, we have not covered other image types like JPEG, GIF. There are a lot of discussions and talks about image optimization for web performance. In our project so far, we just started the first step, but we followed 80/20 rule to optimize our website.
Generally speaking, image optimization has 2 categories: lossless and lossy.
The safe way and easy way is to do lossless image optimization during build process and development phase. There are some high level guidelines for this optimization effort:
PNG or JPG?
Optimize PNG file
As some tools are PNG-specific, while others only optimize DEFLATE, in general one must use a combination of 2 tools in sequence for optimal compression: one which optimizes filters (and removes ancillary chunks), and one which optimizes DEFLATE. Most commonly, OptiPNG is used for the first (non-DEFLATE) step, and either of AdvanceCOMP or PNGOUT is used for the DEFLATE step. For more info or detailed comparison among PNG, JPEG, TIFF, check out http://en.wikipedia.org/wiki/Portable_Network_Graphics
Frequently used Tools
pngcrush
pngout
OptiPNG
ImageOptim
imgopt
Smush.it
Generally speaking, image optimization has 2 categories: lossless and lossy.
- Lossless Image Compression: Remove surplus info from image (Smush.it)
- Lossy Image Compression: Match image quality to screen resolution
The safe way and easy way is to do lossless image optimization during build process and development phase. There are some high level guidelines for this optimization effort:
- Don't use GIF unless you need animated GIF
- Crush PNG (pngout)
- Crush JPG (jpegtran)
- Know PNG24 to PNG8 Conversion Guidelines
- Know the time to choose JPG over PNG (Don't use PNG for everything)
- Combine images (csssprite/spriteMe)
- Compress favicon
- Crop and compress user uploaded avatar
PNG or JPG?
- PNG is a lossless compression format
- JPG doesn't support transparency
- PNG works best for vector type graphics with hard lines.
- JPG works best for anything with complex gradients like photo, screenshot etc
- Use imagemagick to batch convert PNG to JPG
Optimize PNG file
- color depth
Color depth can range from 1 to 64 bits per pixel, reduce color depth can reduce file size
- ancillary chunks
PNG supports metadata—remove it can reduce file size
- interlacing
As each pass of the Adam7 algorithm is separately filtered, this can increase file size
- filter
The choice of filter is potentially very variable and impacting file size
- compression
DEFLATE compressors can produce smaller files.
As some tools are PNG-specific, while others only optimize DEFLATE, in general one must use a combination of 2 tools in sequence for optimal compression: one which optimizes filters (and removes ancillary chunks), and one which optimizes DEFLATE. Most commonly, OptiPNG is used for the first (non-DEFLATE) step, and either of AdvanceCOMP or PNGOUT is used for the DEFLATE step. For more info or detailed comparison among PNG, JPEG, TIFF, check out http://en.wikipedia.org/wiki/Portable_Network_Graphics
Frequently used Tools
pngcrush
pngout
OptiPNG
ImageOptim
imgopt
Smush.it
Subscribe to:
Posts (Atom)