That Gauge is COOL!  http://thecodeplayer.com/walkthrough/make-gauge-charts-using-canvas-and-javascript.

Slides:



Advertisements
Similar presentations
HTML Basics Customizing your site using the basics of HTML.
Advertisements

HTML and CSS. HTML Hyper Text Markup Language Tells browser how to display text and images.
Cascading Style Sheets (CSS). Cascading Style Sheets With the explosive growth of the World Wide Web, designers and programmers quickly explored and reached.
1 Cascading Style Sheets Continued Different kinds of selectors in a style sheet –Simple- Pseudo-Class –Contextual- Pseudo-Element –Class Image Styles.
Cascading Style Sheets
/k/k 1212 Cascading Style Sheets Part one Marko Boon
Presenter: James Huang Date: Sept. 26,  Introduction  Basics  Lists  Links  Forms  CSS 2.
Advance CSS (Menu and Layout) Miftahul Huda. CSS Navigation MENU It's truly remarkable what can be achieved through CSS, especially with navigation menus.
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
CSS normally control the html elements. Three Ways to Insert CSS There are three ways of inserting a style sheet: External style sheet Internal style.
Programming games More drawing. Text. Radian measure. Faces. Homework: Do your own drawings. Create index.html file. Upload work.
CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from Canvas is.
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
HTML, HTML5 Canvas, JavaScript PART 3 LOOKING MORE CLOSELY AT FULL BLOWN GAME DESIGN A PATTERNED BACKGROUND (CREATED WTIH LOOPS) WITH A MOVING OBJECT CHALLENGES.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
Session: 17. © Aptech Ltd. 2Canvas and JavaScript / Session 17  Describe Canvas in HTML5  Explain the procedure to draw lines  Explain the procedure.
Element. The element Used to dynamically draw graphics using javascript. Capable of drawing paths, circles, rectangles, text, and images.
CSS Tutorial 1 Introduction Syntax How to use style specifications. Styles.
CS 174: Web Programming October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Creating Graphics for the Web Learning & Development Team Telerik Software Academy.
Colors & Fonts Building a Website Lesson 7. Font Font The tag specifies the font face, font size, and color of text. The tag can have any or all of these.
Programming games Show your virtual somethings. Demonstrate Hangman and Black Jack. Show credit card (or other form input & calculation) Homework: [Complete.
CSS Layout Cascading Style Sheets. Lesson Overview  In this lesson, you will learn:  CSS properties for margin  CSS properties for dimensions (width,
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
Advanced Topics Lecture 8 Rachel A Ober
 Look especially at › File Tips and Shortcuts › Student video.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
1 HTML. 2 Full forms WWW – world Wide Web HTTP – Hyper Text Transfer Protocol HTML – Hyper Text Markup Language.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
>> CSS Layouts. Recall Properties of Box – Border (style, width, color) – Padding – Margin – Display – Background – Dimension Welcome Padding Area Border.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
1 Cascading Style Sheets
Game Development with JS and Canvas 2D
CSS.
What is a Function Expression?
Web Systems & Technologies
Semester - Review.
CSS Layouts: Grouping Elements
Unit 3 - Review.
>> Introduction to CSS
>> The “Box” Model
Canvas Notes
Introduction to Web programming
Cascading Style Sheets (Layout)
>> CSS Layouts.
Chapter 7 Page Layout Basics Key Concepts
CSS.
Website Design 3
Web Development & Design Foundations with HTML5 7th Edition
מדעי המחשב ורובוטיקה בחט"ב
Introduction to Web programming
CSS.
The Web Wizard’s Guide To DHTML and CSS
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Cascading Style Sheets™ (CSS)
Drawing Graphics in JavaScript
CSS Styles Introduction.
Context of what we are doing.
HTML5 – Canvas JavaScript Simple Drawing
Exercise 9 Skills You create and use styles to create formatting rules that can easily by applied to other pages in the Web site. You can create internal.
Graphics Canvas.
CSc 337 Lecture 20: Canvas.
Programming games Share your plans for your virtual something.
Web Programming and Design
JavaScript – Let’s Draw!
Presentation transcript:

That Gauge is COOL!  http://thecodeplayer.com/walkthrough/make-gauge-charts-using-canvas-and-javascript

tons of great examples… canvas is a little tricky css is somewhat hidden but used! set/clear intervals help make things animated interesting combinations of functions hopefully easy to play with and customize the “walkthough” is fun…

html…. <canvas id="canvas" width="300" height="300"> used to create a container in which a script can draw graphics!

css… body { background: #333; } /*Centering the gauge*/ #canvas { display: block; width: 300px; margin: 100px auto; /*Custom font for numbers*/ @font-face { font-family: "bebas"; src: url("http://thecodeplayer.com/uploads/fonts/BebasNeue.otf");

width? what is the difference between the width in HTML and the width in CSS (besides the px?) a style sheet can size the element during rendering, so it can be scaled up or down… try this!

display the display property defines how a certain HTML element should be displayed block The element is displayed as a block element (like paragraphs and headers). A block element has some whitespace above and below it and does not tolerate any HTML elements next to it inline This is default. The element is displayed as an inline element. An inline element has no line break before or after it, and it tolerates HTML elements next to it list-item The element is displayed as a list-item, which means that it has a bullet in front of it

CANVAS?!? There is a canvas tag in html a #canvas in the .css file! given an id=“canvas” a #canvas in the .css file! a var canvas in the .js file! It must be very important! 

skeleton of the code… window.onload = function(){ //canvas initialization var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); //dimensions var W = canvas.width; var H = canvas.height; //Variables ... function init() { } function draw() … animation_loop = setInterval(animate_to, 1000/difference); //function to make the chart move to new degrees function animate_to() …. init(); //Lets add some animation for fun draw(); redraw_loop = setInterval(draw, 2000); //Draw a new chart every 2 seconds

Woah… There are functions in a function? Yup! They share variables… window.onload = function(){ //canvas initialization var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); //dimensions var W = canvas.width; var H = canvas.height; //Variables var degrees = 0; var new_degrees = 0; var difference = 0; var color = "lightgreen"; //green looks better to me var bgcolor = "#222"; var text; var animation_loop, redraw_loop;

draw() function draw() { //Cancel any movement animation if a new chart is requested if(typeof animation_loop != undefined) clearInterval(animation_loop); //random degree from 0 to 360 new_degrees = Math.round(Math.random()*360); difference = new_degrees - degrees; //This will animate the gauge to new positions //The animation will take 1 second //time for each frame is 1sec / difference in degrees animation_loop = setInterval(animate_to, 1000/difference); }

clearing and setting intervals setInterval() - executes a function, over and over again, at specified time intervals returns a “handle” you can use to clear it! myVar=setInterval("javascript function",milliseconds); clearInterval(myVar);

animate_to() function animate_to() { //clear animation loop if degrees reaches to new_degrees if(degrees == new_degrees) clearInterval(animation_loop); if(degrees < new_degrees) degrees++; else degrees--; init(); }

function init() { //Clear the canvas everytime a chart is drawn ctx function init() { //Clear the canvas everytime a chart is drawn ctx.clearRect(0, 0, W, H); //Background 360 degree arc ctx.beginPath(); ctx.strokeStyle = bgcolor; ctx.lineWidth = 30; ctx.arc(W/2, H/2, 100, 0, Math.PI*2, false); //you can see the arc now ctx.stroke(); //gauge will be a simple arc //Angle in radians = angle in degrees * PI / 180 var radians = degrees * Math.PI / 180; ctx.strokeStyle = color; //The arc starts from the rightmost end. If we deduct 90 degrees from the angles //the arc will start from the topmost end ctx.arc(W/2, H/2, 100, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false); //you can see the arc now //Lets add the text ctx.fillStyle = color; ctx.font = "50px bebas"; text = Math.floor(degrees/360*100) + "%"; //Lets center the text //deducting half of text width from position x text_width = ctx.measureText(text).width; //adding manual value to position y since the height of the text cannot //be measured easily. There are hacks but we will keep it manual for now. ctx.fillText(text, W/2 - text_width/2, H/2 + 15); }