GradRect.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { SGraphItem } from "@persagy-web/graph/lib";
  2. import { SColor, SLinearGradient, SPainter, SRadialGradient, SRect } from "@persagy-web/draw/lib";
  3. /**
  4. * 渐变
  5. *
  6. * @author 郝洁 <haojie@persagy.com>
  7. */
  8. export class GradRect extends SGraphItem {
  9. minX = 0;
  10. minY = 0;
  11. maxY = 1000;
  12. maxX = 1000;
  13. gradient: SRadialGradient | SLinearGradient | null = null;
  14. constructor(parent: SGraphItem | null, grad: SRadialGradient | SLinearGradient) {
  15. super(parent);
  16. this.gradient = grad;
  17. this.gradient.addColorStop(0, new SColor('#ff483b'));
  18. this.gradient.addColorStop(0.5, new SColor('#04ff00'));
  19. this.gradient.addColorStop(1, new SColor('#3d4eff'));
  20. }
  21. /**
  22. * 矩形数据类型绘制
  23. */
  24. boundingRect() {
  25. return new SRect(
  26. this.minX,
  27. this.minY,
  28. this.maxX - this.minX,
  29. this.maxY - this.minY
  30. );
  31. }
  32. onDraw(painter: SPainter) {
  33. painter.pen.color = SColor.Black;
  34. painter.brush.gradient = this.gradient;
  35. painter.drawRect(0, 0, 1000, 1000);
  36. if (this.gradient instanceof SRadialGradient) {
  37. painter.brush.color = SColor.Transparent;
  38. painter.drawCircle(this.gradient.x1, this.gradient.y1, this.gradient.r1);
  39. painter.drawCircle(this.gradient.x2, this.gradient.y2, this.gradient.r2);
  40. }
  41. }
  42. }