Course Link
- Use cases:
- Games
- Photomanpulation
- Audio and Video player
- Charts
- etc
- Important to know
- [1] Canvas like img but without src + alt and with closing tag
- [2] Canvas Can be styled wiht css
- [3] Default value
width 300 \* height 150
- [4] Default value background-color is
transparent
It's prefered to put width and height in canvas directly
<canvas width="300" height="500"></canvas>
u may face some stretch in content if width or height in css dosen't respect width and height ratio
if not width or height set to canvas in html or css Default value is 300 * 150
It's prefered to use id to get canvas as id is unique
Canvas have context this is where we draw our graphics we get it by .getContext("id")
id
to identify which type of graphics u want there are many like 2d
webGl
and etc
usualy they name its var as ctx
and name canvas var as c
Canvas Context method
-
.fillStyle =
- fill graphics with color | Gradient | pattern
- Default value id
black
-
.fillRect(x , y , width , height)
- draw rect
Canvas Context method
-
.createLinearGradient(x0 , y0 , x1 , y1)
x0
=> Gradient Start from lefty0
=> Gradient Start from Topx1
=> Gradient End to lefty1
=> Gradient End to Top.addColorStop(offset , color)
-
.createPattern(image , "repeatation")
image
=> image | video | another canvasrepeatation
=>no-repeat
don't repeat pattern |repeat
repeat pattern in all direction |repeat-x
repeat pattern in x-axis |repeat-y
repeat pattern in y-axis
Handle all these in a variable var
the assign it to .fillStyle = var
Canvas Context method
-
.strokeStyle =
- fill graphics with color | Gradient | pattern
- Default value id
black
-
.fillstroke(x , y , width , height)
- draw rect border
-
.lineWidth =
- control line width
-
.setFont = "22px Tahoma"
- control font style
-
.fillText("some text")
- draw text
-
.strokeText("some text",x,y,maxwidth[opt])
- draw text border
Paths And Lines
Canvas Context method
-
.beginPath()
- to begin path drawing
-
.moveTo(x,y)
- to move brush to x and y u want
- to begin path drawing
-
.lineTo(x,y)
- to draw points of line
-
.stroke()
- to show line
Paths Advanced Training
Canvas Context method
-
.closePath()
- to draw line from current postion to starting position
-
.fill()
- to fill closed path with certin color
Drawing Circle By Arc
Canvas Context method
.arc(x , y , raduis , startAngle , endAngle , antiClockwise [opt] = false
x & y
postion of centeriodraduis
raduis of circlestartAngle
if set to0
it will begin from 3 o'clockendAngle
radian value
Remember
360 Degree = Full Circle
1 Radian = Half Circle => 1 pI => 3.14
2 Radian = Full Circle => 2 pI => 6.28
Shadow Methods
Canvas Context method
-
.shadowColor = ""
- color of shadow
- Accept all type of css like
hexColor
rgba()
etc
-
.shadowBlur = 10
- Blurness of shadow
- number value
-
.shadowOffsetX = "-20"
- move shadow in x direction
- string value
-
.shadowOffsetY = "20"
- move shadow in y direction
- string value
Transformation And Save, Restore Context
Canvas Context method
-
.scale(x,y)
- scale element in all
width | height | x postion | y postion
1
mean 100%
- scale element in all
-
.save()
- save canvas context clean state
- UseCase when need to
scale()
u put this before doing scale and u can useresotre()
to back to normal
-
.restore()
- restore canvas context clean state
-
.translate(x,y)
- translate object by x and y and the origin is object transformation
(object.x,object.y)
not world origin(0,0)
- translate object by x and y and the origin is object transformation
Text Align And Direction
Canvas Context method
-
.textAlign = ""
start
=>left
| the main diffrence appear when change direction of canvas context theleft
will be the same butstart
will be changedend
=>right
| the main diffrence appear when change direction of canvas context theright
will be the same butend
will be changedcenter
-
.direction = ""
rtl
ltr
-
.globalAlpha = 1
1
mean 100% |0.5
mean 50%- number value
It's recomended to draw your shape in single path
ctx.beginPath();
// Draw Here
ctx.fill();
or (Not recomended)
ctx.beginPath();
// Draw Here
ctx.moveTo(x,y) // x and y of next shape
End And Where To Continue