为了研究pixi库,就顺带从头到位学习下canvas吧
var webgl = (function() { try { var canvas = document.createElement('canvas'); return !!window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl')); } catch (e) { return false; } })();
getContext方法指定参数2d,表示该canvas对象用于生成2D图案(即平面图案)。如果参数是3d,就表示用于生成3D图像(即立体图案),这部分实际上单独叫做WebGL API
////////// //绘制路径 // ////////// context.beginPath() context.moveTo(20, 20) context.lineTo(30, 30) context.moveTo(100, 150) context.lineTo(150, 200) context.lineWidth = 2 context.strokeStyle = "red" context.stroke()
////////// //绘制路径 // ////////// context.beginPath() context.moveTo(20, 20) context.lineTo(30, 30) context.moveTo(100, 150) context.lineTo(150, 200) context.lineWidth = 2 context.strokeStyle = "red" context.stroke()
////////// //绘制路径 // ////////// context.beginPath() context.moveTo(20, 20) context.lineTo(30, 30) context.moveTo(100, 150) context.lineTo(150, 200) context.lineWidth = 2 context.strokeStyle = "red" context.stroke()
arc方法用来绘制扇形
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
arc方法
////////// //绘制路径 // ////////// context.beginPath() context.moveTo(20, 20) context.lineTo(30, 30) context.moveTo(100, 150) context.lineTo(150, 200) context.lineWidth = 2 context.strokeStyle = "red" context.stroke()
createLinearGradient方法用来设置渐变色。
createLinearGradient方法的参数是(x1, y1, x2, y2),其中x1和y1是起点坐标,x2和y2是终点坐标。通过不同的坐标值,可以生成从上至下、从左到右的渐变等等。
///////// //渐变色 // ///////// var myGradient = context.createLinearGradient(0, 0, 0, 60); myGradient.addColorStop(0, "#BABABA"); myGradient.addColorStop(1, "#636363"); context.fillStyle = myGradient; context.fillRect(10,250,200,100);
///////// //渐变色 // ///////// var myGradient = context.createLinearGradient(0, 0, 0, 60); myGradient.addColorStop(0, "#BABABA"); myGradient.addColorStop(1, "#636363"); context.fillStyle = myGradient; context.fillRect(10,250,200,100);