- HTML5 CANVAS ELEMENTHtml5 canvas element
<canvas id="myCanvas" width="500" height="300">
Html5 canvas element with fallback content<canvas id="myCanvas" width="500" height="300"> 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);
Begiscurvecontext.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.drawImage(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 processing et 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, green, 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; x++) { 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';