-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathraphael.erl
152 lines (121 loc) · 3.43 KB
/
raphael.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%% Copyright (c) 2011 Joe Armstrong
%% See MIT-LICENSE for licensing information.
%% Time-stamp: <2011-02-12 11:55:07 joe>
-module(raphael).
-compile(export_all).
start(Pid) ->
random_seed(),
put(pid, Pid), %% Ok since we'll never change this
init(Pid),
%% spawn(fun() -> put(pid,Pid),random_seed(), push() end),
event_loop(Pid).
init(Pid) ->
%% clear_page(),
%% set_page_color("#ffffaa"),
load_js(Pid, "raphael-min.js"),
load_js(Pid, "jquery-1.5.min.js"),
Pid ! {eval,"document.body.innerHTML=''"},
Pid ! {eval, "document.body.style.backgroundColor='orange';"},
mk_div("canvas"),
mk_div("log"),
%% cmd("logit('hello')"),
cmd("c=Raphael('canvas',800,500)"),
cmd("r=c.circle(320,240,60);"),
cmd("c.image('joeold.jpg', 500, 50, 294, 195);"),
cmd("c.text(100,10,'red circle is draggble, blue box is clickable');"),
cmd("r.attr('fill','red');"),
cmd(drag_code()),
cmd("r.drag(move,start,up);"),
cmd("b=c.rect(40,40,50,50,10);"),
cmd("b.attr('fill','blue');"),
cmd("b.node.onclick= function(){send('click 3');};").
drag_code() ->
<<" start = function(){
this.ox = this.attr('cx');
this.oy = this.attr('cy');
this.attr({opacity: 1});
}
move = function (dx, dy) {
// move will be called with dx and dy
this.attr({cx: this.ox + dx, cy: this.oy + dy});
},
up = function () {
// alert('stop');
// restoring state
this.attr({opacity: .5});
};
">>.
event_loop(Pid) ->
receive
Any ->
io:format("??event loop:~p~n",[Any]),
event_loop(Pid)
end.
push() ->
%% every 100 ms push a random rectangle to the browser
sleep(100),
cmd(random_rect()),
push().
random_rect() ->
R=random:uniform(255),
G=random:uniform(255),
B=random:uniform(255),
X=400+random:uniform(100),
Y=50+random:uniform(100),
W=random:uniform(250),
H=random:uniform(100),
["glob[1].rect(",i2s(X),",",i2s(Y),",",i2s(W),",",i2s(H),").",
"attr('fill',", rgb(R,G,B),");"].
rgb(R,G,B) ->
["'rgb(",i2s(R),",",i2s(G),",",i2s(B),")'"].
sleep(N) ->
receive
after
N ->
true
end.
clear_page() ->
cmd("$('body').html('');").
set_page_color(C) ->
cmd(["$('body')",css([{"background-color",C}]),";"]).
css(L) ->
[".css({", [[fmt(I),":",fmt(J)]||{I,J} <- L],"})"].
fmt(I) ->
["'",I,"'"].
mk_div(Id) ->
cmd(["$('body').prepend('<div id=\"",Id,"\"></div>');"]).
load_js(Pid, File) ->
%% jQuery magic :-)
cmd(["loadScript('",File,"');"]),
%% wait for the script to run
receive
{browser,_,"loaded"} ->
io:format("script loaded~n"),
true
after 10000 ->
io:format("timeout loading:~p~n",[File])
end.
mkCanvas(Tag, Color, Width, Ht) ->
cmd(["$('body').prepend('<canvas id=\"",
Tag,"\" style=\"background:",Color,"\" width=\"",
i2s(Width),"\" height=\"", i2s(Ht),"\"></canvas>');"]).
cmd(Msg) ->
io:format("eval:~s~n",[Msg]),
get(pid) ! {eval, Msg}.
msg(Pid) ->
Pid ! {send, bgColor("#ffaacc")}.
bgColor(C) ->
["$('body').css({'background-color':'",C,"'});"].
i2s(I) ->
integer_to_list(I).
%% @doc Seed the random number generator.
%% This is evaluated for its side effect. Not well tested at all.
-spec random_seed() -> void.
random_seed() ->
{_,_,X} = erlang:now(),
{H,M,S} = time(),
H1 = H * X rem 32767,
M1 = M * X rem 32767,
S1 = S * X rem 32767,
put(random_seed, {H1,M1,S1}),
void.