6. Drawing the logo
string of text, an angeld path, a squeare, a triangle
Adding Text
c o n t e x t . f o n t = ' i t a l i c 4 0 p x s a n s - s e r i f ' ;
c o n t e x t . t e x t B a s e l i n e = ' t o p ' ;
c o n t e x t . f i l l T e x t ( ' A w e s o m e C o ' , 6 0 , 0 ) ;
7. Drawing the Logo
Drawing lines
c o n t e x t . lineWidth=2;
c o n t e x t . beginPath();
c o n t e x t . moveTo(0,40);
c o n t e x t . lineTo(30,0);
c o n t e x t . lineTo(60,40);
c o n t e x t . lineTo(285,40);
c o n t e x t . stroke();
c o n t e x t . c l o s e P a t h ( )
8. Drawing the Logo
Moving the Origin
c o n t e x t . s a v e ( ) ;
c o n t e x t . t r a n s l a t e ( 2 0 , 2 0 ) ;
c o n t e x t . f i l l R e c t ( 0 , 0 , 2 0 , 2 0 ) ;
9. Drawing the Logo
Drawing a triangle
c o n t e x t . f i l l S t y l e = ' # f f f '
c o n t e x t . s t r o k e S t y l e = ' # f f f ' ;
c o n t e x t . l i n e W i d t h = 2 ;
c o n t e x t . b e g i n P a t h ( ) ;
c o n t e x t . m o v e T o ( 0 , 2 0 ) ;
c o n t e x t . l i n e T o ( 1 0 , 0 ) ;
c o n t e x t . l i n e T o ( 2 0 , 2 0 ) ;
c o n t e x t . l i n e T o ( 0 , 2 0 ) ;
c o n t e x t . f i l l ( ) ;
c o n t e x t . c l o s e P a t h ( ) ;
c o n t e x t . r e s t o r e ( ) ;
10. Drawing the Logo
AddingColors
// context.fillStyle = "#f00";
// context.strokeStyle = "#f00";
var gradient = context.createLinearGradient(0, 0, 0, 40);
gradient.addColorStop(0, '#a00'); // red
gradient.addColorStop(1, '#f00'); // red
context.fillStyle = gradient;
context.strokeStyle = gradient;
15.
Turning Our HTML into a Bar Graph
function canvasGraph(){ var title = $('#graph_data h1').text();
var labels = $("#graph_data>ul>li>p[data-name]").map
(function(){ return $(this).attr("data-name"); });
var percents = $("#graph_data>ul>li>p[data-percent]").map
(function(){ return parseInt($(this).attr("data-percent")); });
var bar = new RGraph.Bar('browsers', percents); bar.Set
('chart.gutter', 50); bar.Set('chart.colors', ['red']); bar.Set
('chart.title', title); bar.Set('chart.labels', labels); bar.Draw();
}
16. Displaying Alternative Content
var canvas = document.getElementById('browsers');
if (canvas.getContext)
{
$('#graph_data').hide();
canvasGraph();
}
17. Falling back
function divGraph(barColor, textColor, width, spacer,
lblHeight)
{
$('#graph_data ul').hide();
var container = $("#graph_data");
container.css( { "display" : "block", "position" : "relative",
"height": "300px"} );
$("#graph_data>ul>li>p").each(function(i){
var bar = $("<div>" + $(this).attr("data-percent") + "%
</div>");
var label = $("<div>" + $(this).attr("data-name") +
"</div>"); var commonCSS = { "width": width + "px",
"position" : "absolute", "left" : i * (width + spacer) + "px"}; var
barCSS = { "background-color" : barColor, "color" : textColor,