js 创建渐变颜色带,并且通过一个数组随机取颜色
//用构造函数定义对象的非函数属性 function ColorBelt() { this.canvas = document.createElement('canvas'); this.context = this.canvas.getContext('2d');//指向2d渲染环境的引用 var colorArr = Array.prototype.slice.call(arguments);//传的参数转为数组 if (0 == colorArr.length) { this.context.fillStyle = this.defGradient(); } else if (1 == colorArr.length) { this.context.fillStyle = [0]; } else { this.context.fillStyle = this.createLinearGradient(colorArr); } this.context.fillRect(0, 0, 100,3); } ColorBelt.prototype.createLinearGradient = function (colorArr) { var maxIndex = colorArr.length - 1; var xSpan = 1 / maxIndex; var grd =