Changeset - 3bb58b74c9c1
[Not reviewed]
default
0 2 0
Drew Perttula - 8 years ago 2017-04-12 06:04:18
drewp@bigasterisk.com
timeline: add cache of floats between graph updates for smoother redraws
Ignore-this: dbdc4782855087f4ec12f1d24d440484
2 files changed with 16 insertions and 3 deletions:
0 comments (0 inline, 0 general)
light9/web/graph.coffee
Show inline comments
 
@@ -127,24 +127,25 @@ class window.SyncedGraph
 
    if @patchSenderUrl
 
      @_client = new RdfDbClient(@patchSenderUrl, @clearGraph.bind(@),
 
                                 @_applyPatch.bind(@), @setStatus)
 
    
 
  clearGraph: -> # for debugging
 
    # just deletes the statements; watchers are unaffected.
 
    if @graph?
 
      @_applyPatch({addQuads: [], delQuads: @graph.find()})
 

	
 
    # if we had a Store already, this lets N3.Store free all its indices/etc
 
    @graph = N3.Store()
 
    @_addPrefixes(@prefixes)
 
    @cachedFloatValues = new Map();
 
    
 
      
 
  _addPrefixes: (prefixes) ->
 
    @graph.addPrefixes(prefixes)
 
        
 
  Uri: (curie) ->
 
    N3.Util.expandPrefixedName(curie, @graph._prefixes)
 

	
 
  Literal: (jsValue) ->
 
    N3.Util.createLiteral(jsValue)
 

	
 
  LiteralRoundedFloat: (f) ->
 
@@ -170,24 +171,25 @@ class window.SyncedGraph
 
    [q.subject, q.predicate, q.object, q.graph] for q in @graph.find()
 

	
 
  applyAndSendPatch: (patch) ->
 
    if !Array.isArray(patch.addQuads) || !Array.isArray(patch.delQuads)
 
      throw new Error("corrupt patch: #{patch}")
 
    @_applyPatch(patch)
 
    @_client.sendPatch(patch) if @_client
 

	
 
  _applyPatch: (patch) ->
 
    # In most cases you want applyAndSendPatch.
 
    # 
 
    # This is the only method that writes to @graph!
 
    @cachedFloatValues.clear()
 
    for quad in patch.delQuads
 
      @graph.removeTriple(quad)
 
    for quad in patch.addQuads
 
      @graph.addTriple(quad)
 
    #log('applied patch locally', patchSizeSummary(patch))
 
    @_watchers.graphChanged(patch)
 
    @_autoDeps.graphChanged(patch)
 

	
 
  getObjectPatch: (s, p, newObject, g) ->
 
    # send a patch which removes existing values for (s,p,*,c) and
 
    # adds (s,p,newObject,c). Values in other graphs are not affected.
 
    existing = @graph.findByIRI(s, p, null, g)
 
@@ -224,25 +226,31 @@ class window.SyncedGraph
 
    @_autoDeps.askedFor(s, p, null, null)
 
    quads = @graph.findByIRI(s, p)
 
    switch quads.length
 
      when 0
 
        throw new Error("no value for "+s+" "+p)
 
      when 1
 
        obj = quads[0].object
 
        return obj
 
      else
 
        throw new Error("too many values: " + JSON.stringify(quads))
 

	
 
  floatValue: (s, p) ->
 
    parseFloat(N3.Util.getLiteralValue(@_singleValue(s, p)))
 
    key = s + '|' + p
 
    hit = @cachedFloatValues.get(key)
 
    return hit if hit != undefined
 

	
 
    ret = parseFloat(N3.Util.getLiteralValue(@_singleValue(s, p)))
 
    @cachedFloatValues.set(key, ret)
 
    return ret
 
    
 
  stringValue: (s, p) ->
 
    N3.Util.getLiteralValue(@_singleValue(s, p))
 
    
 
  uriValue: (s, p) ->
 
    @_singleValue(s, p)
 

	
 
  objects: (s, p) ->
 
    @_autoDeps.askedFor(s, p, null, null)
 
    quads = @graph.findByIRI(s, p)
 
    return (q.object for q in quads)
 

	
light9/web/timeline/timeline.coffee
Show inline comments
 
@@ -424,25 +424,30 @@ Polymer
 
            return false
 
    return true
 
      
 
            
 
  update: (patch) ->
 
    if not @patchCouldAffectMe(patch)
 
      # as autodep still fires all handlers on all patches, we just
 
      # need any single dep to cause another callback. (without this,
 
      # we would no longer be registered at all)
 
      @graph.subjects(@uri, @uri, @uri)
 
      return
 
    if @isDetached?
 
      return 
 
      return
 
 
 
    @updateDisplay()
 

	
 
  updateDisplay: ->
 
      
 
    # update our note DOM and SVG elements based on the graph
 
    U = (x) -> @graph.Uri(x)
 

	
 
    yForV = (v) => @offsetTop + (1 - v) * @offsetHeight
 

	
 
    originTime = @graph.floatValue(@uri, U(':originTime'))
 
    effect = @graph.uriValue(@uri, U(':effectClass'))
 
    for curve in @graph.objects(@uri, U(':curve'))
 
      if @graph.uriValue(curve, U(':attr')) == U(':strength')
 
        @updateStrengthCurveEtc(originTime, curve, yForV, effect)
 
        
 
  updateStrengthCurveEtc: (originTime, curve, yForV, effect) ->
 
@@ -550,24 +555,25 @@ Polymer
 
      @graph.patchObject(@existingColorScaleSetting, U(':value'), settingValue, @song)
 
    else
 
      setting = @graph.nextNumberedResource(@uri + 'set')
 
      patch = {delQuads: [], addQuads: [
 
        quad(@uri, U(':setting'), setting)
 
        quad(setting, U(':effectAttr'), U(':colorScale'))
 
        quad(setting, U(':value'), settingValue)
 
        ]}
 
      @graph.applyAndSendPatch(patch)
 
    
 
  addHandler: ->
 
    @graph.runHandler(@update.bind(@))
 
    
 
  update: ->
 
    U = (x) -> @graph.Uri(x)
 
    @effect = @graph.uriValue(@uri, U(':effectClass'))
 
    @effectLabel = @effect.replace(/.*\//, '')
 
    @noteLabel = @uri.replace(/.*\//, '')
 

	
 
    @existingColorScaleSetting = null
 
    for setting in @graph.objects(@uri, U(':setting'))
 
      ea = @graph.uriValue(setting, U(':effectAttr'))
 
      value = @graph.stringValue(setting, U(':value'))
 
      if ea == U(':colorScale')
 
        @colorScaleFromGraph = value
 
@@ -726,25 +732,24 @@ Polymer
 
      @ctx.beginPath()
 
      @ctx.moveTo(@cursorPath.mid0.e(1), @cursorPath.mid0.e(2))
 
      @ctx.lineTo(p.e(1), p.e(2)) for p in [
 
        @cursorPath.mid1, @cursorPath.mid2, @cursorPath.mid3]
 
      @ctx.fill()
 
      
 
      @ctx.strokeStyle = '#ff0303'
 
      @ctx.lineWidth = 3
 
      @ctx.beginPath()
 
      _line(@ctx, @cursorPath.bot0, @cursorPath.bot1, '#ff0303', '3px')
 
      @ctx.stroke()
 
    
 
    
 
Polymer
 
  is: 'light9-adjusters-canvas'
 
  behaviors: [ Polymer.IronResizableBehavior ]
 
  properties:
 
    adjs: { type: Object, notify: true }, # adjId: Adjustable
 
  listeners: 'iron-resize': 'update'
 
  ready: ->
 
    @adjs = {}
 
    @ctx = @$.canvas.getContext('2d')
 

	
 
    @dirty = false
 
    @redraw()
0 comments (0 inline, 0 general)