24
CANVAS Fundamentals By RaedRASHEED 2015

CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Embed Size (px)

Citation preview

Page 1: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

CANVAS F u n d a m e n t a l s

ByRaedRASHEED

2015

Page 2: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

What is Canvas?

Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is typically stretched across a wooden frame.

On the HTML canvas, you can draw all kinds of graphics, from simple lines, to complex graphic objects.

Page 3: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

The HTML <canvas> Element

The HTML <canvas> element (introduced in HTML5) is a container for canvas graphics.

An HTML canvas is a rectangular area on an HTML page.

Canvas has several methods for drawing paths, boxes, circles, text, and graphic images.

Page 4: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Basic Canvas Example

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">

Your browser does not support the HTML5 canvas tag.</canvas>

</body>

Page 5: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Basic Canvas Example

Page 6: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Drawing with JavaScript

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");var ctx = c.getContext("2d");ctx.fillStyle = "#FF0000";ctx.fillRect(0,0,150,75);

</script>

</body>

Page 7: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Drawing with JavaScript

Page 8: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Line

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

ctx.moveTo(0,0);

ctx.lineTo(200,100);

ctx.stroke();

</script>

</body>

Page 9: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Line

Page 10: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Circle

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

ctx.beginPath();

ctx.arc(95,50,40,0,2*Math.PI);

ctx.stroke();

</script>

</body>

Page 11: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Circle

Page 12: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Text

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");var ctx = c.getContext("2d");ctx.font = "30px Arial";ctx.fillText("Hello World",10,50);

</script>

</body>

Page 13: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw a Text

Page 14: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Stroke Text

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");var ctx = c.getContext("2d");ctx.font = "30px Arial";ctx.strokeText("Hello World",10,50);

</script>

</body>

Page 15: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Stroke Text

Page 16: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Gradient

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

Page 17: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Gradient

// Create gradient

var grd = ctx.createLinearGradient(0,0,200,0);

grd.addColorStop(0,"red");

grd.addColorStop(1,"white");

// Fill with gradient

ctx.fillStyle = grd;

ctx.fillRect(10,10,150,80);

</script>

</body>

Page 18: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Gradient

Page 19: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Circular Gradient

<body><canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">

Your browser does not support the HTML5 canvas tag.</canvas><script>

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

Page 20: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Circular Gradient

// Create gradient

var grd = ctx.createRadialGradient(75,50,5,90,60,100);

grd.addColorStop(0,"red");

grd.addColorStop(1,"white");

// Fill with gradient

ctx.arc(50, 50, 50, 0, 2*Math.PI);

ctx.fillStyle = grd;

ctx.fill();

</script>

</body>

Page 21: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Circular Gradient

Page 22: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Image

<body>

<p>Image to use:</p>

<img id="scream" src="canvas.jpg" alt="The Scream" width="220" height="277">

<p>Canvas to fill:</p>

<canvas id="myCanvas" width="250" height="300" style="border:1px solid #d3d3d3;">

Your browser does not support the HTML5 canvas tag.

</canvas>

<p><button onclick="myCanvas()">Try it</button></p>

Page 23: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Image

<script>

function myCanvas() {

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");

var img = document.getElementById("scream");

ctx.drawImage(img,10,10);

}

</script>

</body>

Page 24: CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is

Draw Image