選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

circle.tsx 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { CircleShape, ShapeType } from "types"
  4. import { boundsCache } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { createShape } from "./base-shape"
  8. const circle = createShape<CircleShape>({
  9. create(props) {
  10. return {
  11. id: uuid(),
  12. type: ShapeType.Circle,
  13. name: "Circle",
  14. parentId: "page0",
  15. childIndex: 0,
  16. point: [0, 0],
  17. radius: 20,
  18. rotation: 0,
  19. style: {},
  20. ...props,
  21. }
  22. },
  23. render({ id, radius }) {
  24. return <circle id={id} cx={radius} cy={radius} r={radius} />
  25. },
  26. getBounds(shape) {
  27. if (boundsCache.has(shape)) {
  28. return boundsCache.get(shape)
  29. }
  30. const {
  31. point: [x, y],
  32. radius,
  33. } = shape
  34. const bounds = {
  35. minX: x,
  36. maxX: x + radius * 2,
  37. minY: y,
  38. maxY: y + radius * 2,
  39. width: radius * 2,
  40. height: radius * 2,
  41. }
  42. boundsCache.set(shape, bounds)
  43. return bounds
  44. },
  45. hitTest(shape, test) {
  46. return (
  47. vec.dist(vec.addScalar(shape.point, shape.radius), test) < shape.radius
  48. )
  49. },
  50. hitTestBounds(shape, bounds) {
  51. const shapeBounds = this.getBounds(shape)
  52. return (
  53. boundsContained(shapeBounds, bounds) ||
  54. intersectCircleBounds(
  55. vec.addScalar(shape.point, shape.radius),
  56. shape.radius,
  57. bounds
  58. ).length > 0
  59. )
  60. },
  61. rotate(shape) {
  62. return shape
  63. },
  64. translate(shape, delta) {
  65. shape.point = vec.add(shape.point, delta)
  66. return shape
  67. },
  68. scale(shape, scale) {
  69. return shape
  70. },
  71. stretch(shape, scaleX, scaleY) {
  72. return shape
  73. },
  74. })
  75. export default circle