HTML5: Canvas ATLS 3020 - Digital Media 2 Week 9 - Day 1 Schedule - - PowerPoint PPT Presentation

html5 canvas
SMART_READER_LITE
LIVE PREVIEW

HTML5: Canvas ATLS 3020 - Digital Media 2 Week 9 - Day 1 Schedule - - PowerPoint PPT Presentation

HTML5: Canvas ATLS 3020 - Digital Media 2 Week 9 - Day 1 Schedule Today Monday, March 16 jQuery Review Labs 9, 10, 11, 12 are due HTML5 Canvas Quiz 2 Work day Wednesday, March 11 Wednesday, March


slide-1
SLIDE 1

HTML5: Canvas

ATLS 3020 - Digital Media 2 Week 9 - Day 1

slide-2
SLIDE 2
  • Wednesday, March 11

○ Project 2 Proposal is due ○ jQuery Animations

  • Monday, March 16

○ Labs 9, 10, 11, 12 are due ○ Quiz 2 ○ Work day

  • Wednesday, March 18

○ Project 2 Critiques

Schedule

  • Today

○ jQuery Review ○ HTML5 Canvas

slide-3
SLIDE 3

Project 2: Animation

Create an animation in HTML5, Javascript, and/or jQuery. This can be a stand alone animation or you can add it on to Project 1. Be creative!

Your project plan should include the following: ○ description of your project ○ pseudocode for algorithms and program flow ○ visual design template or example ○ brief description of how you will test your project

slide-4
SLIDE 4

Project 2: Rubric

25% Concept

concept and design well-thought-out and well-executed

25% Aesthetics

good look and feel of the whole webpage, works on all screen sizes

25% Creativity

project has some personal flair

25% Technicality

code is running correctly (on all browsers) with no bugs

slide-5
SLIDE 5

Canvas

  • HTML5 Element
  • Create graphic objects in HTML and manipulate them in

javascript and CSS3

  • Similar to Photoshop or Illustrator Canvas
slide-6
SLIDE 6

Canvas: Manipulation

To access the canvas you need both HTML and Javascript.

<canvas id="my_canvas" width="300" height="300"></canvas> HTML canvas = document.getElementById("my_canvas"); context = canvas.getContext("2d"); Javascript

HTML: Create the canvas element where you want it on on the webpage JS: 1. Get the html element.

  • 2. Get the element’s context. We will draw on the context.
slide-7
SLIDE 7

Rectangle Functions

fillRect(x, y, width, height) Javascript

Draws a filled rectangle

strokeRect(x, y, width, height) Javascript

Draws a rectangular outline

clearRect(x, y, width, height) Javascript

Clears the rectangle area Remember, these are functions of the canvas’ context, so to implement these functions, you need to use this syntax.

context.function(. . .); Javascript

slide-8
SLIDE 8

Drawing Properties

fillStyle = "hex color" Javascript

Sets the fill color, pattern, or gradient

strokeStyle = "hex color" Javascript

Sets the stroke color, pattern, or gradient

The default fill and stroke color is black.

These properties are also part of the canvas’ context. To change the color of what is being drawn on the context, you must change the color of the context first.

// blue square context.fillStyle = "#00f"; context.fillRect(0, 0, 100, 100); // red square context.fillStyle = "#f00"; context.fillRect(100, 100, 100, 100); Javascript lineWidth = number Javascript

Sets the line thickness

slide-9
SLIDE 9

Paired Exercise

Re-create this painting (or something similar)

slide-10
SLIDE 10

Gradients

createLinearGradient(x0, y0, x1, y1) Javascript

Paints along a line from (x0, y0) to (x1, yx)

createRadialGradient(x0, y0, r0, x1, y1, r1) Javascript

Paints along a cone between 2 circles

addColorStop(position, color) Javascript

Adds a color point to your gradient, position is between 0 and 1

var g = context.createLinearGradient(0, 0, 100, 100); g.addColorStop(0, "red"); g.addColorStop(1, "blue"); context.fillStyle = g; Javascript

Example Code

1. Create variable that holds linear gradient 2. Add color stops (as many as you want) 3. Set fillStyle equal to the gradient

slide-11
SLIDE 11

Paths

beginPath() Javascript

Begin the path (like placing a pencil)

moveTo(x, y) Javascript

Moves pencil to specific place (without drawing)

closePath() Javascript

End the path You can make unique shapes by using “paths”

lineTo(x, y) Javascript

Draws to this point

fill() stroke() Javascript

Draw the fill and/or stroke of the path

slide-12
SLIDE 12

Images

drawImage(image, dx, dy) Javascript

Takes an image: draws it

drawImage(image, dx, dy, dw, dh) Javascript

Takes an image: scales and draws it You can do a lot a image manipulation with the canvas

drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) Javascript

Takes an image: clips, scales, and draws it

var i = new Image(); i.src = "images/my_image.jpg"; i.onload = function() { drawImage(i, 10, 10); } Javascript

Example Code

1. Create an image object (this is like creating an image in javascript and storing it in a variable) 2. Set the source of the image 3. Create a load event. Set it equal to a function. 4. Inside of the function, call the drawImage() function.

slide-13
SLIDE 13

Paired Exercise

  • 1. Comment out your last drawing.
  • 2. Create a new drawing (on the same canvas) with these 3

things:

  • a. a rectangle filled with a gradient
  • b. a diamond (created with a path)
  • c. an image
slide-14
SLIDE 14

Circles

arc(x, y, radius, startAngle, endAngle, anticlockwise) Javascript

To draw circles or arcs use the “arc” function as a path

  • x and y are the coordinates of the

center of the circle on which the arc should be drawn

  • radius is the radius of the arc
  • startAngle and endAngle

define the start and end points of the arc in radians (not degrees)

  • anticlockwise is a Boolean

value - true draws the arc anticlockwise, false clockwise

slide-15
SLIDE 15

context.beginPath(); context.arc(350, 350, 300, 0, 2*Math.PI, 0); context.stroke(); Javascript

Circle Example Code

  • This creates a circle . . .
  • with x, y at (350, 350) . . .
  • with radius of 300 px
  • The arc line will start at 0 radians . . .
  • and end at 2π radians . . .
  • and it will be drawn clockwise

var startAngle = 1.1 * Math.PI; var endAngle = 1.9 * Math.PI; context.beginPath(); context.arc(350, 350, 200, startAngle, endAngle, 0); context.stroke(); Javascript

Arc Example Code

  • This creates an arc . . .
  • with the center x, y at (350, 350) . . .
  • with radius of 200 px
  • The arc line will start at 1.1π radians . . .
  • and end at 1.9π radians . . .
  • and it will be drawn clockwise
slide-16
SLIDE 16

Text Function and Properties

fillText(text, x, y) Javascript

Adds text at that location

font = "font type" Javascript

Changes the font of the text

textAlign = "placement" Javascript

Changes the text alignment of the text

textBaseline = "placement" Javascript

Controls where the text is drawn relative to the starting point

slide-17
SLIDE 17

Lab 11

Create a picture on the canvas. Your picture must contain these elements:

  • Objects

○ 5 rectangles ○ 2 paths ○ 2 arcs or circles

  • Fill/Stroke Color

○ 1 gradient filled object ○ 3 object with different fill color ○ 3 with different stroke color

  • Misc

○ 1 image that is clipped or scaled ○ 1 line of text