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

draw.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { DrawShape, ShapeType } from "types"
  4. import { registerShapeUtils } from "./index"
  5. import { intersectPolylineBounds } from "utils/intersections"
  6. import { boundsContainPolygon } from "utils/bounds"
  7. import getStroke from "perfect-freehand"
  8. import {
  9. getBoundsFromPoints,
  10. getSvgPathFromStroke,
  11. translateBounds,
  12. } from "utils/utils"
  13. import { DotCircle } from "components/canvas/misc"
  14. import { shades } from "lib/colors"
  15. const pathCache = new WeakMap<DrawShape, string>([])
  16. const draw = registerShapeUtils<DrawShape>({
  17. boundsCache: new WeakMap([]),
  18. create(props) {
  19. return {
  20. id: uuid(),
  21. type: ShapeType.Draw,
  22. isGenerated: false,
  23. name: "Draw",
  24. parentId: "page0",
  25. childIndex: 0,
  26. point: [0, 0],
  27. points: [[0, 0]],
  28. rotation: 0,
  29. ...props,
  30. style: {
  31. strokeWidth: 2,
  32. strokeLinecap: "round",
  33. strokeLinejoin: "round",
  34. ...props.style,
  35. stroke: "transparent",
  36. },
  37. }
  38. },
  39. render(shape) {
  40. const { id, point, points } = shape
  41. if (points.length < 2) {
  42. return <DotCircle cx={point[0]} cy={point[1]} r={3} />
  43. }
  44. if (!pathCache.has(shape)) {
  45. pathCache.set(shape, getSvgPathFromStroke(getStroke(points)))
  46. }
  47. return <path id={id} d={pathCache.get(shape)} />
  48. },
  49. applyStyles(shape, style) {
  50. Object.assign(shape.style, style)
  51. shape.style.fill = "transparent"
  52. return this
  53. },
  54. getBounds(shape) {
  55. if (!this.boundsCache.has(shape)) {
  56. const bounds = getBoundsFromPoints(shape.points)
  57. this.boundsCache.set(shape, bounds)
  58. }
  59. return translateBounds(this.boundsCache.get(shape), shape.point)
  60. },
  61. getRotatedBounds(shape) {
  62. return this.getBounds(shape)
  63. },
  64. getCenter(shape) {
  65. const bounds = this.getBounds(shape)
  66. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  67. },
  68. hitTest(shape, point) {
  69. let pt = vec.sub(point, shape.point)
  70. let prev = shape.points[0]
  71. for (let i = 1; i < shape.points.length; i++) {
  72. let curr = shape.points[i]
  73. if (vec.distanceToLineSegment(prev, curr, pt) < 4) {
  74. return true
  75. }
  76. prev = curr
  77. }
  78. return false
  79. },
  80. hitTestBounds(this, shape, brushBounds) {
  81. const b = this.getBounds(shape)
  82. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  83. const rotatedCorners = [
  84. [b.minX, b.minY],
  85. [b.maxX, b.minY],
  86. [b.maxX, b.maxY],
  87. [b.minX, b.maxY],
  88. ].map((point) => vec.rotWith(point, center, shape.rotation))
  89. return (
  90. boundsContainPolygon(brushBounds, rotatedCorners) ||
  91. intersectPolylineBounds(
  92. shape.points.map((point) => vec.add(point, shape.point)),
  93. brushBounds
  94. ).length > 0
  95. )
  96. },
  97. rotateTo(shape, rotation) {
  98. shape.rotation = rotation
  99. return this
  100. },
  101. translateTo(shape, point) {
  102. shape.point = point
  103. return this
  104. },
  105. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  106. const initialShapeBounds = this.boundsCache.get(initialShape)
  107. shape.points = initialShape.points.map(([x, y]) => {
  108. return [
  109. bounds.width *
  110. (scaleX < 0
  111. ? 1 - x / initialShapeBounds.width
  112. : x / initialShapeBounds.width),
  113. bounds.height *
  114. (scaleY < 0
  115. ? 1 - y / initialShapeBounds.height
  116. : y / initialShapeBounds.height),
  117. ]
  118. })
  119. const newBounds = getBoundsFromPoints(shape.points)
  120. shape.point = vec.sub(
  121. [bounds.minX, bounds.minY],
  122. [newBounds.minX, newBounds.minY]
  123. )
  124. return this
  125. },
  126. transformSingle(shape, bounds, info) {
  127. this.transform(shape, bounds, info)
  128. return this
  129. },
  130. setParent(shape, parentId) {
  131. shape.parentId = parentId
  132. return this
  133. },
  134. setChildIndex(shape, childIndex) {
  135. shape.childIndex = childIndex
  136. return this
  137. },
  138. setPoints(shape, points) {
  139. // const bounds = getBoundsFromPoints(points)
  140. // const corner = [bounds.minX, bounds.minY]
  141. // const nudged = points.map((point) => vec.sub(point, corner))
  142. // this.boundsCache.set(shape, translategetBoundsFromPoints(nudged))
  143. // shape.point = vec.add(shape.point, corner)
  144. shape.points = points
  145. return this
  146. },
  147. canTransform: true,
  148. canChangeAspectRatio: true,
  149. })
  150. export default draw