/
svg_test.html
161 lines (133 loc) · 4.06 KB
/
svg_test.html
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
153
154
155
156
157
158
159
160
161
<script>
svg_ns = 'http://www.w3.org/2000/svg';
document.onkeydown = function(e){update(e);};
var globstr = "Hello World";
var text;
var svg;
D = function(x, y){
return x == undefined ? y : x;
};
update = function(e) {
cc = e.keyCode;
// can browse this in the log window
console.log("pressed",e);
console.log("pressed="+e.keyCode+" shift="+e.shiftKey);
if(e.keyCode==8 && globstr.length>0){
globstr = globstr.substring(0, globstr.length-1);
} else {
if ( e.shiftKey == true) {
globstr += String.fromCharCode(cc);
} else {
globstr += String.fromCharCode(cc+32);
}
};
change_text(globstr);
return false;
}
mk_canvas = function(o) {
var x = document.createElementNS(svg_ns, 'svg');
x.setAttribute("width", D(o.width,700));
x.setAttribute("height", D(o.height, 200));
x.setAttribute("style","background-color:" + D(o.color,"red"));
x.setAttribute("id", o.id);
x.addEventListener("mousemove", drag, false);
// x.addEventListener("mousedown", mouse_down, false);
// x.addEventListener("mouseup", mouse_up, false);
return x;
};
mk_rect = function(o) {
var obj = document.createElementNS(svg_ns, 'rect');
obj.setAttribute('x', D(o.x, 10));
obj.setAttribute('y', D(o.y,20));
obj.setAttribute('width', D(o.width,120));
obj.setAttribute('height', D(o.ht, 40));
obj.setAttribute('fill', D(o.color,'#aabb11'));
obj.setAttribute('stroke','black');
obj.setAttribute('stroke-width',3);
obj.setAttribute("rx", D(o.rx, 5));
obj.setAttribute("ry", D(o.ry, 5));
obj.addEventListener("click", buttonClicked, false);
obj.addEventListener("mouseover", buttonOver, false);
obj.addEventListener("mouseout", buttonOut, false);
obj.addEventListener("onkeydown", key1, false);
return obj;
};
key1 = function(evt){
console.log("key1");
}
buttonClicked = function(evt) {
var x =evt.target;
x.setAttribute("fill", "orange");
};
buttonOver = function(evt) {
var x =evt.target;
x.setAttribute("fill", "red");
};
buttonOut = function(evt) {
var x =evt.target;
x.setAttribute("fill", "#aabb11");
};
keypress = function(evt) {
// var x = evt.target;
console.log("pressed");
}
mk_text = function(o) {
var text = document.createElementNS(svg_ns, "text");
text.setAttribute("fill", D(o.fill,"green"));
var size = D(o.size, 1);
text.setAttribute("font-size", size+"px");
var font = D(o.font,"Courier");
text.setAttribute("font-family", font);
text.setAttribute("x", D(o.x,10));
text.setAttribute("y", D(o.y,10));
text.setAttribute("text-anchor", D(o.anchor,"start"));
var str = D(o.str,"** missing str in text **");
var data = document.createTextNode(globstr);
text.setAttribute("id", o.id);
text.appendChild(data);
return text;
};
change_text = function(x) {
text.textContent=x;
}
window.onload = function() {
svg = mk_canvas({width:800, height:200, color:'#ffeecc',id:1}),
c = document.body.appendChild(svg);
c.appendChild(mk_rect({}));
text = mk_text({x:250,y:50, size:24, str:"Hello Joe"})
c.appendChild(text);
var fo = document.createElementNS(svg_ns,"foreignObject");
fo.setAttribute("x", 100);
fo.setAttribute("y", 100);
fo.setAttribute("width", 100);
fo.setAttribute("height", 100);
svg.appendChild(fo);
var n = document.createElement('input');
n.setAttribute('type','button');
n.setAttribute('value','click me');
n.setAttribute('name','joe');
fo.appendChild(n);
console.log("fo=",fo);
console.log("n=",n);
}
function drag(evt)
{
var t = evt.target;
// console.log("drag="+t);
}
</script>
<body>
<h2>Unsolved Problems</h2>
<ul>
<li>When I press backspace I change pages ... seems ok now ...
<li>Code to add a foreign object (button) does not work (why)
</ul>
<h2>refs</h2>
<ul>
<li><a href="http://www.w3.org/TR/SVG/shapes.html#RectElement">rect reference</a>
<li><a href="http://www.kevlindev.com/tutorials/basics/events/mouse/js_dom/index.htm">
kevlindev tutorials</a>
<li><a href="http://billmill.org/static/canvastutorial/keyboard.html">canvas tutorial</a>
</ul>
<button onclick="change_text('new')">change text</button>
</body>