-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdraw.js
206 lines (160 loc) · 6.02 KB
/
draw.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
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Drawing = require('../drawing');
var Axes = require('../../plots/cartesian/axes');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
module.exports = function draw(gd) {
var fullLayout = gd._fullLayout,
imageDataAbove = [],
imageDataSubplot = {},
imageDataBelow = [],
subplot,
i;
// Sort into top, subplot, and bottom layers
for(i = 0; i < fullLayout.images.length; i++) {
var img = fullLayout.images[i];
if(img.visible) {
if(img.layer === 'below' && img.xref !== 'paper' && img.yref !== 'paper') {
subplot = img.xref + img.yref;
var plotinfo = fullLayout._plots[subplot];
if(plotinfo.mainplot) {
subplot = plotinfo.mainplot.id;
}
if(!imageDataSubplot[subplot]) {
imageDataSubplot[subplot] = [];
}
imageDataSubplot[subplot].push(img);
} else if(img.layer === 'above') {
imageDataAbove.push(img);
} else {
imageDataBelow.push(img);
}
}
}
var anchors = {
x: {
left: { sizing: 'xMin', offset: 0 },
center: { sizing: 'xMid', offset: -1 / 2 },
right: { sizing: 'xMax', offset: -1 }
},
y: {
top: { sizing: 'YMin', offset: 0 },
middle: { sizing: 'YMid', offset: -1 / 2 },
bottom: { sizing: 'YMax', offset: -1 }
}
};
// Images must be converted to dataURL's for exporting.
function setImage(d) {
var thisImage = d3.select(this);
if(this.img && this.img.src === d.source) {
return;
}
thisImage.attr('xmlns', xmlnsNamespaces.svg);
var imagePromise = new Promise(function(resolve) {
var img = new Image();
this.img = img;
// If not set, a `tainted canvas` error is thrown
img.setAttribute('crossOrigin', 'anonymous');
img.onerror = errorHandler;
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL('image/png');
thisImage.attr('xlink:href', dataURL);
};
thisImage.on('error', errorHandler);
thisImage.on('load', resolve);
img.src = d.source;
function errorHandler() {
thisImage.remove();
resolve();
}
}.bind(this));
gd._promises.push(imagePromise);
}
function applyAttributes(d) {
var thisImage = d3.select(this);
// Axes if specified
var xa = Axes.getFromId(gd, d.xref),
ya = Axes.getFromId(gd, d.yref);
var size = fullLayout._size,
width = xa ? Math.abs(xa.l2p(d.sizex) - xa.l2p(0)) : d.sizex * size.w,
height = ya ? Math.abs(ya.l2p(d.sizey) - ya.l2p(0)) : d.sizey * size.h;
// Offsets for anchor positioning
var xOffset = width * anchors.x[d.xanchor].offset,
yOffset = height * anchors.y[d.yanchor].offset;
var sizing = anchors.x[d.xanchor].sizing + anchors.y[d.yanchor].sizing;
// Final positions
var xPos = (xa ? xa.r2p(d.x) + xa._offset : d.x * size.w + size.l) + xOffset,
yPos = (ya ? ya.r2p(d.y) + ya._offset : size.h - d.y * size.h + size.t) + yOffset;
// Construct the proper aspectRatio attribute
switch(d.sizing) {
case 'fill':
sizing += ' slice';
break;
case 'stretch':
sizing = 'none';
break;
}
thisImage.attr({
x: xPos,
y: yPos,
width: width,
height: height,
preserveAspectRatio: sizing,
opacity: d.opacity
});
// Set proper clipping on images
var xId = xa ? xa._id : '',
yId = ya ? ya._id : '',
clipAxes = xId + yId;
thisImage.call(Drawing.setClipUrl, clipAxes ?
('clip' + fullLayout._uid + clipAxes) :
null
);
}
var imagesBelow = fullLayout._imageLowerLayer.selectAll('image')
.data(imageDataBelow),
imagesAbove = fullLayout._imageUpperLayer.selectAll('image')
.data(imageDataAbove);
imagesBelow.enter().append('image');
imagesAbove.enter().append('image');
imagesBelow.exit().remove();
imagesAbove.exit().remove();
imagesBelow.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
imagesAbove.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
var allSubplots = Object.keys(fullLayout._plots);
for(i = 0; i < allSubplots.length; i++) {
subplot = allSubplots[i];
var subplotObj = fullLayout._plots[subplot];
// filter out overlaid plots (which havd their images on the main plot)
// and gl2d plots (which don't support below images, at least not yet)
if(!subplotObj.imagelayer) continue;
var imagesOnSubplot = subplotObj.imagelayer.selectAll('image')
// even if there are no images on this subplot, we need to run
// enter and exit in case there were previously
.data(imageDataSubplot[subplot] || []);
imagesOnSubplot.enter().append('image');
imagesOnSubplot.exit().remove();
imagesOnSubplot.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
}
};