-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgwidgets.js
555 lines (358 loc) · 13.3 KB
/
svgwidgets.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// SVG iPython Widgets
// A library to manipulate Scalar Vector Graphics in the iPython notebook
// using Widgets, which synchronise between the Python backend and the
// Javascript frontend.
// Copyright 2014 Tom Brown ([email protected])
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// code to define our new Backbone.js View objects
require(["widgets/js/widget"], function(WidgetManager){
// widget_properties is put into the global scope by svgwidgets.py
// and contains a list of class names with their properties
for(var class_name in widget_properties){
var fertile = widget_properties[class_name]["fertile"];
var view_name = widget_properties[class_name]["view_name"];
// choose the base class to inherit from, depending on whether
// the class can have children
if(fertile){
var BaseClass = IPython.WidgetManager._view_types.ContainerView;
}
else{
var BaseClass = IPython.DOMWidgetView;
}
// the class definition of the Backbone.js view
var NewView = BaseClass.extend({
// store the properties we need for each class
fertile: fertile,
draggable: widget_properties[class_name]["draggable"],
// note that this.attributes is reserved by Backbone.js
svg_attributes: Object.keys(widget_properties[class_name]["attributes"]),
tag_name: widget_properties[class_name]["tag_name"],
render: function(){
// called when view is rendered
// the DOM element to render
var el = document.createElementNS("http://www.w3.org/2000/svg",this.tag_name);
// needs to be called to set this.el to el properly
this.setElement(el);
// render any children with the ContainerView render method
if(this.fertile){
this.constructor.__super__.render.apply(this);
}
// set the initial mode for the SVG
if(this.tag_name === "svg"){
this.mode = null;
}
// make sure all attributes, etc., are up-to-date
this.update();
},
update: function(){
// called when something is changed
// update all the SVG attributes
for(var i=0, length = this.svg_attributes.length; i<length; i++){
var name = this.svg_attributes[i];
var value = this.model.get(name);
this.el.setAttribute(name.replace("_","-"),value);
}
// update the content for TextWidget
if(this.tag_name === "text"){
this.el.innerHTML = this.model.get("content");
}
// update the SVG drawing mode
if(this.tag_name === "svg"){
var mode = this.model.get("mode");
if(mode !== this.mode){
set_svg_mode(this,mode);
}
}
this.constructor.__super__.update.apply(this);
},
on_msg: function (content) {
// deal with requests from the Python backend
if (content["message_type"] === "get_html") {
var message = {"message_type": "html","html": this.el.outerHTML};
this.send(message);
}
}
});
// register the View with the widget manager
WidgetManager.register_widget_view(view_name, NewView);
}
});
// attach all the event listeners to the SVG view object, depending on
// the mode
var set_svg_mode = function(svg_view,mode){
svg_view.mode = mode;
// define useful variables for the drawing modes
if(mode !== "select"){
var class_name = mode.charAt(0).toUpperCase() + mode.slice(1) + "Widget";
var properties = widget_properties[class_name];
var tag_name = properties["tag_name"];
var attributes = properties["attributes"];
var gui_controls = ["fill","fill_opacity","stroke","stroke_width"];
}
// svg_view.el.onmousedown is set to listeners[mode] at the end
var listeners = {};
// if mousedown in select mode, drag any draggable elements
listeners.select = function(event){
// SVG View which has been selected
var selected;
// recursively see which if any child views have been selected
var search_children = function(view){
if(event.target === view.el){
return view;
}
else{
for(var child_id in view.child_views){
var child_view = view.child_views[child_id];
var result = search_children(child_view);
if(result !== undefined){
return result;
}
}
return undefined;
}
}
selected = search_children(svg_view);
// if the selected item is draggable, let it be dragged
if(selected !== undefined && selected.draggable){
svg_view.previousX = event.pageX;
svg_view.previousY = event.pageY;
//if the object has been translated, get its current translation coords
var coords = false;
if(selected.el.hasAttribute("transform")){
var transform_string = selected.el.getAttribute("transform");
var coords = parse_translate(transform_string);
if(coords){
svg_view.translateX = coords[0];
svg_view.translateY = coords[1];
}
}
if(!coords){
svg_view.translateX = 0.0;
svg_view.translateY = 0.0;
}
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
svg_view.translateX += event.pageX - svg_view.previousX;
svg_view.translateY += event.pageY - svg_view.previousY;
svg_view.previousX = event.pageX;
svg_view.previousY = event.pageY;
var transform = "translate(" + svg_view.translateX + "," + svg_view.translateY + ")";
selected.el.setAttribute("transform",transform);
}
svg_view.el.onmouseup = function(event){
// send the final result to the model
var transform = selected.el.getAttribute("transform");
selected.model.set("transform",transform);
selected.touch();
// turn listeners off again
selected = undefined;
svg_view.el.onmousemove = null;
svg_view.el.onmouseup = null;
}
}
}
// some universal functions for the drawing of elements
var set_style_attributes = function(child){
for(var i=0, length=gui_controls.length; i<length; i++){
var attribute = gui_controls[i];
if(attribute in attributes){
child.setAttribute(attribute.replace("_","-"),svg_view.model.get(attribute));
}
}
child.setAttribute("transform","");
}
var onmouseup = function(child){
svg_view.el.removeChild(child);
var child_attributes = {};
for(var attribute in attributes){
child_attributes[attribute] = child.getAttribute(attribute.replace("_","-"));
}
var message = {"message_type": "new", "class_name": class_name, "attributes": child_attributes};
svg_view.send(message);
// turn listeners off again
svg_view.el.onmousemove = null;
svg_view.el.onmouseup = null;
}
// now for each element type, define the mouse behaviour on the
// geometry
listeners.rect = function(event){
set_svg_ref_coords(svg_view.el);
var coords = get_svg_coords(event,svg_view.el);
var startX = coords.x;
var startY = coords.y;
var x = startX;
var y = startY;
var width = 10;
var height = 10;
var child = document.createElementNS("http://www.w3.org/2000/svg",tag_name);
set_style_attributes(child);
child.setAttribute("x",x);
child.setAttribute("y",y);
child.setAttribute("width",width);
child.setAttribute("height",height);
svg_view.el.appendChild(child);
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
var coords = get_svg_coords(event,svg_view.el);
x = Math.min(coords.x,startX);
y = Math.min(coords.y,startY);
width = Math.abs(coords.x - startX);
height = Math.abs(coords.y - startY);
child.setAttribute("x",x);
child.setAttribute("y",y);
child.setAttribute("width",width);
child.setAttribute("height",height);
}
svg_view.el.onmouseup = function(event){
onmouseup(child);
}
}
// if mousedown in circle mode, draw a circle
listeners.circle = function(event){
set_svg_ref_coords(svg_view.el);
var coords = get_svg_coords(event,svg_view.el);
var cx = coords.x;
var cy = coords.y;
var r = 5;
var child = document.createElementNS("http://www.w3.org/2000/svg",tag_name);
set_style_attributes(child);
child.setAttribute("cx",cx);
child.setAttribute("cy",cy);
child.setAttribute("r",r);
svg_view.el.appendChild(child);
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
var coords = get_svg_coords(event,svg_view.el);
r = Math.abs(coords.x - cx);
child.setAttribute("r",r);
}
svg_view.el.onmouseup = function(event){
onmouseup(child);
}
}
listeners.ellipse = function(event){
set_svg_ref_coords(svg_view.el);
var coords = get_svg_coords(event,svg_view.el);
var cx = coords.x;
var cy = coords.y;
var rx = 5;
var ry = 5;
var child = document.createElementNS("http://www.w3.org/2000/svg",tag_name);
set_style_attributes(child);
child.setAttribute("cx",cx);
child.setAttribute("cy",cy);
child.setAttribute("rx",rx);
child.setAttribute("ry",ry);
svg_view.el.appendChild(child);
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
var coords = get_svg_coords(event,svg_view.el);
rx = Math.abs(coords.x - cx);
ry = Math.abs(coords.y - cy);
child.setAttribute("rx",rx);
child.setAttribute("ry",ry);
}
svg_view.el.onmouseup = function(event){
onmouseup(child);
}
}
listeners.line = function(event){
set_svg_ref_coords(svg_view.el);
var coords = get_svg_coords(event,svg_view.el);
var x1 = coords.x;
var y1 = coords.y;
var x2 = x1 + 5;
var y2 = y1;
var child = document.createElementNS("http://www.w3.org/2000/svg",tag_name);
set_style_attributes(child);
child.setAttribute("x1",x1);
child.setAttribute("y1",y1);
child.setAttribute("x2",x2);
child.setAttribute("y2",y2);
svg_view.el.appendChild(child);
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
var coords = get_svg_coords(event,svg_view.el);
x2 = coords.x;
y2 = coords.y;
child.setAttribute("x2",x2);
child.setAttribute("y2",y2);
}
svg_view.el.onmouseup = function(event){
onmouseup(child);
}
}
listeners.path = function(event){
set_svg_ref_coords(svg_view.el);
var coords = get_svg_coords(event,svg_view.el);
var d = "M" + String(coords.x) + "," + String(coords.y);
var child = document.createElementNS("http://www.w3.org/2000/svg",tag_name);
set_style_attributes(child);
child.setAttribute("d",d);
svg_view.el.appendChild(child);
// now define the behaviour on mousemove and mouseup
// NB: mouseout doesn't work on SVG in Chrome
svg_view.el.onmousemove = function(event){
var coords = get_svg_coords(event,svg_view.el);
d += " " + String(coords.x) + "," + String(coords.y);
child.setAttribute("d",d);
}
svg_view.el.onmouseup = function(event){
onmouseup(child);
}
}
svg_view.el.onmousedown = listeners[mode];
}
// a hopefully browser-independent way of getting the SVG offsets
// inspired by http://stackoverflow.com/questions/20957627/how-do-you-transform-event-coordinates-to-svg-coordinates-despite-bogus-getbound
// called only on mousedown, to avoid excessive calling during
// mousemove - this has side-effect that scrolling while drawing an
// element will disrupt the positioning
var set_svg_ref_coords = function(svg){
svg.reference = svg.getScreenCTM();
}
// get the coordinates of the event in the SVG's frame of reference
var get_svg_coords = function(event,svg){
return {x: event.clientX - svg.reference["e"],
y: event.clientY - svg.reference["f"]}
}
// parse a string to look for translate coordinates,
// e.g. "translate(2.3,4.5)" will return [2.3,4.5]; if the string is
// nonsense, returns false
var parse_translate = function(a_string){
var index = a_string.indexOf("translate(")
if(index < 0){
return false;
}
a_string = a_string.slice(index+10);
index = a_string.indexOf(")");
if(index < 0){
return false;
}
a_string = a_string.slice(0,index);
var coords = a_string.split(",");
if(coords.length !== 2){
return false;
}
for(var i=0;i<2;i++){
coords[i] = parseFloat(coords[i]);
if(coords[i] === NaN){
return false;
}
}
return coords;
}