Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to select anchors with mouse #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/Graphic/Graphic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Graphic.Element
viewMatrix:

highlight(graphic): A function that takes in a graphic and returns either
a color or null.
a {color, lineWidth} object or null.

###
throw "Not implemented"
Expand Down Expand Up @@ -94,7 +94,31 @@ class Graphic.Group extends Graphic.Element


class Graphic.Anchor extends Graphic.Element
render: (opts) ->
@highlightIfNecessary(opts)

hitDetect: (opts) ->
{x, y, viewMatrix} = opts
[myX, myY] = viewMatrix.compose(@matrix).origin()
distSq = (x - myX) * (x - myX) + (y - myY) * (y - myY)
if distSq <= @highlightRadius * @highlightRadius
return [@particularElement]
else
return null

highlightIfNecessary: ({highlight, ctx, viewMatrix}) ->
return unless highlight
highlightSpec = highlight(this)
if highlightSpec
ctx.save()
ctx.beginPath();
[myX, myY] = viewMatrix.compose(@matrix).origin()
ctx.arc(myX, myY, @highlightRadius, 0, 2 * Math.PI, false)
ctx.fillStyle = highlightSpec.color
ctx.fill()
ctx.restore()

highlightRadius: 5



Expand All @@ -104,7 +128,19 @@ class Graphic.Path extends Graphic.Element
@performPaintOps(opts)
@highlightIfNecessary(opts)

# Render anchor highlights, if necessary
for childGraphic in @childGraphics
childGraphic.render(opts)

hitDetect: (opts) ->
# Check for anchor hits
latestHit = null
for childGraphic in @childGraphics
latestHit = childGraphic.hitDetect(opts) ? latestHit
if latestHit
console.log('path hit anchor!')
return latestHit.concat(@particularElement)

opts.ctx = getDummyCanvasCtx()
{ctx, x, y} = opts
@buildPath(opts)
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ module.exports = Element = Node.createVariant

matrix: ->
matrix = new Util.Matrix()
for position in @childrenOfType(Model.Position)
matrix = matrix.compose(position.matrix())
for transform in @childrenOfType(Model.Transform)
matrix = matrix.compose(transform.matrix())
return matrix
Expand Down
34 changes: 31 additions & 3 deletions src/Model/Model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ Model.Transform.addChildren [
]


Model.Position = Model.Component.createVariant
label: "Position"
matrix: ->
{x, y} = @getAttributesValuesByName()
return Util.Matrix.naturalConstruct(x, y, 1, 1, 0)
defaultAttributesToChange: ->
{x, y} = @getAttributesByName()
return [x, y]
controllableAttributes: ->
{x, y} = @getAttributesByName()
return [x, y]
controlPoints: ->
{x, y} = @getAttributesByName()
return [
{point: [0, 0], attributesToChange: [x, y], filled: true}
]

Model.Position.addChildren [
createAttribute("X", "x", "0.00")
createAttribute("Y", "y", "0.00")
]


Model.Fill = Model.Component.createVariant
label: "Fill"
Expand Down Expand Up @@ -122,19 +144,25 @@ Model.Shape.addChildren [
]


Model.Point = Model.Element.createVariant()
Model.Point.addChildren [
Model.Position.createVariant()
]


Model.Group = Model.Shape.createVariant
label: "Group"
graphicClass: Graphic.Group


Model.Anchor = Model.Shape.createVariant
Model.Anchor = Model.Point.createVariant
label: "Anchor"
graphicClass: Graphic.Anchor

createAnchor = (x, y) ->
anchor = Model.Anchor.createVariant()
transform = anchor.childOfType(Model.Transform)
attributes = transform.getAttributesByName()
position = anchor.childOfType(Model.Position)
attributes = position.getAttributesByName()
attributes.x.setExpression(x)
attributes.y.setExpression(y)
return anchor
Expand Down