UNPKG

2.34 kBJavaScriptView Raw
1import * as util from '../util';
2
3let rendererDefaults = util.defaults({
4 hideEdgesOnViewport: false,
5 textureOnViewport: false,
6 motionBlur: false,
7 motionBlurOpacity: 0.05,
8 pixelRatio: undefined,
9 desktopTapThreshold: 4,
10 touchTapThreshold: 8,
11 wheelSensitivity: 1,
12 debug: false,
13 showFps: false
14});
15
16let corefn = ({
17
18 renderTo: function( context, zoom, pan, pxRatio ){
19 let r = this._private.renderer;
20
21 r.renderTo( context, zoom, pan, pxRatio );
22 return this;
23 },
24
25 renderer: function(){
26 return this._private.renderer;
27 },
28
29 forceRender: function(){
30 this.notify('draw');
31
32 return this;
33 },
34
35 resize: function(){
36 this.invalidateSize();
37
38 this.emitAndNotify('resize');
39
40 return this;
41 },
42
43 initRenderer: function( options ){
44 let cy = this;
45
46 let RendererProto = cy.extension( 'renderer', options.name );
47 if( RendererProto == null ){
48 util.error( `Can not initialise: No such renderer \`${options.name}\` found. Did you forget to import it and \`cytoscape.use()\` it?` );
49 return;
50 }
51
52 if( options.wheelSensitivity !== undefined ){
53 util.warn(`You have set a custom wheel sensitivity. This will make your app zoom unnaturally when using mainstream mice. You should change this value from the default only if you can guarantee that all your users will use the same hardware and OS configuration as your current machine.`);
54 }
55
56 let rOpts = rendererDefaults(options);
57
58 rOpts.cy = cy;
59
60 cy._private.renderer = new RendererProto( rOpts );
61
62 this.notify('init');
63 },
64
65 destroyRenderer: function(){
66 let cy = this;
67
68 cy.notify('destroy'); // destroy the renderer
69
70 let domEle = cy.container();
71 if( domEle ){
72 domEle._cyreg = null;
73
74 while( domEle.childNodes.length > 0 ){
75 domEle.removeChild( domEle.childNodes[0] );
76 }
77 }
78
79 cy._private.renderer = null; // to be extra safe, remove the ref
80 cy.mutableElements().forEach(function( ele ){
81 let _p = ele._private;
82 _p.rscratch = {};
83 _p.rstyle = {};
84 _p.animation.current = [];
85 _p.animation.queue = [];
86 });
87 },
88
89 onRender: function( fn ){
90 return this.on('render', fn);
91 },
92
93 offRender: function( fn ){
94 return this.off('render', fn);
95 }
96
97});
98
99corefn.invalidateDimensions = corefn.resize;
100
101export default corefn;