HTML5 Convas APIs方法详解方法详解
☆ canvas.getContext(‘2d’)
不可在convas中直接绘图,必须用该方法获得其二维空间绘图上
下文。
☆ context.beginPath()
表示开始新的路径绘制。
☆ context.isPointInPath(x, y)
判断某个点是否在路径上。在坐标系被转换后该方法不适用。
☆ context.moveTo(x,y)
相当于将画笔从画板提起,笔尖离开画板,然后再将笔尖定位在
(x,y)坐标处,在这个新的位置开始新的绘制。
☆ context.lineTo(x, y)
相当于画笔笔尖不离开画板,画笔笔尖从当前坐标位置移动至
(x,y)坐标处,绘制一条线段。
☆ context.stroke()
在convas上绘图后,一些绘制操作必须调用该方法才能让绘制内
容显示。
☆ context.save()
该方法保存convas的当前状态,无论以后对convas坐任何改变,
只要在做这些改变前保存convas状态,以后就可以调用
context.restore()方法恢复到保存的这个状态。通常在一段新绘制
或修改操作前应该保存convas的原始状态(没有进行任何绘制或更改
),每次在一段新绘制或修改操作结束后在将其恢复到原始状态。这
样有利于以后的绘制操作。
实际上,canvas的2d绘图环境context的许多属性和一些方法与状
态有关,每个属性的值被改变(或者使用某些方法改变绘图状态),
绘图状态就改变。若在每次改变后都保存,则一个属性的多个状态会
以栈(stack)的形式保存,可以依照栈的顺序多次调用restore()方
法来回到相应保存的状态。
☆ context.translate(x, y)
该方法将当前坐标原点移动到(x, y)处。
☆ context.restore()
恢复convas状态为上一次保存的状态。
☆ context.closePath()
This command is very similar in behavior to the lineTo
function, with the difference being that the destination is
automatically assumed to be the
origination of the path. However, the closePath also informs
the canvas that the current shape has closed or formed a
completely contained area. This will be useful for future
fills and strokes.
At this point, you are free to continue with more
segments in your path to create additional subpaths. Or you
can beginPath at any time to start over and clear the path
list entirely.
☆ context.fill();
在设置填充样式后填充闭合路径。调用该方法后不必再调用
context.stroke()方法。