gradient.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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:
  46. coordinate = [0, -height / 2, 0, height / 2];
  47. break;
  48. case 90:
  49. coordinate = [width / 2, 0, -width / 2, 0];
  50. break;
  51. case -90:
  52. coordinate = [-width / 2, 0, width / 2, 0];
  53. break;
  54. case 180:
  55. coordinate = [0, height / 2, 0, -height / 2];
  56. break;
  57. case -180:
  58. coordinate = [0, -height / 2, 0, height / 2];
  59. break;
  60. default:
  61. let x1 = 0;
  62. let y1 = 0;
  63. let x2 = 0;
  64. let y2 = 0;
  65. if (direction[1] > 0 && direction[1] < 90) {
  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 if (direction[1] > -180 && direction[1] < -90) {
  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. } else if (direction[1] > 90 && direction[1] < 180) {
  76. 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;
  77. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  78. x2 = -x1;
  79. y1 = -y2;
  80. } else {
  81. 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;
  82. y2 = Math.tan((90 - direction[1]) * Math.PI * 2 / 360) * x1;
  83. x2 = -x1;
  84. y1 = -y2;
  85. }
  86. coordinate = [x1, y1, x2, y2];
  87. break;
  88. }
  89. return coordinate;
  90. }
  91. function linearEffect(width, height, bg, ctx) {
  92. const param = analizeLinear(bg, width, height);
  93. const grd = ctx.createLinearGradient(param[0], param[1], param[2], param[3]);
  94. const content = bg.match(/linear-gradient\((.+)\)/)[1];
  95. const colorPer = analizeGrad(content.substring(content.indexOf(',') + 1));
  96. for (let i = 0; i < colorPer.colors.length; i++) {
  97. grd.addColorStop(colorPer.percents[i], colorPer.colors[i]);
  98. }
  99. ctx.fillStyle = grd
  100. //ctx.fillRect(-(width / 2), -(height / 2), width, height);
  101. }
  102. module.exports = {api}
  103. })();