博客网
HTML5画布
画布元素
线
线
线条宽度
线条颜色
线帽
曲线
弧
二次曲线
贝塞尔曲线
路径
路径
线条连接
圆角
形状
自定义形状
长方形
圆圈
半圆
椭圆形
填充样式
颜色填充
线性渐变
径向渐变
图案
图像
绘制图像
图像大小
图像裁剪
图像加载器
文本
字体、大小和样式
文本颜色
文本笔划
文本对齐
文本基线
文本度量
文本换行符
转换
转换
比例
旋转
自定义变换
剪切
镜子
重置变换
状态堆栈
复合材料
阴影
全局Alpha
剪裁区域
操作
图像数据和URL
图像数据
反色
灰度
获取数据URL
加载数据URL
保存绘图
动画
清除画布
动画帧
线性运动
加快
振荡
启动和停止
鼠标检测
鼠标坐标
操作
代码
运行
<!DOCTYPE HTML> <html> <head> <title>操作</title> <style> body { margin: 0px; padding: 0px; } </style> </head> <body> <canvas id="myCanvas" width="578" height="430"></canvas> <canvas id="tempCanvas" width="578" height="430" style="display:none;"></canvas> <script> var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); var tempCanvas = document.getElementById('tempCanvas'); var tempContext = tempCanvas.getContext('2d'); var squareWidth = 55; var circleRadius = 35; var shapeOffset = 50; var operationOffset = 150; var arr = []; arr.push('source-atop'); arr.push('source-in'); arr.push('source-out'); arr.push('source-over'); arr.push('destination-atop'); arr.push('destination-in'); arr.push('destination-out'); arr.push('destination-over'); arr.push('lighter'); arr.push('darker'); arr.push('xor'); arr.push('copy'); // translate context to add 10px padding context.translate(10, 10); // draw each of the operations for(var n = 0; n < arr.length; n ) { var thisOperation = arr[n]; tempContext.save(); // clear temp context tempContext.clearRect(0, 0, canvas.width, canvas.height); // draw rectangle (destination) tempContext.beginPath(); tempContext.rect(0, 0, squareWidth, squareWidth); tempContext.fillStyle = 'blue'; tempContext.fill(); // set global composite tempContext.globalCompositeOperation = thisOperation; // draw circle (source) tempContext.beginPath(); tempContext.arc(shapeOffset, shapeOffset, circleRadius, 0, 2 * Math.PI, false); tempContext.fillStyle = 'red'; tempContext.fill(); tempContext.restore(); // draw text tempContext.font = '10pt Verdana'; tempContext.fillStyle = 'black'; tempContext.fillText(thisOperation, 0, squareWidth 45); // translate visible context so operation is drawn in the right place if(n > 0) { if(n % 4 === 0) { context.translate(operationOffset * -3, operationOffset); } else { context.translate(operationOffset, 0); } } // copy drawing from tempCanvas onto visible canvas context.drawImage(tempCanvas, 0, 0); } </script> </body> </html>