- HTML5 Canvas ELEMENTHTML5 Canvas element
<canvas id= "MyCanvas" width= "height=" >
HTML5 canvas element with fallback content<canvas id= "MyCanvas" width= "$" height= "> your browser doesn ' t support canvas!</canvas>
2d Contextvar context = Canvas.getcontext (' 2d ');
Webgl Context (3d)var context = Canvas.getcontext (' WebGL ');
- Shapesdraw Rectangle
Context.rect (x, y, width, height); Context.fill (); Context.stroke ();
Fill Rectangle ShorthandContext.fillrect (x, y, width, height);
Stroke Rectangle ShorthandContext.strokerect (x, y, width, height);
Draw CircleContext.arc (x, y, radius, 0, Math.PI * 2); Context.fill (); Context.stroke ();
- Stylesfill
Context.fillstyle = ' red '; Context.fill ();
StrokeContext.strokestyle = ' red '; Context.stroke ();
Linear Gradientvar grd = context.createlineargradient (x1, y1, x2, y2); Grd.addcolorstop (0, ' red '); Grd.addcolorstop (1, ' Blue '); Context.fillstyle = Grd;context.fill ();
Radial Gradientvar grd = context.createradialgradient (x1, y1, Radius1, x2, Y2, radius2); Grd.addcolorstop (0, ' red '); Grd.addcolorstop (1, ' Blue '); Context.fillstyle = Grd;context.fill ();
Patternvar imageobj = new Image (), imageobj.onload = function () { var pattern = Context.createpattern (imageobj, ' repeat ');
context.fillstyle = pattern; Context.fill ();}; IMAGEOBJ.SRC = ' path/to/my/image.jpg ';
Line JoinContext.linejoin = ' Miter|round|bevel ';
Line CapContext.linecap = ' Butt|round|square ';
ShadowContext.shadowcolor = ' black '; context.shadowblur = 20;context.shadowoffsetx = 10;context.shadowoffsety = 10;
Alpha (Opacity)Context.globalalpha = 0.5; Between 0 and 1
- COLOR formatsstring
Context.fillstyle = ' red ';
Hex LongContext.fillstyle = ' #ff0000 ';
Hex ShortContext.fillstyle = ' #f00 ';
RgbContext.fillstyle = ' rgb (255,0,0) ';
RGBAContext.fillstyle = ' Rgba (255,0,0,1) ';
- Pathsbegin Path
Context.beginpath ();
LineContext.lineto (x, y);
ArcContext.arc (x, y, radius, startangle, Endangle, counterclockwise);
Quadratic curveContext.quadraticcurveto (CX, CY, X, y);
Bézier curveContext.beziercurveto (cx1, Cy1, CX2, Cy2, x, y);
Close PathContext.closepath ();
- imagesdraw image with default Size
var imageobj = new Image (); imageobj.onload = function () {context.drawimage (imageobj, x, y);}; IMAGEOBJ.SRC = ' path/to/my/image.jpg ';
Draw image and set Sizevar imageobj = new Image (); imageobj.onload = function () {Context.drawima GE (imageobj, x, y, width, height);}; IMAGEOBJ.SRC = ' path/to/my/image.jpg ';
Crop imagevar imageobj = new Image (); imageobj.onload = function () {Context.drawimage (imageobj , SX, SY, SW, SH, dx, dy, DW, DH);}; IMAGEOBJ.SRC = ' path/to/my/image.jpg ';
- textfill text
context.font = ' 40px Arial '; context.fillstyle = ' red '; Context.filltext (' Hello world! ', x, y);
Stroke textcontext.font = ' 40pt Arial '; context.strokestyle = ' red '; Context.stroketext (' Hello world! ', x, y);
Bold Textcontext.font = ' bold 40px Arial ';
Italic textcontext.font = ' Italic 40px Arial ';
Text aligncontext.textalign = ' start|end|left|center|right ';
Text baselinecontext.textbaseline = ' top|hanging|middle|alphabetic|ideographic|bottom ';
Get Text widthvar width = context.measuretext (' Hello World '). Width;
- Transformstranslate
Context.translate (x, y);
ScaleContext.scale (x, y);
RotateContext.rotate (radians);
Flip horizontallyContext.scale (-1, 1);
Flip verticallyContext.scale (1,-1);
Custom TransformContext.transform (A, B, C, D, E, f);
Set TransformContext.settransform (A, B, C, D, E, f);
ShearContext.transform (1, SY, SX, 1, 0, 0);
ResetContext.settransform (1, 0, 0, 1, 0, 0);
- State Stackpush state onto Stack
Context.save ();
Pop state off of StackContext.restore ();
- Clippingclip
Draw path Herecontext.clip ();
- Image Dataget Image Data
var imageData = Context.getimagedata (x, y, width, height); var data = Imagedata.data;
Loop Through Image Pixelsvar imageData = Context.getimagedata (x, y, width, height); var data = Imagedata.data;var len = Data.length;var i, red, gree N, Blue, alpha;for (i = 0; i < len; i + = 4) { red = data[i]; Green = Data[i + 1]; Blue = Data[i + 2]; Alpha = Data[i + 3];}
Loop Through Image Pixels with coordinatesvar imageData = Context.getimagedata (x, y, width, height); var data = Imagedata.data;var x, y, red, green, blue, alpha;for ( y = 0; Y < ImageHeight; y++) {for (x = 0; x < imagewidth; + +) { red = data[((imagewidth * y) + x) * 4]; Green = data[((imagewidth * y) + x) * 4 + 1]; Blue = data[((imagewidth * y) + x) * 4 + 2]; Alpha = data[((imagewidth * y) + x) * 4 + 3];} }
Set Image DataContext.putimagedata (ImageData, x, y);
- Data urlsget Data URL
var dataurl = Canvas.todataurl ();
Render Canvas with Data URLvar imageobj = new Image (), imageobj.onload = function () { context.drawimage (imageobj, 0, 0);}; IMAGEOBJ.SRC = Dataurl;
- Compositescomposite Operations
Context.globalcompositeoperation = ' source-atop|source-in|source-out|source-over|destination-atop|destination-in |destination-out|destination-over|lighter|xor|copy ';