What is it all about?
Recently we were requested to develop a very unusual consumption experience for exploring Fiori business content, enriched with context aware services (you can find here more information about it). While this is still in development, we thought some of you would be interested in learning about some of our considerations in selecting the technology for the UI.
Nir and I are developers in the SAP Portal group, in SAP Labs Israel, and we just love to bring Web UI to the extreme.
So what was on our plate? Complex shapes, dynamic graphics, multiple layers, complex and simultaneous animations, and, needless to say, mobile-friendly and absolutely smooth performance.
We also wanted: minimal learning curve, usage of a technology which is common in the industry, accessibility & text extraction, ability to embed external HTML content.
The good part: we were free to support a limited product-availability matrix, with only the most up-to-date Webkit browsers on PC and the retina iPad.
After a long investigation and numerous POC’s, we decided to go with CSS Transformations over HTML DOM elements with a pinch of Canvas. We were surprised to find out that the technology we had known for so long would fit us best, and while on this quest we learned to improve our skills using it.
Here are the technologies we investigated in order to find which best suit our requirements:
According to the HTML5 specification, the CANVAS element is:
“a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.”
The HTML5 Canvas element is an HTML tag similar to the <div>, <a>, or <table> tag, with the exception that its contents are rendered with JavaScript.
In order to leverage the HTML5 Canvas, we need to place the <canvas> tag somewhere inside the HTML document, access the <canvas> tag with JavaScript, create a context, and then utilize the HTML5 Canvas API to draw visualizations.
The canvas element is the actual DOM node that is embedded in the HTML page.
The canvas context is an object with properties and methods that you can use to render graphics inside the canvas element. The context can be 2D or webgl (3D).
For example, let’s draw a rectangle with HTML5 Canvas:
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.rect(188, 50, 200, 100);
context.fillStyle = 'yellow';
context.fill();
context.lineWidth = 7;
context.strokeStyle = 'black';
context.stroke();
</script>
</body>
The result:
HTML5 Canvas Advantages:
HTML5 Canvas Disadvantages:
Poor text rendering capabilities. Might not be the best choice for cases where accessibility is crucial
Events are binding to <canvas> tag and not on specific Web element, so in order to handle events for a specific Web element, you may need to do a lot of hard work or to rely on a Javascript library like KineticJS
SVG literally means Scalable Vector Graphics. It is a language for describing two-dimensional graphics in XML.
SVG allows for three types of graphic objects: vector graphic shapes (e.g., paths consisting of straight lines and curves), images, and text.
Graphical objects can be grouped, styled, transformed and composited into previously rendered objects. SVG also supports embedded or external CSS rules.
Example of SVG shapes:
<line x1="0" y1="0" x2="200" y2="200" stroke-width="1" stroke="#000"/>
<rect x="250" y="250" width="200" height="200" fill="rgb(234,234,234)"
stroke-width="1" stroke="rgb(0,0,0)"/>
SVG Advantages:
SVG Disadvantages:
Modern browsers (Safari, Chrome, Opera, Firefox, IE9+) provide Web developers with CSS animations capabilities.
The effect of CSS Transform is to modify the appearance of an element in the browser by translation, rotation, scaling or other means.
While CSS Transformation in itself is a powerful tool, the ability to animate the transformations using webkit-transition makes it even better, so you can define animation properties like duration, delay, easing etc.
For example:
.element {
-webkit-transition-property:width;
-webkit-transition-duration:1s;
-webkit-transition-timing-function:linear;
-webkit-transition-delay:2s;
}
In addition, you can combine multiple transform animations to a single element by listing them one after the other, so you can create complex animations easily.
You can predefine animation for an element and control the timing by @-webkit-keyframes. A keyframe defines the style that is applied for that moment within the animation. The animation engine smoothly interpolate styles between the keyframes.
Here is an example of a keyframe animation that changes the element size:
@-webkit-keyframes item-animation {
0% {
-webkit-transform: scale(0.2, 0.2);
}
45% {
-webkit-transform: scale(1.1, 1.1);
}
100% {
-webkit-transform: scale(1, 1);
}
}
In the same CSS file you can attach the keyframe animation to an element:
.element {
-webkit-animation: item-animation 0.5s;
-webkit-animation-fill-mode: both;
}
Furthermore, from a performance perspective, it is better to use CSS Transform in comparison to changing an object position and size by plain CSS properties (top, left, width, height).
Changing CSS properties by Javascript causes the browser to paint each frame, which result in a flickering transition. The CSS Transform version gets the screen element elevated onto its own layer on the GPU (called a RenderLayer). Now that it sits on its own layer, any 2D transform, 3D transform, or opacity changes, can happen purely on the GPU, which will stay extremely fast and still get us quick frame rates.
CSS Transform Advantages:
CSS Transform Disadvantages:
It is quite hard to declare one technology as the optimal one, since the decision, like always, depends on your needs.
After investigating each technology and doing some POCs, we found out which of our requirements are answered by each UI technology:
Requirement | HTML5 Canvas | SVG | CSS Transform |
Draw complex shapes | V | V | Sufficient (for required implementation) |
Dynamic graphics | V | V | V |
Complex animations | V | V | V |
Good performance | V | Partially (CPU costly) | V |
Mobile-friendly | V | V | V |
Minimal learning curve | X | X | V |
Common in industry | Partially | Partially | V |
Accessibility & text extraction | X | V | V |
Embed external HTML content | X | Partially (foreignObject) | V |
The ability to embed HTML content is an important requirement since we must make sure our solution will support integration with other UI libraries like UI5.
In addition to technical pros and cons for each UI technology, we also took into consideration our team’s wide experience in using HTML and CSS, which could help us provide a good solution in minimal time and learning curve.
In conclusion, our main choice is CSS Transform for animations, complex Web elements with embedded HTML, and simple event-handling for each element.
Additionally, we chose to use HTML5 Canvas for animations of non-Web elements like background scenery elements.
Hope you enjoyed this post,
Nir and Yotam.
Sources:
http://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/
http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/
http://msdn.microsoft.com/en-us/hh534406.aspx
http://creativejs.com/resources/requestanimationframe/
http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
http://www.w3schools.com/cssref/css3_pr_transform.asp
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transforms
https://developer.mozilla.org/en-US/docs/Web/CSS/transform
http://www.the-art-of-web.com/css/css-animation/#.Ulvx7FBkPzg
http://www.w3schools.com/cssref/css3_pr_animation-keyframes.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
9 | |
8 | |
7 | |
7 | |
6 | |
6 | |
6 | |
6 | |
4 |