Skip to content

Commit

Permalink
New structures default to (0,0) and some work on info dict
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgrosner committed Jan 8, 2012
1 parent 0c92158 commit e27790d
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 36 deletions.
1 change: 0 additions & 1 deletion Atom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ class Atom extends Element

asArray: => [@x, @y, @z]


29 changes: 16 additions & 13 deletions CanvasContext.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ class CanvasContext
el.init()

@canvas.addEventListener 'mousewheel', @changeZoom
@zoom_prev = 1
@zoom = 1
console.log @.findBestZoom()

@x_origin = @canvas.width/2
@zoom = @.findBestZoom()
@zoom_prev = @zoom
center = @.avgCenterOfAllElements()

for el in @elements
el.translateTo(center)

@x_origin = @canvas.width/2
@y_origin = @canvas.height/2
@.clear()
@canvas.addEventListener 'dblclick', @translateOrigin

@.drawAll()

findBestZoom: =>
Expand All @@ -42,7 +45,7 @@ class CanvasContext
max_x = Math.abs(a.x)
if Math.abs(a.y) > max_y
max_y = Math.abs(a.y)
@zoom = if max_x > max_y then @canvas.width/max_x else @canvas.width/max_y
if max_x > max_y then @canvas.width/(2*max_x) else @canvas.width/(2*max_y)

drawGridLines: =>
"""
Expand Down Expand Up @@ -80,6 +83,7 @@ class CanvasContext
clear: =>
@canvas.width = @canvas.width
@context.translate @x_origin, @y_origin
@.drawGridLines()

mousedown: (e) =>
@mouse_x_prev = e.x
Expand Down Expand Up @@ -134,19 +138,18 @@ class CanvasContext
htmlInfo = (index, oldhtml) =>
el_info = ("<p>#{el.writeContextInfo()}</p>" for el in @elements)
el_info.join " "
#"#{oldhtml}<br><br>#{el_info}"
"<a href=\"javascript:window.ctx.changeAllDrawMethods('points');\">Canvas</a><br><br>#{el_info}"
$("#ctx-info").html htmlInfo

avgCenterOfAllElements: =>
avgs = [0.0, 0.0, 0.0]
total_atoms = 0
for el in @elements
elAvg = el.avgCenter()
avgs[0] += elAvg[0]
avgs[1] += elAvg[1]
avgs[2] += elAvg[2]
ela = el.atoms.length
avgs[0] += elAvg[0]*ela
avgs[1] += elAvg[1]*ela
avgs[2] += elAvg[2]*ela
total_atoms += el.atoms.length
(a/total_atoms for a in avgs)



47 changes: 33 additions & 14 deletions Element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Element
@cc = @parent.cc

@info = {}

writeContextInfo: =>
if @.constructor.name != "Residue"
# THIS WILL BREAK IE COMPAT.
Expand All @@ -31,6 +31,7 @@ class Element

propogateInfo: (info) ->
@info = info
@info.drawColor = if @info.drawColor? then @info.drawColor else randomRGB()
#if not @info.drawColor?
# @info.drawColor = randomRGB()
for c in @children
Expand All @@ -57,22 +58,33 @@ class Element
@.drawPoints()

drawPaths: =>
isBonded = (a1, a2) ->
if a1.parent.typeName() != a2.parent.typeName()
false
else if atomAtomDistance(a1, a2) < 3 and a1.parent.isProtein()
true
else if atomAtomDistance(a1, a2) < 10 and a1.parent.isDNA()
true
else
false

x = @cc.context
@info.drawColor = if @info.drawColor? then @info.drawColor else randomRGB()

for i in [2..@atoms.length-1]
x.beginPath()
a2 = @atoms[i]
a1 = @atoms[i-1]
if atomAtomDistance(a1, a2) < 10
x.moveTo(a1.x, a1.y)
x.lineTo(a2.x, a2.y)
x.strokeStyle = arrayToRGB (c + a1.z for c in @info.drawColor)
#x.lineJoin = "round"
#x.lineCap = "round"
x.lineWidth = (3*a1.z + 200)/200
x.closePath()
x.stroke()
for j in [i+1..i+5] when j < @atoms.length-1
x.beginPath()
a2 = @atoms[i]
a1 = @atoms[j]
if isBonded a1, a2
x.moveTo(a1.x, a1.y)
x.lineTo(a2.x, a2.y)
x.strokeStyle = arrayToRGB (c + a1.z for c in @info.drawColor)
#x.lineJoin = "round"
#x.lineCap = "round"
lw = (3*a1.z + 200)/200
x.lineWidth = if lw > 0 then lw else lw
x.closePath()
x.stroke()

drawPoints: =>
@atoms.sort sortByZ
Expand All @@ -99,3 +111,10 @@ class Element
avgs[2] += a.z
(a/@atoms.length for a in avgs)

translateTo: (center) =>
for a in @atoms
a.x -= center[0]
a.y -= center[1]
a.z -= center[2]


Loading

0 comments on commit e27790d

Please sign in to comment.