gradient.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* eslint-disable */
  2. // 当ctx传入当前文件,const grd = ctx.createCircularGradient() 和
  3. // const grd = this.ctx.createLinearGradient() 无效,因此只能分开处理
  4. // 先分析,在外部创建grd,再传入使用就可以
  5. !(function () {
  6. var api = {
  7. isGradient: function(bg) {
  8. if (bg && (bg.startsWith('linear') || bg.startsWith('radial'))) {
  9. return true;
  10. }
  11. return false;
  12. },
  13. doGradient: function(bg, width, height, ctx) {
  14. if (bg.startsWith('linear')) {
  15. linearEffect(width, height, bg, ctx);
  16. } else if (bg.startsWith('radial')) {
  17. radialEffect(width, height, bg, ctx);
  18. }
  19. },
  20. }
  21. function analizeGrad(string) {
  22. const colorPercents = string.substring(0, string.length - 1).split("%,");
  23. const colors = [];
  24. const percents = [];
  25. for (let colorPercent of colorPercents) {
  26. colors.push(colorPercent.substring(0, colorPercent.lastIndexOf(" ")).trim());
  27. percents.push(colorPercent.substring(colorPercent.lastIndexOf(" "), colorPercent.length) / 100);
  28. }
  29. return {colors: colors, percents: percents};
  30. }
  31. function radialEffect(width, height, bg, ctx) {
  32. const colorPer = analizeGrad(bg.match(/radial-gradient\((.+)\)/)[1]);
  33. const grd = ctx.createRadialGradient(0, 0, 0, 0, 0, width < height ? height / 2 : width / 2);
  34. for (let i = 0; i < colorPer.colors.length; i++) {
  35. grd.addColorStop(colorPer.percents[i], colorPer.colors[i]);
  36. }
  37. ctx.fillStyle = grd;
  38. //ctx.fillRect(-(width / 2), -(height / 2), width, height);
  39. }
  40. function analizeLinear(bg, width, height) {
  41. const direction = bg.match(/([-]?\d{1,3})deg/);
  42. const dir = direction && direction[1] ? parseFloat(direction[1]) : 0;
  43. let coordinate;
  44. switch (dir) {
  45. case 0: coordinate = [0, -height / 2, 0, height / 2]; break;
  46. case 90: coordinate = [width / 2, 0, -width / 2, 0]; break;
  47. case -90: coordinate = [-width / 2, 0, width / 2, 0]; break;
  48. case 180: coordinate = [0, height / 2, 0, -height / 2]; break;
  49. case -180: coordinate = [0, -height / 2, 0, height / 2]; break;
  50. default:
  51. let x1 = 0;
  52. let y1 = 0;
  53. let x2 = 0;
  54. let y2 = 0;
  55. if (direction[1] > 0 && direction[1] < 90) {
  56. x1 = (width / 2) - ((width / 2) * Math.tan((90 - direction[1]) * Math.PI * 2 / 360) - height / 2) * Math.sin(2 * (90 - direction[1]) * Math.PI * 2 / 360) / 2;
  57. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  58. x2 = -x1;
  59. y1 = -y2;
  60. } else if (direction[1] > -180 && direction[1] < -90) {
  61. x1 = -(width / 2) + ((width / 2) * Math.tan((90 - direction[1]) * Math.PI * 2 / 360) - height / 2) * Math.sin(2 * (90 - direction[1]) * Math.PI * 2 / 360) / 2;
  62. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  63. x2 = -x1;
  64. y1 = -y2;
  65. } else if (direction[1] > 90 && direction[1] < 180) {
  66. x1 = (width / 2) + (-(width / 2) * Math.tan((90 - direction[1]) * Math.PI * 2 / 360) - height / 2) * Math.sin(2 * (90 - direction[1]) * Math.PI * 2 / 360) / 2;
  67. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  68. x2 = -x1;
  69. y1 = -y2;
  70. } else {
  71. x1 = -(width / 2) - (-(width / 2) * Math.tan((90 - direction[1]) * Math.PI * 2 / 360) - height / 2) * Math.sin(2 * (90 - direction[1]) * Math.PI * 2 / 360) / 2;
  72. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  73. x2 = -x1;
  74. y1 = -y2;
  75. }
  76. coordinate = [x1, y1, x2, y2];
  77. break;
  78. }
  79. return coordinate;
  80. }
  81. function linearEffect(width, height, bg, ctx) {
  82. const param = analizeLinear(bg, width, height);
  83. const grd = ctx.createLinearGradient(param[0], param[1], param[2], param[3]);
  84. const content = bg.match(/linear-gradient\((.+)\)/)[1];
  85. const colorPer = analizeGrad(content.substring(content.indexOf(',') + 1));
  86. for (let i = 0; i < colorPer.colors.length; i++) {
  87. grd.addColorStop(colorPer.percents[i], colorPer.colors[i]);
  88. }
  89. ctx.fillStyle = grd
  90. //ctx.fillRect(-(width / 2), -(height / 2), width, height);
  91. }
  92. module.exports = { api }
  93. })();