Thursday, 22 December 2011

HTML5 canvas.getContext("2d") reference is here baby!!!!!


Context

The canvas element has no drawing abilities of its own.
A canvas is a rectangular area, where you can control every pixel using JavaScript.
HTML5 has a built-in object for the canvas element, the getContext("2d") object.
The getContext("2d") object has methods to draw lines, boxes, circles, and more.

Colors and Styles

AttributeValueDescription
fillStylecolor/styleThe fill-color of the drawing, black is default
strokeStylecolor/styleThe stroke-color of the drawing, black is default
lineWidthnumberThe width of the drawing stroke, 1 is default
lineCapbutt
round
square
The style of the ending of the line, butt is default
lineJoinbevel
round
miter
The style of the corners of the line, miter is default
miterLimitnumberThe limit size of the corners in a line, default is 10
shadowColorcolorThe color of the shadow, black is default
shadowOffsetXnumberThe horizontal distance of the shadow, 0 is default
shadowOffsetYnumberThe vertical distance of the shadow, 0 is default
shadowBlurnumberThe size of the blurring effect, 0 is default
MethodDescription
createPattern(obj,pattern)Creates a pattern from an image to be used by the fillStyle or strokeStyle attributes
createLinearGradient(x0,y0,z1,y1)Creates a gradient object. Use this object as a value to the strokeStyle or fillStyle attributes
createRadialGradient(x0,y0,r0,x1,y1,r1)Creates a gradient object as a circle. Use this object as a value to the strokeStyle or fillStyle attributes
addColorStop(position,color)This is a method of the gradient objects above. Specifies the gradient's position and color
drawCustomFocusRing(element) 
drawSystemFocusRing(element) 

Path, Curve, Circle, and Rectangle

MethodDescription
fillRect(x,y,w,h)Draws a filled rectangle using the color/style of the fillStyle attribute
strokeRect(x,y,w,h)Draws the lines of a rectangle using the color/style of the strokeStyle attribute
clearRect(x,y,w,h)Clears a rectangle area. All pixels inside this area will be erased
beginPath()Starts a new path, or clears the current path
moveTo(x,y)Moves the path to the specified point, without creating a line
closePath()Creates a line (path) from the last point in the path, to the first point. Completes the path
lineTo(x,y)Creates a line from the last point in the path to the given point
rect(x,y,w,h)Creates a rectangle
fill()Fills the current path with the selected color, black is default
stroke()Creates a stroke/path described with the specified path methods
clip()Creates an area in the canvas, and this area is the only visible area in the canvas
quadraticCurveTo(cpx,cpy,x,y)Creates a quadratic Bwzier curve from the current point in the path to the specified path
bezierCurveTo(cpx,cpy,cpx,cpy,x,y)Creates a cubic Bezier curve from the current point in the path to the specified path
arc(x,y,r,sAngle,eAngle,aClockwise)Creates a circle, or parts of a circle
arcTo(x1,y1,x2,y2,radius)Creates an arc between two points
isPointInPath(x,y)Returns a Boolean value, true if the specified point is in the path, otherwise false

Transformations

MethodDescription
scale(x,y)Scales the drawings based on the x and y parameters
rotate(angle)Rotates the drawings based on the given angle
translate(x,y)Moves the drawings horizontally and vertically
transform(a,b,c,d,e,f)Changes the shape of the drawings based on a matrix
setTransform(a,b,c,d,e,f)Clears the current transformation, then makes the changes of the drawings based on a matrix

Text

AttributeValueDescription
fontfontpropertiesSpecifies font properties used for writing on the canvas
textAlignstart
end
left
right
center
The alignment of the text, "start" is default
textBaselinealphabetic
bottom
hanging
ideographic
middle
top
The vertical alignment of the text, "alphabetic" is default
MethodDescription
fillText(text,x,y,maxWidth)Draws text on the canvas, and fills it with the color set by the fillStyle attribute
strokeText(text,x,y,maxWidth)Draws text on the canvas, without filling, using the color set by the strokeStyle attribute.
measureText(text).widthMeasures the given text string, returns the result in pixels

Images and Pixel Manipulation

AttributeValueDescription
widthnumber
heightnumber
dataarray
MethodDescription
drawImage
createImageData(sw,sh)
createImageData(imagedata)
getImageData(sq,sy,sw,sh)
putImageData
(imagedata,dx,dy,x,y,width,height)

Compositing

AttributeValueDescription
globalAlphanumber
globalCompositeOperation
source-atop
source-in
source-out
source-over
destination-atop
destination-in
destination-out
destination-over
lighter
copy
xor
vendorName-operationName
Case sensitive. source-over is the default value.

No comments:

Post a Comment