以下是drawImage API解释:
- //在(x,y)出绘制图像image context.drawImage(image, x, y)
- //在(x,y)出绘制width*height的图像image context.drawImage(image, x, y, width, height)
- //在image的(sx,sy)处截取sWidth*sHeight的图像,在(dx,dy)处绘制dWidth*dHeight的图像 context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
3. 高级功能
3.1 使Canvas填满浏览器窗口
最简单的方式是将canvas元素的宽度和高度精确设置为浏览器窗口的宽度和高度,用CSS消去白色空隙。
CSS代码:
- * { margin: 0;
- padding: 0; }
- html, body {
- height: 100%; width: 100%;
- }
- canvas { display: block;
- }
Javascript代码:
- function resizeCanvas() { //canvas由jQuery获取
- canvas.attr("width", $(window).get(0).innerWidth); canvas.attr("height", $(window).get(0).innerHeight);
- context.fillRect(0, 0, canvas.width(), canvas.height()); }
- $(window).resize(resizeCanvas); resizeCanvas();
3.2 绘图状态
在canvas中,绘图状图指的是描述某一时刻2D渲染上下文外观的整套属性,包括:变换矩阵、裁剪区域、globalAlpha、globalCompositeOperation、strokeStyle、fillStyle、lineWidth、lineCap、lineJoin、miterLimit、shadowOffsetX、shadowOffsetY、shadowBlur、shadowColor、font、textAlign和textBaseline。
当需要改变画布全局状态时,一般先将当前状态保存起来——调用save方法将状态推入绘图状态栈),做完操作之后,再调用restore方法回复绘图状态。









