CMPE 280 Web UI Design and Development September 12 Class Meeting

Slides:



Advertisements
Similar presentations
HTML5 CANVAS.  Rectangles  Arcs  Lines  Features we won’t look at  Images  Drag and drop  Animation  Widely supported DRAWING PICTURES IN HTML.
Advertisements

Lecture # 11 JavaScript Graphics. Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG), as the name implies, are - scalable (without pixelation):
CSS level 3. History of CSS CSS level 1 – original CSS CSS level 2 CSS level 2 Revision 1 (CSS2.1) – up to CSS2.1 monolithic structure CSS level 3 – specification.
Basics of Web Design Chapter 6 More CSS Basics Key Concepts.
Programming Club IIT Kanpur
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Chapter 7 Creating Graphics. Chapter Objectives Use the Pen tool Reshape frames and apply stroke effects Work with polygons and compound paths Work with.
Web Programming Language Week 13 Ken Cosh HTML 5.
HTML 5 Tutorial Chapter 5 Canvas. Canvas The tag is used to display graphics. The Canvas is a rectangular area we control each and every pixel of it.
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.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
© 2010 Delmar, Cengage Learning Chapter 7 Creating Graphics.
Intro to the Canvas Canvas is an HTML5 element If you see this, your browser doesn't support the canvas Canvas is manipulated with javascript c = document.getElementById("mycanvas");
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
ADOBE INDESIGN CS3 Chapter 7 CREATING GRAPHICS. Chapter 72 Introduction InDesign allows you to create graphics using tools in InDesign The Pen Tool can.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
Can SAS Do Canvas? Creating Graphs Using HTML 5 Canvas Elements Edwin J. van Stein Astellas Pharma Global Development.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Web Development & Design Foundations with HTML5 8th Edition
3 1 2 Significant Figures (Advanced) Rotating numbers on a curved path
CMPE 280 Web UI Design and Development September 14 Class Meeting
Tutorial 4 Topic: CSS 3.0 Li Xu
Canvas Notes
Levels of Organization Ecology Flow
Animated picture effects for PowerPoint slides
Web Programming Language
TRAFFIC LAWYERS OF TEXAS
CMPE 280 Web UI Design and Development February 20 Class Meeting
Chapter 6 More CSS Basics Key Concepts
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Physics Careers February1st, 2017
Smart Graphic Layout TOPIC statement
TermiGator Ebrahem Hamdan University of Florida
РОСІЙСЬКА МОВА Кількість завдань - 51 Час на виконання – 150 хв.
ФІЗИКА Кількість завдань - 34 Час на виконання – 180 хв.
سید محسن هاشمی نسب و رضایی
Basics of Web Design Chapter 6 More CSS Basics Key Concepts
Custom animation effects: curve up and grow (Intermediate)
PowerPoint Heaven PowerPoint Heaven. PowerPoint Heaven PowerPoint Heaven.
ISC440: Web Programming II Ch14: HTML5 Canvas
بسم الله الرحمن الرحیم مركز بهمن استاندارد- مديريت ارزيابي و مانيتورينگ كيفي.
Order of Operations Problems
The Canvas.
TAB ONE TAB TWO TAB THREE TAB FOUR TAB FIVE
1.
Order of Operations Problems
Cascading Style Sheets Color and Font Properties
Levels of Organization Ecology Flow
Basics of Web Design Chapter 6 More CSS Basics Key Concepts
3 1 2 Significant Figures (Advanced) Rotating numbers on a curved path
SEEM4570 Tutorial 3: CSS + CSS3.0
Balancing Chemical Equations
موضوع بحث: تعریف علم اصول جلسه 43.
اشاره به نتایج قیاس های فقهی گاهی، حکم شرعی است
علم اصول، «نفس قواعد» است نه «علم به قواعد»
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
3 1 2 Significant Figures (Advanced) Rotating numbers on a curved path
Drawing Graphics in JavaScript
CSc 337 Lecture 21: Canvas.
Graphics Canvas.
Smart Graphic Layout TOPIC statement
قاعده لا ضرر، تنها در شبهات حکمیه جاری است
جلسه 34.
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
Balancing Chemical Equations
TITLE BYOT Half Circle (Advanced)
1 ج : اشاره بعضی از اصولیون به تعریف ترکیبی آخوند با «یک لفظ»
Presentation transcript:

CMPE 280 Web UI Design and Development September 12 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

The HTML5 Canvas Object HTML5 introduces the drawing canvas. The <canvas> tag provides a graphics context. A rich set of drawing operations. Execute using JavaScript. Replaces the need for Flash or Java. Used by many game developers. Universally supported by modern browsers.

Simple Canvas Demo <canvas id = "canvas" width = "200" height = "200"> <p>Your browser does not support the canvas tag.</p> </canvas> canvas/simple.html function draw() { var canvas = document.getElementById("canvas"); var con = canvas.getContext('2d'); con.strokeStyle = "black"; con.strokeRect(0, 0, 200, 200); con.fillStyle = "red"; con.fillRect(40, 140, 150, 50); } Demo

Canvas Basics Two main canvas drawing operations: stroke: draw a line fill: fill in a shape Specify strokestyle and fillstyle. Basic shapes: lines, rectangles, arcs, text Create paths to draw complex shapes. Draw images. Alter pixels.

Ways to Specify Colors By name: red, silver, gray, etc. RGB with integers 0-255 or percentages: rgb(10, 250, 100) or rgb(100%, 100%, 0%) RGBA with alpha transparency. HSL and HSLA RGB with six-digit hex values: #FF0000 is red, #FFFF00 is yellow. RGB with three-digit hex values: #F00 is red, #FF0 is yellow.

Gradients Fill a shape with a gradient. Linear gradient or radial gradient. Color stop: Specifies a color to add to a gradient and a position along the gradient. Position 0 through 1 0 = beginning of the gradient 1 = end of the gradient

Gradients, cont’d <canvas id = "linear" height = "200" width = "200"> <p>Canvas not supported!</p> </canvas> <canvas id = "radial" canvas/gradients.html

Gradients, cont’d function draw() { var linear = document.getElementById("linear"); var radial = document.getElementById("radial"); // Linear gradient var con = linear.getContext("2d"); lGrad = con.createLinearGradient(0, 0, 100, 200); lGrad.addColorStop(0, "#FF0000"); // red lGrad.addColorStop(0.5, "#00FF00"); // green lGrad.addColorStop(1, "#0000FF"); // blue con.fillStyle = lGrad; con.fillRect(0, 0, 200, 200); ... } canvas/gradients.html

Gradients, cont’d function draw() { ... // Radial gradient con = radial.getContext("2d"); rGrad = con.createRadialGradient(50, 50, 0, 100, 100, 125); rGrad.addColorStop(0, "#FF0000"); // red rGrad.addColorStop(0.5, "#00FF00"); // green rGrad.addColorStop(1, "#0000FF"); // blue con.fillStyle = rGrad; con.fillRect(0, 0, 200, 200); } canvas/gradients.html

Gradients, cont’d createLinearGradient(0, 0, 100, 200) starting position ending position radius of inner circle radius of outer circle createRadialGradient(50, 50, 0, 100, 100, 125) center position of inner circle center position of outer circle

Rectangle Operations strokeRect(x, y, w, h) fillRect(x, y, w, h) Draw the outline of a rectangle with the upper left corner at position x, y and width w and height h. Use the current strokeStyle and lineWidth. fillRect(x, y, w, h) Draw a filled-in rectangle. Fill with the current fillStyle. clearRect(x, y, w, h) Erase a rectangle by filling in with the current background color.

Rectangle Operations, cont’d <canvas id = "canvas" height = "200" width = "200" > <p>Canvas not supported</p> </canvas> canvas/rectangles.html function draw() { var canvas = document.getElementById("canvas"); var con = canvas.getContext("2d"); con.fillStyle = "blue"; con.strokeStyle = "black"; con.lineWidth = "5"; con.strokeRect( 0, 0, 200, 200); con.fillRect (10, 10, 180, 80); con.clearRect ( 0, 50, 90, 70); }

Drawing Text fillText(string, x, y) Display the characters of string at offset x and baseline at y. strokeText(string, x, y) Display the outline of the characters of string. <canvas id = "canvas" height = "200" width = "280" > <p>Canvas not supported</p> </canvas>

Drawing Text, cont’d function draw() { canvas/text.html function draw() { var canvas = document.getElementById("canvas"); var con = canvas.getContext("2d"); con.strokeStyle = "black"; con.strokeRect(0, 0, 280, 200); con.font = "40pt sans-serif"; con.fillStyle = "red"; con.fillText ("CMPE 280", 5, 75); con.strokeText("CMPE 280", 5, 150); }

Shadows Add a shadow to any object you draw on the canvas. Canvas shadow attributes: shadowOffsetX, shadowOffsetY How much to move the shadow along the x and y axes. shadowColor Default is black. shadowBlur 0: crisp and sharp 5: softer and lighter

Shadows, cont’d function draw() { var canvas = document.getElementById("canvas"); var con = canvas.getContext("2d"); con.strokeStyle = "black"; con.strokeRect(0, 0, 200, 100); con.shadowOffsetX = 3; con.shadowOffsetY = 3; con.shadowColor = "gray"; con.shadowBlur = 5; con.font = "40pt sans-serif"; con.fillStyle = "red"; con.fillText("CMPE 280", 5, 65); } canvas/shadow.html

Paths A path records “pen motion”. function draw() { ... con.strokeStyle = "red"; con.fillStyle = "green"; con.lineWidth = "10"; con.beginPath(); con.moveTo(25, 25); con.lineTo(150, 150); con.lineTo(25, 150); con.lineTo(25, 100); con.closePath(); con.stroke(); con.fill(); } A path records “pen motion”. canvas/path.html

Line Attributes strokeStyle lineWidth lineJoin lineCap in pixels miter: sharp corners round: rounded corners bevel: squared-off corners lineCap round: rounded edges butt: squared-off edges cut off exactly at line width square: like butt but with a small added length

Line Attributes, cont’d Line joins: con.lineJoin = "round" con.strokeStyle = "red"; con.beginPath(); con.moveTo(20, 50); con.lineTo(30, 20); con.lineTo(40, 50); con.stroke(); con.closePath(); con.lineJoin = "bevel" con.strokeStyle = "blue"; con.moveTo(60, 50); con.lineTo(70, 20); con.lineTo(80, 50); con.lineJoin = "miter"; con.strokeStyle = "green" con.beginPath(); con.moveTo(100, 50); con.lineTo(110, 20); con.lineTo(120, 50); con.stroke(); con.closePath(); canvas/lines.html

Line Attributes, cont’d Line caps: line joins con.lineCap = "round"; con.strokeStyle = "red" con.beginPath(); con.moveTo(20, 100); con.lineTo(180, 100); con.stroke(); con.closePath(); con.lineCap = "butt"; con.strokeStyle = "blue" con.moveTo(20, 120); con.lineTo(180, 120); line caps con.lineCap = "square"; con.strokeStyle = "green" con.beginPath(); con.moveTo(20, 140); con.lineTo(180, 140); con.stroke(); con.closePath(); canvas/lines.html

Arcs and Circles arc(70, 30, 50, 0, Math.PI, false) Compass Radians starting and ending angles in radians radius arc(70, 30, 50, 0, Math.PI, false) center position drawing direction true: counter-clockwise false: clockwise Compass Radians East South Math.PI/2 West Math.PI North 3*Math.PI/2

Arcs and Circles, cont’d // Stroked closed half-circle con.strokeStyle = "green"; con.lineWidth = "5"; con.beginPath(); con.arc(70, 30, 50, 0, Math.PI, false); con.closePath(); con.stroke(); // Filled full circle con.fillStyle = "rgba(255, 0, 0, 0.5)"; con.arc(70, 100, 50, 0, 2*Math.PI, true); con.fill(); // Stroked quarter arc con.strokeStyle = "blue"; con.arc(130, 120, 50, Math.PI/2, 3*Math.PI/2, true); canvas/arcs.html

Quadratic Curve Curve with starting and ending points. A control point affects the shape of the curve. quadraticCurveTo(100, 10, 190, 190) control point position ending point position

Quadratic Curve, cont’d con.strokeStyle = "green"; con.beginPath(); con.moveTo(10,190); con.quadraticCurveTo(100, 10, 190, 190); con.stroke(); con.closePath(); // Blue dots: start and end points. drawDot(10, 190, "blue"); drawDot(190, 190, "blue"); // Red dot: control point. drawDot(100, 10, "red"); canvas/quadratic.html function drawDot(x, y, color) { con.fillStyle = color; con.beginPath(); con.arc(x, y, 10, 0, 2*Math.PI, true); con.fill(); con.closePath(); }

Bézier Curve Similar to the quadratic curve, but with two control points. bezierCurveTo(100, 10, 100, 190, 190, 190) control point #1 position control point #2 position ending point position

Bézier Curve, cont’d canvas/bezier.html con.strokeStyle = "green"; con.beginPath(); con.moveTo(10,10); con.bezierCurveTo(100, 10, 100, 190, 190, 190); con.stroke(); con.closePath(); // Blue dots: start and end points. drawDot(10, 10, "blue"); drawDot(190, 190, "blue"); // Red dots: control points. drawDot(100, 10, "red"); drawDot(100, 190, "red");

Drawing an Image Draw the entire image: Draw part of the image: destination position drawImage(image, 10, 10, 180, 135) destination width and height image upper left corner position destination position drawImage(image, 150, 70, 190, 120, 10, 10, 180, 110) image width and height destination width and height

Drawing an Image, cont’d var image = new Image(); image.src = "images/RonCats.jpg"; ... con.drawImage(image, 10, 10, 180, 135); con.drawImage(image, 150, 70, 190, 120, 10, 10, 180, 110); canvas/images.html

Altering Pixels An image is displayed in a canvas as an array of pixels. Each pixel takes up four elements of the array, R, G, B, and A, each value can be 0 – 255. Convert an image to imageData to get the array of RGBA values. You can then alter each value.

Altering Pixels, cont’d var imageData = con.getImageData(10, 10, 180, 135); for (var row = 0; row < 135; row++) { for (var col = 0; col < 180; col++) { var index = 4*(col + row*imageData.width); var r = imageData.data[index]; var g = imageData.data[index+1]; var b = imageData.data[index+2]; var a = imageData.data[index+3]; g = r; b = r; imageData.data[index] = r; imageData.data[index+1] = g; imageData.data[index+2] = b; imageData.data[index+3] = a; } con.putImageData(imageData, 10, 10); Get the image data from the canvas. Separate each pixel into R, G, B, and A What is the result? canvas/pixels.html Alter each pixel. Set the altered pixel back into the image data. Put the altered image data back into the canvas.

Altering Pixels, cont’d Original: Altered: