Skip to content

Instantly share code, notes, and snippets.

@ToddG
Created July 4, 2011 21:51
Show Gist options
  • Save ToddG/1063991 to your computer and use it in GitHub Desktop.
Save ToddG/1063991 to your computer and use it in GitHub Desktop.
pushing server side data to client and displaying via raphaeljs
%% -*- mode: nitrogen -*-
-module (rtext2).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").
-include_lib("nitrogen_core/include/google_chart.hrl").
-include("records.hrl").
%% http://raphaeljs.com/text-rotation.html
main() -> #template { file="./site/templates/raphael.html" }.
title() -> "Todd's <b>Raphael Text Log</b> Page".
body() ->
Panel = #panel { style="margin: 50px 100px;", body=[
#span { text="Raphael Log Test Below" },
#panel { id=holder},
#span { id=transfer, text="---------------" }
]},
wf:comet(fun() -> bg_update(transfer, 1, 1000) end),
wf:wire(#script { script="
var hldr = obj('holder');
var tx = obj('transfer');
var setRText=function(text){
console.log('setting text');
var R = Raphael(hldr, 640, 480);
var attr = {font: '50px Helvetica, Arial', opacity: 0.5};
var txt = [];
txt[0] = R.text(320, 240, text).attr(attr).attr({fill: 'hsb(0, .5, 1)'});
};
var scrapeElement=function(){
setRText(tx.innerHTML);
setTimeout('scrapeElement()', 1000)};
scrapeElement();
"}),
wf:wire(transfer, #event { type=onAfterUpdate, actions=#script { script="
setRText(obj('transfer').text);
"}}),
Panel.
event(E) ->
wf:info("event: ~p~n", [ E ]).
bg_update(ControlID, Count, Delay) when Count > 5 ->
% not invoking self again...but we are getting invoked?
wf:info("count=~p~n", [ Count ]),
ok;
bg_update(ControlID, Count, Delay) ->
timer:sleep(Delay),
wf:update(ControlID, wf:to_list(Count)),
wf:flush(),
wf:info("count=~p~n", [ Count ]),
bg_update(ControlID, Count + 1, Delay).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment