Presentation is loading. Please wait.

Presentation is loading. Please wait.

INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.

Similar presentations


Presentation on theme: "INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE."— Presentation transcript:

1 INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 sunny.shi@senecacollege.ca SENECA COLLEGE

2 2 Outline Questions? Asgns, labs? Canvas

3 Browser Object - window Method – open() Opens a new browser window winRef = window.open(URL,name,specs,replace); winRef = window.open('http://www.google.com','winName','width= 400,height=200, scrollbars = yes'); winRef.document.write(" write to new window in blue. "); 3 window_open.html

4 4 Browser Object - window Syntax: window.open(URL,name,specs,replace)  URL, optional. Open the page in the URL. If no URL, a new window with about:blank is opened  Name, Optional. Specifies the target attribute or the name of the window.  _blank - URL is loaded into a new window. default  _parent - URL is loaded into the parent frame  _self - URL replaces the current page  _top - URL replaces any framesets that may be loaded  name - The name of the window

5 5 Specs:

6 - an element to give you a drawing space in JavaScript on your Web pages. - only a container for graphics. Need a script to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, characters, and adding images. 6 canvas.html

7 Create a Canvas A canvas is a rectangular area on an HTML page, By default, element has no border and no content.  Always specify id (to be referred to in a script), and  width and height to define the size of the canvas. You can have multiple elements on one HTML page. 7

8 Create a Canvas add a border to the canvas: 8

9 Draw onto the Canvas Using JavaScript. 9 var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.fillStyle="#FF0000"; ctx.fillRect(10,10,20,50); ctx.strokeStyle = "green"; ctx.strokeRect(0,0,150,75);

10 Canvas Coordinates The canvas is a two-dimensional grid. The upper-left corner, coordinate (0,0) ctx.fillRect(0,0,150,75) – Start at the upper-left corner (0,0) and draw a 150x75 pixels rectangle. 10

11 Canvas - Paths Draw straight lines on a canvas: moveTo(x,y): the starting point of the line lineTo(x,y): the ending point of the line To actually draw the line, use stroke(). 11 var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.moveTo(0,0); ctx.lineTo(300,150); ctx.stroke();

12 Canvas – draw a circle arc(x,y,r,start,stop, boolean) x, y: coordinate Start: starting angle (e.g. 0) Stop: stopping angle (e.g., 2 * Matho.PI) Boolean: whether counterclockwise. Default is false. To actually draw the circle, use stroke() or fill(). 12

13 Canvas - Text Draw text on a canvas: font - defines the font properties for text fillText(text,x,y) - Draws "filled" text on the canvas 13 var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="30px Arial"; ctx.fillText("Hello World",10,50);

14 Canvas - Text strokeText(text,x,y) - Draws text on the canvas (no fill) 14 var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="30px Arial"; ctx.strokeText("Hello World",10,50);

15 Canvas - Gradients Gradients can be used to fill rectangles, circles, lines, text, etc. Shapes on the canvas are not limited to solid colors. Two types of gradients: createLinearGradient(x,y,x1,y1) - Creates a linear gradient createRadialGradient(x,y,r,x1,y1,r1) - Creates a radial/circular gradient Once we have a gradient object, we must add two or more color stops. The addColorStop() method specifies the color stops, and its position along the gradient. Gradient positions can be anywhere between 0 to 1. Set the fillStyle or strokeStyle property to the gradient, and then draw the shape, like a rectangle, text, or a line. 15

16 Canvas - Gradients context.createLinearGradient(x0,y0,x1,y1); Creates a linear gradient object. The gradient can be used to fill rectangles, circles, lines, text, etc. Tip: Use the addColorStop() method to specify different colors, and where to position the colors in the gradient object.addColorStop() 16

17 Canvas - Gradients // Create gradient var grd=ctx.createLinearGradient(0,0,200,0); grd.addColorStop(0.5,"red"); grd.addColorStop(0.6,"green"); // Fill with gradient ctx.fillStyle=grd; ctx.fillRect(10,10,150,80); 17

18 Canvas - Gradients context.createRadialGradient(x0,y0,r0,x1,y1,r1); var grd=ctx.createRadialGradient(75,50,5,90,60,100); creates a radial/circular gradient object. The gradient can be used to fill rectangles, circles, lines, text, etc. Use addColorStop() to specify different colors, and where to position the colors in the gradient object.addColorStop() 18

19 Canvas - Image Draw an image, canvas, or video onto the canvas. Can also draw parts of an image, and/or increase/reduce the image size Position the image on the canvas: – context.drawImage(img,x,y); Position the image, and specify width and height of the image: – context.drawImage(img,x,y,width,height); Clip the image and position the clipped part on the canvas: – context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); Note: the above img is an object. 19

20 20 Property Values

21 Canvas -Transparent Canvases are transparent by default. – Set a page background image, – put a canvas over it. – If nothing on the canvas, you can fully see the page background. 21

22 Canvas -Transparent Property: globalAlpha number 0 to 1, 0 is fully transparent and 1 is fully opaque. ctx.globalAlpha = 0.5; Property: fillStyle ctx2.fillStyle="rgba(0, 0, 200, 0.5)"; // the 4 th parameter is the opacity 22

23 Canvas Reference http://www.w3schools.com/tags/ref_canvas.asp http://www.w3schools.com/tags/ref_canvas.asp 23

24 24 Canvas Reference

25 25 Canvas Reference

26 26 Canvas Reference

27 Next Class Ajax & Questions

28 Thank you!


Download ppt "INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE."

Similar presentations


Ads by Google