Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 3c4eb4f

Browse files
Add conversion function from canvas item to Path2d
This only works for rectangle items right now, but may be extended in the future
1 parent f8ef850 commit 3c4eb4f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/library/src/util/canvas.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,27 @@ const renderElement = (ctx, content, cache={}) => {
211211

212212
export const makeRenderFunction = (content, cache) => (ts, canvas, ctx) =>
213213
(content || []).forEach(c => renderElement(ctx, c, cache))
214+
215+
export const makePath = (ctx, content) => {
216+
// Transformation as above
217+
ctx.save()
218+
ctx.beginPath()
219+
ctx.translate(content.left, content.top)
220+
ctx.rotate(toRadians(content.angle))
221+
222+
const path = new Path2D()
223+
224+
// Type-specific path extensions
225+
switch (content.type) {
226+
case 'rect':
227+
path.rect(
228+
-content.width / 2, -content.height / 2,
229+
content.width, content.height,
230+
)
231+
break
232+
// TODO: cover remaining object types
233+
}
234+
235+
ctx.restore()
236+
return path
237+
}

0 commit comments

Comments
 (0)