Canvas
基本用法
|
|
toDataURL12// parmas: 'image/png' | 'image/jpeg'(只支持较新版本)let imgURL = context.toDataURL('image/png')
2D上下文
填充和描边
1234// parmas: 字符串,渐变对象,模式对象// '#ffffff' 'red' 'rgba(0,0,255,.5)' 'rgb(0,0,225)' context.createLinearGridient() context.createPattern()context.fillStyle = 'rgba(0,0,255,.5)'context.strokeStyle = 'red'绘制矩形
123456789// parmas: x,y,width,height// 有填充context.fillStyle = '#0000ff'context.fillRect(10, 10, 50, 50)// 无填充context.strokeStyle = 'pink'context.strokeRect(10, 80, 50, 50)// 清除矩形区域context.clearRect(10, 10, 20, 20)绘制路径
123456789101112131415161718192021222324// 开始路径context.beginPath()// 线宽// parmas: integercontext.lineWidth = 5// 线头// parmas: 'butt' (平头) | 'round' (圆头) | 'square' (方头)context.lineCap = 'round'// 相交点// parmas: 'round' (圆交) | 'bevel' (斜交) | 'miter' (斜接)context.lineJoin = 'bevel'// 绘制圆// parmas: x,y,r,begin(rad),end(rad),boolean(false -> 顺时针)context.arc(100, 100, 99, 0, 2 * Math.PI, false)// 绘制弧arcTobezierCurveToquadraticCurveToreact绘制文本
12345678// parmas: 文本样式 、大小、字体context.font = '14px bold Arial'// parmas: start | end | center | left | right (建议使用start | end)context.textAlign = 'center'// parmas: top | hanging | middle | alphabetic | ideotraphic | bottomcontext.textBaseLine = 'buttom'// parmas: string,x,ycontext.fillText(12, 300, 230)