HTML5资讯

当前位置: HTML5技术网 > HTML5资讯 > HTML5画布(CANVAS)速查简表

HTML5画布(CANVAS)速查简表

HTML5画布(CANVAS)元素

复制代码
浏览器不支持画布(canvas)时的备案
  1.   your browser doesn't support canvas!
复制代码
2d context
  1. var context = canvas.getContext('2d');
复制代码
Webgl context (3d)
  1. var context = canvas.getContext('webgl');
复制代码
图形
绘制方形
  1. context.rect(x, y, width, height);context.fill();context.stroke();
复制代码
填充区域
  1. context.fillRect(x, y, width, height);
复制代码
绘制方形的边框
  1. context.strokeRect(x, y, width, height);
复制代码
绘制圆形
  1. context.arc(x, y, radius, 0, Math.PI * 2);context.fill();context.stroke();
复制代码
风格
填充
  1. context.fillStyle = 'red';context.fill();
复制代码
勾勒
  1. context.strokeStyle = 'red';context.stroke();
复制代码
线性渐变
  1. var grd = context.createLinearGradient(x1, y1, x2, y2);grd.addColorStop(0, 'red');   grd.addColorStop(1, 'blue');context.fillStyle = grd;context.fill();
复制代码
径向渐变
  1. var grd = context.createRadialGradient(x1, y1, radius1, x2, y2, radius2);grd.addColorStop(0, 'red');grd.addColorStop(1, 'blue');context.fillStyle = grd;context.fill();
复制代码
图案
  1. var imageObj = new Image();imageObj.onload = function() {  var pattern = context.createPattern(imageObj, 'repeat');  context.fillStyle = pattern;  context.fill();};imageObj.src = 'path/to/my/image.jpg';
复制代码
交点
  1. context.lineJoin = 'miter|round|bevel';
复制代码
线头
  1. context.lineCap = 'butt|round|square';
复制代码
阴影
  1. context.shadowColor = 'black';context.shadowBlur = 20;context.shadowOffsetX = 10;context.shadowOffsetY = 10;
复制代码
Alpha (透明)
  1. context.globalAlpha = 0.5; // between 0 and 1
复制代码
颜色格式
字符串
  1. context.fillStyle = 'red';
复制代码
16进制
  1. context.fillStyle = '#ff0000';
复制代码
16进制简写
  1. context.fillStyle = '#f00';
复制代码
RGB
  1. context.fillStyle = 'rgb(255,0,0)';
复制代码
RGBA
  1. context.fillStyle = 'rgba(255,0,0,1)';
复制代码
路径
开始路径
  1. context.beginPath();
复制代码
画线
  1. context.lineTo(x, y);
复制代码
弧形
  1. context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
复制代码
二次曲线
  1. context.quadraticCurveTo(cx, cy, x, y);
复制代码
二次曲线
  1. context.bezierCurveTo(cx1, cy1, cx2, cy2, x, y);
复制代码
关闭路径
  1. context.closePath();
复制代码
图片
画图
  1. var imageObj = new Image();imageObj.onload = function() {  context.drawImage(imageObj, x, y);};imageObj.src = 'path/to/my/image.jpg';
复制代码
指定尺寸画图
  1. var imageObj = new Image();imageObj.onload = function() {  context.drawImage(imageObj, x, y, width, height);};imageObj.src = 'path/to/my/image.jpg';
复制代码
裁剪图片
  1. var imageObj = new Image();imageObj.onload = function() {  context.drawImage(imageObj, sx, sy, sw, sh, dx, dy, dw, dh);};imageObj.src = 'path/to/my/image.jpg';
复制代码
文本
写文字
  1. context.font = '40px Arial';context.fillStyle = 'red';context.fillText('Hello World!', x, y);
复制代码
写镂空文字
  1. context.font = '40pt Arial';context.strokeStyle = 'red';context.strokeText('Hello World!', x, y);
复制代码
粗体
  1. context.font = 'bold 40px Arial';
复制代码
斜体
  1. context.font = 'italic 40px Arial';
复制代码
对齐方式
  1. context.textAlign = 'start|end|left|center|right';
复制代码
文字基线
  1. context.textBaseline = 'top|hanging|middle|alphabetic|ideographic|bottom';
复制代码
获取文本宽度
  1. var width = context.measureText('Hello world').width;
复制代码
动画
移动
  1. context.translate(x, y);
复制代码
扩大缩小
  1. context.scale(x, y);
复制代码
旋转
  1. context.rotate(radians);
复制代码
水平翻转
  1. context.scale(-1, 1);
复制代码
上下翻转
  1. context.scale(1, -1);
复制代码
自定义变换
  1. context.transform(a, b, c, d ,e, f);
复制代码
设置变换
  1. context.setTransform(a, b, c, d ,e, f);
复制代码
切割
  1. context.transform(1, sy, sx, 1, 0, 0);
复制代码
重置
  1. context.setTransform(1, 0, 0, 1, 0, 0);
复制代码
状态存储
存储
  1. context.save();
复制代码
恢复
  1. context.restore();
复制代码
裁剪
裁剪
  1. // draw path herecontext.clip();
复制代码
图像数据
获取图像数据
  1. var imageData = context.getImageData(x, y, width, height);var data = imageData.data;
复制代码
遍历像素点
  1. var imageData = context.getImageData(x, y, width, height);var data = imageData.data;var len = data.length;var i, red, green, blue, alpha;
  2. for(i = 0; i < len; i += 4) {  red = data[i];  green = data[i + 1];  blue = data[i + 2];  alpha = data[i + 3];}
复制代码
沿坐标遍历像素点
  1. var imageData = context.getImageData(x, y, width, height);var data = imageData.data;var x, y, red, green, blue, alpha;
  2. 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];  }}
复制代码
设置图像数据
  1. context.putImageData(imageData, x, y);
复制代码
DATA URLS
获取Data URL
  1. var dataURL = canvas.toDataURL();
复制代码
使用Data URL生成图像
  1. var imageObj = new Image();imageObj.onload = function() {  context.drawImage(imageObj, 0, 0);};
  2. imageObj.src = dataURL;
复制代码
合成
合成操作
  1. context.globalCompositeOperation = 'source-atop|source-in|source-out|source-over|destination-atop|destination-in|destination-out|destination-over|lighter|xor|copy';
复制代码



【HTML5画布(CANVAS)速查简表】相关文章

1. HTML5画布(CANVAS)速查简表

2. 使用HTML5画布(canvas)生成阴影效果

3. 三分钟HTML5画布(Canvas)动画教程

4. 使用HTML5画布实现的超棒javascript动画仪表板:gauge.js

5. HTML5画布下js的文字云/标签云效果 - D3 Cloud

6. HTML5 Canvas cheat sheet:元素详细速查手册

7. HTML5 Canvas API速查手册

8. HTML5 标签、事件句柄属性以及浏览器兼容情况速查手册

9. HTML 5标签、属性、事件及兼容性速查表

10. 利用分层优化 HTML5 画布渲染

本文来源:https://www.51html5.com/a3662.html

点击展开全部

﹝HTML5画布(CANVAS)速查简表﹞相关内容

其它栏目

也许您还喜欢